2012-08-15 22:16:17 +00:00
|
|
|
__all__ = ('autoclass', 'ensureclass')
|
|
|
|
|
2012-09-06 12:19:45 +00:00
|
|
|
from jnius import (
|
|
|
|
JavaClass, MetaJavaClass, JavaMethod, JavaStaticMethod,
|
|
|
|
JavaField, JavaStaticField, JavaMultipleMethod, find_javaclass
|
|
|
|
)
|
|
|
|
|
2012-08-14 13:09:48 +00:00
|
|
|
|
|
|
|
class Class(JavaClass):
|
|
|
|
__metaclass__ = MetaJavaClass
|
|
|
|
__javaclass__ = 'java/lang/Class'
|
|
|
|
|
2015-01-03 18:56:50 +00:00
|
|
|
desiredAssertionStatus = JavaMethod('()Z')
|
2012-09-06 12:19:45 +00:00
|
|
|
forName = JavaMultipleMethod([
|
|
|
|
('(Ljava/lang/String,Z,Ljava/lang/ClassLoader;)Ljava/langClass;', True, False),
|
|
|
|
('(Ljava/lang/String;)Ljava/lang/Class;', True, False), ])
|
|
|
|
getClassLoader = JavaMethod('()Ljava/lang/ClassLoader;')
|
|
|
|
getClasses = JavaMethod('()[Ljava/lang/Class;')
|
|
|
|
getComponentType = JavaMethod('()Ljava/lang/Class;')
|
|
|
|
getConstructor = JavaMethod('([Ljava/lang/Class;)Ljava/lang/reflect/Constructor;')
|
2012-08-15 17:16:39 +00:00
|
|
|
getConstructors = JavaMethod('()[Ljava/lang/reflect/Constructor;')
|
2012-09-06 12:19:45 +00:00
|
|
|
getDeclaredClasses = JavaMethod('()[Ljava/lang/Class;')
|
|
|
|
getDeclaredConstructor = JavaMethod('([Ljava/lang/Class;)Ljava/lang/reflect/Constructor;')
|
|
|
|
getDeclaredConstructors = JavaMethod('()[Ljava/lang/reflect/Constructor;')
|
|
|
|
getDeclaredField = JavaMethod('(Ljava/lang/String;)Ljava/lang/reflect/Field;')
|
2012-08-14 13:09:48 +00:00
|
|
|
getDeclaredFields = JavaMethod('()[Ljava/lang/reflect/Field;')
|
2012-09-06 12:19:45 +00:00
|
|
|
getDeclaredMethod = JavaMethod('(Ljava/lang/String,[Ljava/lang/Class;)Ljava/lang/reflect/Method;')
|
|
|
|
getDeclaredMethods = JavaMethod('()[Ljava/lang/reflect/Method;')
|
|
|
|
getDeclaringClass = JavaMethod('()Ljava/lang/Class;')
|
|
|
|
getField = JavaMethod('(Ljava/lang/String;)Ljava/lang/reflect/Field;')
|
|
|
|
getFields = JavaMethod('()[Ljava/lang/reflect/Field;')
|
|
|
|
getInterfaces = JavaMethod('()[Ljava/lang/Class;')
|
|
|
|
getMethod = JavaMethod('(Ljava/lang/String,[Ljava/lang/Class;)Ljava/lang/reflect/Method;')
|
|
|
|
getMethods = JavaMethod('()[Ljava/lang/reflect/Method;')
|
2015-01-03 18:56:50 +00:00
|
|
|
getModifiers = JavaMethod('()[I')
|
2012-08-14 13:09:48 +00:00
|
|
|
getName = JavaMethod('()Ljava/lang/String;')
|
2012-09-06 12:19:45 +00:00
|
|
|
getPackage = JavaMethod('()Ljava/lang/Package;')
|
|
|
|
getProtectionDomain = JavaMethod('()Ljava/security/ProtectionDomain;')
|
|
|
|
getResource = JavaMethod('(Ljava/lang/String;)Ljava/net/URL;')
|
|
|
|
getResourceAsStream = JavaMethod('(Ljava/lang/String;)Ljava/io/InputStream;')
|
|
|
|
getSigners = JavaMethod('()[Ljava/lang/Object;')
|
|
|
|
getSuperclass = JavaMethod('()Ljava/lang/reflect/Class;')
|
2015-01-03 18:56:50 +00:00
|
|
|
isArray = JavaMethod('()Z')
|
|
|
|
isAssignableFrom = JavaMethod('(Ljava/lang/reflect/Class;)Z')
|
|
|
|
isInstance = JavaMethod('(Ljava/lang/Object;)Z')
|
|
|
|
isInterface = JavaMethod('()Z')
|
|
|
|
isPrimitive = JavaMethod('()Z')
|
2012-09-06 12:19:45 +00:00
|
|
|
newInstance = JavaMethod('()Ljava/lang/Object;')
|
|
|
|
toString = JavaMethod('()Ljava/lang/String;')
|
|
|
|
|
2012-08-14 13:09:48 +00:00
|
|
|
|
|
|
|
class Object(JavaClass):
|
|
|
|
__metaclass__ = MetaJavaClass
|
|
|
|
__javaclass__ = 'java/lang/Object'
|
|
|
|
|
|
|
|
getClass = JavaMethod('()Ljava/lang/Class;')
|
2013-06-25 09:49:42 +00:00
|
|
|
hashCode = JavaMethod('()I')
|
2012-08-14 13:09:48 +00:00
|
|
|
|
2012-09-06 12:19:45 +00:00
|
|
|
|
2012-08-14 13:09:48 +00:00
|
|
|
class Modifier(JavaClass):
|
|
|
|
__metaclass__ = MetaJavaClass
|
|
|
|
__javaclass__ = 'java/lang/reflect/Modifier'
|
|
|
|
|
|
|
|
isAbstract = JavaStaticMethod('(I)Z')
|
|
|
|
isFinal = JavaStaticMethod('(I)Z')
|
|
|
|
isInterface = JavaStaticMethod('(I)Z')
|
|
|
|
isNative = JavaStaticMethod('(I)Z')
|
|
|
|
isPrivate = JavaStaticMethod('(I)Z')
|
|
|
|
isProtected = JavaStaticMethod('(I)Z')
|
|
|
|
isPublic = JavaStaticMethod('(I)Z')
|
|
|
|
isStatic = JavaStaticMethod('(I)Z')
|
|
|
|
isStrict = JavaStaticMethod('(I)Z')
|
|
|
|
isSynchronized = JavaStaticMethod('(I)Z')
|
|
|
|
isTransient = JavaStaticMethod('(I)Z')
|
|
|
|
isVolatile = JavaStaticMethod('(I)Z')
|
|
|
|
|
2012-09-06 12:19:45 +00:00
|
|
|
|
2012-08-14 13:09:48 +00:00
|
|
|
class Method(JavaClass):
|
|
|
|
__metaclass__ = MetaJavaClass
|
|
|
|
__javaclass__ = 'java/lang/reflect/Method'
|
|
|
|
|
|
|
|
getName = JavaMethod('()Ljava/lang/String;')
|
|
|
|
toString = JavaMethod('()Ljava/lang/String;')
|
|
|
|
getParameterTypes = JavaMethod('()[Ljava/lang/Class;')
|
|
|
|
getReturnType = JavaMethod('()Ljava/lang/Class;')
|
|
|
|
getModifiers = JavaMethod('()I')
|
2012-08-26 00:51:22 +00:00
|
|
|
isVarArgs = JavaMethod('()Z')
|
2012-08-14 13:09:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Field(JavaClass):
|
|
|
|
__metaclass__ = MetaJavaClass
|
|
|
|
__javaclass__ = 'java/lang/reflect/Field'
|
|
|
|
|
|
|
|
getName = JavaMethod('()Ljava/lang/String;')
|
|
|
|
toString = JavaMethod('()Ljava/lang/String;')
|
|
|
|
getType = JavaMethod('()Ljava/lang/Class;')
|
|
|
|
getModifiers = JavaMethod('()I')
|
|
|
|
|
2012-09-06 12:19:45 +00:00
|
|
|
|
2012-08-15 17:16:39 +00:00
|
|
|
class Constructor(JavaClass):
|
|
|
|
__metaclass__ = MetaJavaClass
|
|
|
|
__javaclass__ = 'java/lang/reflect/Constructor'
|
|
|
|
|
|
|
|
toString = JavaMethod('()Ljava/lang/String;')
|
|
|
|
getParameterTypes = JavaMethod('()[Ljava/lang/Class;')
|
|
|
|
getModifiers = JavaMethod('()I')
|
2012-08-26 00:51:22 +00:00
|
|
|
isVarArgs = JavaMethod('()Z')
|
2012-08-14 13:09:48 +00:00
|
|
|
|
2012-09-06 12:19:45 +00:00
|
|
|
|
2012-08-14 13:09:48 +00:00
|
|
|
def get_signature(cls_tp):
|
|
|
|
tp = cls_tp.getName()
|
|
|
|
if tp[0] == '[':
|
|
|
|
return tp.replace('.', '/')
|
2012-09-06 12:19:45 +00:00
|
|
|
signatures = {
|
|
|
|
'void': 'V', 'boolean': 'Z', 'byte': 'B',
|
2012-08-14 13:09:48 +00:00
|
|
|
'char': 'C', 'short': 'S', 'int': 'I',
|
|
|
|
'long': 'J', 'float': 'F', 'double': 'D'}
|
|
|
|
ret = signatures.get(tp)
|
|
|
|
if ret:
|
|
|
|
return ret
|
2012-08-14 15:21:55 +00:00
|
|
|
# don't do it in recursive way for the moment,
|
|
|
|
# error on the JNI/android: JNI ERROR (app bug): local reference table overflow (max=512)
|
|
|
|
#
|
|
|
|
#ensureclass(tp)
|
2012-08-14 13:09:48 +00:00
|
|
|
return 'L{0};'.format(tp.replace('.', '/'))
|
|
|
|
|
|
|
|
|
|
|
|
registers = []
|
|
|
|
|
2012-09-06 12:19:45 +00:00
|
|
|
|
2012-08-14 13:09:48 +00:00
|
|
|
def ensureclass(clsname):
|
|
|
|
if clsname in registers:
|
|
|
|
return
|
|
|
|
jniname = clsname.replace('.', '/')
|
|
|
|
if MetaJavaClass.get_javaclass(jniname):
|
|
|
|
return
|
|
|
|
registers.append(clsname)
|
|
|
|
autoclass(clsname)
|
|
|
|
|
2015-04-30 20:13:16 +00:00
|
|
|
def lower_name(s):
|
|
|
|
return s[:1].lower() + s[1:] if s else ''
|
|
|
|
|
|
|
|
def bean_getter(s):
|
|
|
|
return (s.startswith('get') and len(s) > 3 and s[3].isupper()) or (s.startswith('is') and len(s) > 2 and s[2].isupper())
|
2012-08-14 13:09:48 +00:00
|
|
|
|
|
|
|
def autoclass(clsname):
|
|
|
|
jniname = clsname.replace('.', '/')
|
|
|
|
cls = MetaJavaClass.get_javaclass(jniname)
|
2012-08-26 00:51:22 +00:00
|
|
|
if cls:
|
|
|
|
return cls
|
2012-08-14 13:09:48 +00:00
|
|
|
|
|
|
|
classDict = {}
|
|
|
|
|
2012-08-18 10:59:15 +00:00
|
|
|
#c = Class.forName(clsname)
|
|
|
|
c = find_javaclass(clsname)
|
2012-08-14 13:09:48 +00:00
|
|
|
if c is None:
|
|
|
|
raise Exception('Java class {0} not found'.format(c))
|
|
|
|
return None
|
|
|
|
|
2012-08-15 17:16:39 +00:00
|
|
|
constructors = []
|
|
|
|
for constructor in c.getConstructors():
|
|
|
|
sig = '({0})V'.format(
|
|
|
|
''.join([get_signature(x) for x in constructor.getParameterTypes()]))
|
2012-08-26 00:51:22 +00:00
|
|
|
constructors.append((sig, constructor.isVarArgs()))
|
2012-08-15 17:16:39 +00:00
|
|
|
classDict['__javaconstructor__'] = constructors
|
|
|
|
|
2012-08-18 17:32:15 +00:00
|
|
|
methods = c.getMethods()
|
2012-08-15 17:16:39 +00:00
|
|
|
methods_name = [x.getName() for x in methods]
|
|
|
|
for index, method in enumerate(methods):
|
|
|
|
name = methods_name[index]
|
|
|
|
if name in classDict:
|
|
|
|
continue
|
|
|
|
count = methods_name.count(name)
|
|
|
|
|
|
|
|
# only one method available
|
|
|
|
if count == 1:
|
|
|
|
static = Modifier.isStatic(method.getModifiers())
|
2012-08-26 00:51:22 +00:00
|
|
|
varargs = method.isVarArgs()
|
2012-08-15 17:16:39 +00:00
|
|
|
sig = '({0}){1}'.format(
|
|
|
|
''.join([get_signature(x) for x in method.getParameterTypes()]),
|
|
|
|
get_signature(method.getReturnType()))
|
|
|
|
cls = JavaStaticMethod if static else JavaMethod
|
2012-08-26 00:51:22 +00:00
|
|
|
classDict[name] = cls(sig, varargs=varargs)
|
2015-04-30 20:13:16 +00:00
|
|
|
if name != 'getClass' and bean_getter(name) and len(method.getParameterTypes()) == 0:
|
|
|
|
lowername = lower_name(name[3:])
|
|
|
|
classDict[lowername] = (lambda n: property(lambda self: getattr(self, n)()))(name)
|
2012-08-15 17:16:39 +00:00
|
|
|
continue
|
|
|
|
|
2015-04-30 20:13:16 +00:00
|
|
|
# multiple signatures
|
2012-08-15 17:16:39 +00:00
|
|
|
signatures = []
|
|
|
|
for index, subname in enumerate(methods_name):
|
|
|
|
if subname != name:
|
|
|
|
continue
|
|
|
|
method = methods[index]
|
|
|
|
sig = '({0}){1}'.format(
|
|
|
|
''.join([get_signature(x) for x in method.getParameterTypes()]),
|
|
|
|
get_signature(method.getReturnType()))
|
|
|
|
'''
|
|
|
|
print 'm', name, sig, method.getModifiers()
|
|
|
|
m = method.getModifiers()
|
|
|
|
print 'Public', Modifier.isPublic(m)
|
|
|
|
print 'Private', Modifier.isPrivate(m)
|
|
|
|
print 'Protected', Modifier.isProtected(m)
|
|
|
|
print 'Static', Modifier.isStatic(m)
|
|
|
|
print 'Final', Modifier.isFinal(m)
|
|
|
|
print 'Synchronized', Modifier.isSynchronized(m)
|
|
|
|
print 'Volatile', Modifier.isVolatile(m)
|
|
|
|
print 'Transient', Modifier.isTransient(m)
|
|
|
|
print 'Native', Modifier.isNative(m)
|
|
|
|
print 'Interface', Modifier.isInterface(m)
|
|
|
|
print 'Abstract', Modifier.isAbstract(m)
|
|
|
|
print 'Strict', Modifier.isStrict(m)
|
|
|
|
'''
|
2012-08-26 00:51:22 +00:00
|
|
|
signatures.append((sig, Modifier.isStatic(method.getModifiers()), method.isVarArgs()))
|
2012-08-15 17:16:39 +00:00
|
|
|
|
2012-08-20 09:08:32 +00:00
|
|
|
classDict[name] = JavaMultipleMethod(signatures)
|
2012-08-14 13:09:48 +00:00
|
|
|
|
2015-04-30 20:13:16 +00:00
|
|
|
for iclass in c.getInterfaces():
|
|
|
|
if iclass.getName() == 'java.util.List':
|
|
|
|
classDict['__getitem__'] = lambda self, index: self.get(index)
|
|
|
|
classDict['__len__'] = lambda self: self.size()
|
|
|
|
break
|
|
|
|
|
2012-08-18 17:32:15 +00:00
|
|
|
for field in c.getFields():
|
2012-08-14 13:09:48 +00:00
|
|
|
static = Modifier.isStatic(field.getModifiers())
|
|
|
|
sig = get_signature(field.getType())
|
|
|
|
cls = JavaStaticField if static else JavaField
|
|
|
|
classDict[field.getName()] = cls(sig)
|
|
|
|
|
|
|
|
classDict['__javaclass__'] = clsname.replace('.', '/')
|
|
|
|
|
2012-09-06 12:19:45 +00:00
|
|
|
return MetaJavaClass.__new__(
|
|
|
|
MetaJavaClass,
|
|
|
|
clsname, # .replace('.', '_'),
|
|
|
|
(JavaClass, ),
|
|
|
|
classDict)
|