def javaClassToHtml(javaClass, level=0): """return html class representation.""" if type(javaClass) is StringType or type(javaClass) is UnicodeType: class_ = Class.forName(javaClass) className = javaClass else: class_ = javaClass className = javaClass.__name__ if Class.isInterface(class_): typeStr = 'interface' elif Modifier.isAbstract(Class.getModifiers(class_)): typeStr = 'abstract class' else: typeStr = 'class' #indent = (' '*level+1)*4) indent = ' ' * (level + 1) * 4 header = '%s<p>%s %s' % (indent, typeStr, className) super_ = Class.getSuperclass(class_) if super_ is not None: header += ' extends ' + Class.getName(super_) interfaces = Class.getInterfaces(class_) if len(interfaces) > 0: interfacesNames = join(map(Class.getName, interfaces), ', ') header += ' implements %s' % (interfacesNames) body = membersToHtml(lambda: Class.getDeclaredFields(class_), level) body += membersToHtml(lambda: Class.getDeclaredConstructors(class_), level) body += membersToHtml(lambda: Class.getDeclaredMethods(class_), level) body = '%s{</p>\n%s<ul class="ind%i">\n%s' % (header, indent, level, body) body = boldJavaKeywords(classLink(className, body)) li = '%s<li>\n%s\n%s</li>\n' ind2 = (' ' * (level + 2) * 4) for cl in Class.getDeclaredClasses(class_): body += li % (ind2, javaClassToHtml(Class.getName(cl), level+1), \ indent*2) return '%s%s</ul>}' % (body, indent)
def javaStaticMethods(clazz): """Return names of static methods for a given Java class.""" static_methods = {} for method in Class.getDeclaredMethods(clazz): modifiers = method.getModifiers() if Modifier.isStatic(modifiers) and Modifier.isPublic(modifiers): static_methods[method.name] = method methods = static_methods.keys() for base in clazz.__bases__: if not ispython(base): methods.extend(javaStaticMethods(base)) return methods
def staticMethodNames(clazz): """return a list of static method name for a class""" # TODO get static methods from base classes static_methods = {} declared_methods = Class.getDeclaredMethods(clazz) for method in declared_methods: if Modifier.isStatic(method.getModifiers()) and Modifier.isPublic(method.getModifiers()): static_methods[method.name] = method methods = static_methods.keys() for eachBase in clazz.__bases__: methods.extend(staticMethodNames(eachBase)) return methods
def javaInstanceMethods(clazz): """Return names of instance methods for a given Java class.""" names = set() for method in Class.getDeclaredMethods(clazz): modifiers = method.getModifiers() if not Modifier.isStatic(modifiers) and Modifier.isPublic(modifiers): name = method.name names.add(name) if name.startswith('get') and len(name) > 3 and not method.getParameterTypes(): property_name = name[3].lower() + name[4:] names.add(property_name) for base in clazz.__bases__: if not ispython(base): names = names | javaInstanceMethods(base) return names
def staticMethodNames(clazz): """return a list of static method name for a class""" # TODO get static methods from base classes static_methods = {} declared_methods = Class.getDeclaredMethods(clazz) for method in declared_methods: if Modifier.isStatic(method.getModifiers()) and Modifier.isPublic( method.getModifiers()): static_methods[method.name] = method methods = static_methods.keys() for eachBase in clazz.__bases__: methods.extend(staticMethodNames(eachBase)) return methods
def staticMethodNames(clazz): """return a list of static method name for a class""" static_methods = {} declared_methods = Class.getDeclaredMethods(clazz) for method in declared_methods: if Modifier.isStatic(method.getModifiers()) and Modifier.isPublic(method.getModifiers()): static_methods[method.name] = method methods = static_methods.keys() for eachBase in clazz.__bases__: # with Jython 2.5 type is a base of Object, which puts asName in the list # will be a problem for real Java objects that extend Python objects # see similar "fixes" in instanceMethodNames and staticFieldNames if not ispython(eachBase): methods.extend(staticMethodNames(eachBase)) return methods
def instanceMethodNames(clazz): """return a Set of instance method name for a Class""" method_names = Set() declared_methods = Class.getDeclaredMethods(clazz) for method in declared_methods: modifiers = method.getModifiers() if not Modifier.isStatic(modifiers) and Modifier.isPublic(modifiers): name = method.name method_names.add(name) if name.startswith("get") and len(name) > 3 and len(method.getParameterTypes()) == 0: property_name = name[3].lower() + name[4:] method_names.add(property_name) for eachBase in clazz.__bases__: if not ispython(eachBase): method_names = method_names | instanceMethodNames(eachBase) return method_names
'char': prim("Character"), 'byte': prim("Byte"), 'long': prim("Long"), 'double': prim("Double"), 'float': prim("Float"), 'boolean': prim("Boolean") } USELESS_TYPES = (PRIMITIVES['char'], Class.forName('[C'), PRIMITIVES['byte']) """ We want to create Jython wrappers for all public methods of PApplet except those in "BAD_METHOD". Also, if we have both foo(int) and foo(char), we throw away the char variant, and always call the int variant. Same with foo(byte). Sadly, Java has no unisgned types, so the distinction is weird. """ WANTED_METHODS = [m for m in Class.getDeclaredMethods(PApplet) if Modifier.isPublic(m.getModifiers()) and not BAD_METHOD.match(m.getName()) and not any(k in USELESS_TYPES for k in m.getParameterTypes())] """ We want to create Jython wrappers for all variables visible during the Processing runtime. """ WANTED_FIELDS = [f for f in Class.getDeclaredFields(PApplet) if Modifier.isPublic(f.getModifiers()) and not Modifier.isStatic(f.getModifiers()) and not BAD_FIELD.match(f.getName())] class ClassConversionInfo(object):
'byte': prim("Byte"), 'long': prim("Long"), 'double': prim("Double"), 'float': prim("Float"), 'boolean': prim("Boolean") } USELESS_TYPES = (PRIMITIVES['char'], Class.forName('[C'), PRIMITIVES['byte']) """ We want to create Jython wrappers for all public methods of PApplet except those in "BAD_METHOD". Also, if we have both foo(int) and foo(char), we throw away the char variant, and always call the int variant. Same with foo(byte). Sadly, Java has no unsigned types, so the distinction is weird. """ WANTED_METHODS = [ m for m in Class.getDeclaredMethods(PApplet) if Modifier.isPublic(m.getModifiers()) and not m.getExceptionTypes() and not BAD_METHOD.match(m.getName()) and not any( k in USELESS_TYPES for k in m.getParameterTypes()) ] """ We want to create Jython wrappers for all variables visible during the Processing runtime. """ WANTED_FIELDS = [ f for f in Class.getDeclaredFields(PApplet) if Modifier.isPublic(f.getModifiers()) and not Modifier.isStatic( f.getModifiers()) and not BAD_FIELD.match(f.getName()) ]