Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reflect class (#534) #539

Open
wants to merge 21 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions jnius/jnius_conversion.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,14 @@ cdef convert_jobject_to_python(JNIEnv *j_env, definition, jobject j_object):
from .reflect import Object
ret_jc = Object(noinstance=True)
else:
from .reflect import autoclass
ret_jc = autoclass(r.replace('/', '.'))(noinstance=True)
from .reflect import autoclass, reflect_class
#ret_jc = autoclass(r.replace('/', '.'))(noinstance=True)
tshirtman marked this conversation as resolved.
Show resolved Hide resolved
c = find_javaclass(r)
if c is None:
# The class may have come from another ClassLoader
# we need to get that ClassLoader
raise JavaException("could not find %s in default ClassLoader" % r)
ret_jc = reflect_class(c)(noinstance=True)
else:
ret_jc = jclass_register[r](noinstance=True)
ret_jc.instanciate_from(create_local_ref(j_env, j_object))
Expand Down
17 changes: 13 additions & 4 deletions jnius/reflect.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
JavaException
)

__all__ = ('autoclass', 'ensureclass', 'protocol_map')
__all__ = ('autoclass', 'ensureclass', 'protocol_map', 'reflect_class')

log = getLogger(__name__)

Expand All @@ -25,6 +25,7 @@ class Class(with_metaclass(MetaJavaClass, JavaClass)):
('(Ljava/lang/String,Z,Ljava/lang/ClassLoader;)Ljava/langClass;', True, False),
('(Ljava/lang/String;)Ljava/lang/Class;', True, False), ])
getClassLoader = JavaMethod('()Ljava/lang/ClassLoader;')
getClass = JavaMethod('()Ljava/lang/Class;')
getClasses = JavaMethod('()[Ljava/lang/Class;')
getComponentType = JavaMethod('()Ljava/lang/Class;')
getConstructor = JavaMethod('([Ljava/lang/Class;)Ljava/lang/reflect/Constructor;')
Expand Down Expand Up @@ -55,6 +56,7 @@ class Class(with_metaclass(MetaJavaClass, JavaClass)):
isInstance = JavaMethod('(Ljava/lang/Object;)Z')
isInterface = JavaMethod('()Z')
isPrimitive = JavaMethod('()Z')
hashCode = JavaMethod('()I')
newInstance = JavaMethod('()Ljava/lang/Object;')
toString = JavaMethod('()Ljava/lang/String;')

Expand Down Expand Up @@ -222,15 +224,22 @@ def autoclass(clsname, include_protected=True, include_private=True):
if cls:
return cls

classDict = {}
cls_start_packagename = '.'.join(clsname.split('.')[:-1])

# c = Class.forName(clsname)
c = find_javaclass(clsname)
if c is None:
raise Exception('Java class {0} not found'.format(c))
return None

return reflect_class(c, include_protected, include_private)


# NOTE: See also comments on autoclass()
def reflect_class(c, include_protected=True, include_private=True):
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

better call it cls.
Also a docstring would be nice.


clsname = c.getName()
classDict = {}
cls_start_packagename = '.'.join(clsname.split('.')[:-1])

classDict['_class'] = c

constructors = []
Expand Down
22 changes: 22 additions & 0 deletions tests/test_reflect_class.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@


import unittest
from jnius.reflect import autoclass, reflect_class

class ReflectTest(unittest.TestCase):

def test_reflect_class(self):
cls_loader = autoclass("java.lang.ClassLoader").getSystemClassLoader()
# choose an obscure class that jnius hasnt seen before during unit tests
cls = cls_loader.loadClass("java.util.zip.CRC32")
tshirtman marked this conversation as resolved.
Show resolved Hide resolved
# we get a Class object
self.assertEqual("java.lang.Class", cls.getClass().getName())
# which represents CRC32
self.assertEqual("java.util.zip.CRC32", cls.getName())
# lets make that into a python obj representing the class
pyclass = reflect_class(cls)
# check it refers to the same thing
self.assertEqual("java/util/zip/CRC32", pyclass.__javaclass__)
# check we can instantiate it
instance = pyclass()
self.assertIsNotNone(instance)