-
Notifications
You must be signed in to change notification settings - Fork 255
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
tshirtman
wants to merge
21
commits into
kivy:master
Choose a base branch
from
cmacdonald:reflect_class_issue534
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Reflect class (#534) #539
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
425d09d
split autoclass
cmacdonald a0c855f
test case for a class already in the JVM but not loaded
cmacdonald b047a85
problematic code
cmacdonald 130f6a6
check test works as expected, skip if not
cmacdonald 50f2850
fix jclass_register access
cmacdonald a858692
add docstrings; rename c to cls_object
cmacdonald e7f0ade
added full test case
cmacdonald b0fa4eb
parameterize the default metajavaclass params
hx2A 681aa82
change param variables to _DEFAULT_INCLUDE_PROTECTED, _PRIVATE
hx2A 12ec3e7
add unittest
hx2A 7a3db98
minor change to unittests
hx2A d681440
Merge remote-tracking branch 'h2xa/fix_20200526' into reflect_class_i…
cmacdonald 54db994
more comments in test case
cmacdonald c5970e7
use _DEFAULT_INCLUDE_PROTECTED
cmacdonald 103c6e5
resort to using object.getClass()
cmacdonald 6fba0d9
use _class attribute in classDict if present.
cmacdonald 8f49008
Apply feedback to jnius/reflect.py
cmacdonald 413b6f9
addressed review feedback
cmacdonald 5ff37f1
make error handling optional
cmacdonald 17e264c
addresses feedback: using JNI calls and not catching exception on fin…
cmacdonald 4ef1593
addressed some regression on this unit test. refactored reflect_class…
cmacdonald File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
|
||
|
||
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_name = "java.util.zip.CRC32" | ||
from jnius import MetaJavaClass | ||
if MetaJavaClass.get_javaclass(cls_name) is not None: | ||
self.skipTest("%s already loaded - has this test run more than once?" % cls_name) | ||
|
||
cls = cls_loader.loadClass(cls_name) | ||
# we get a Class object | ||
self.assertEqual("java.lang.Class", cls.getClass().getName()) | ||
# which represents CRC32 | ||
self.assertEqual(cls_name, 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(cls_name.replace('.', '/'), pyclass.__javaclass__) | ||
# check we can instantiate it | ||
instance = pyclass() | ||
self.assertIsNotNone(instance) | ||
|
||
|
||
def test_dynamic_jar(self): | ||
# the idea behind this test is to: | ||
# 1. load an external jar file using an additional ClassLoader | ||
# 2. check we can instantate the Class instance | ||
# 3. check we can reflect the Class instance | ||
# 4. check we can call a method that returns an object that can only be accessed using the additional ClassLoader | ||
jar_url = "https://repo1.maven.org/maven2/commons-io/commons-io/2.6/commons-io-2.6.jar" | ||
url = autoclass("java.net.URL")(jar_url) | ||
sys_cls_loader = autoclass("java.lang.ClassLoader").getSystemClassLoader() | ||
new_cls_loader = autoclass("java.net.URLClassLoader").newInstance([url], sys_cls_loader) | ||
cls_object = new_cls_loader.loadClass("org.apache.commons.io.IOUtils") | ||
self.assertIsNotNone(cls_object) | ||
io_utils = reflect_class(cls_object) | ||
self.assertIsNotNone(io_utils) | ||
|
||
stringreader = autoclass("java.io.StringReader")("test1\ntest2") | ||
#lineIterator returns an object of class LineIterator - here we check that jnius can reflect that, despite not being in the boot classpath | ||
lineiter = io_utils.lineIterator(stringreader) | ||
self.assertEqual("test1", lineiter.next()) | ||
self.assertEqual("test2", lineiter.next()) | ||
|
||
# Equivalent Java code: | ||
# var new_cls_loader = java.net.URLClassLoader.newInstance(new java.net.URL[] {new java.net.URL("https://repo1.maven.org/maven2/commons-io/commons-io/2.6/commons-io-2.6.jar")}, ClassLoader.getSystemClassLoader()) | ||
# var cls = new_cls_loader.loadClass("org.apache.commons.io.IOUtils") | ||
# var sr = new java.io.StringReader("test1\ntest2") | ||
# var m = $2.getMethod("lineIterator", Reader.class) | ||
# m.invoke(null, (Object) sr) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that's pretty cool :)