From: <cg...@us...> - 2009-01-01 00:20:01
|
Revision: 5824 http://jython.svn.sourceforge.net/jython/?rev=5824&view=rev Author: cgroves Date: 2009-01-01 00:19:56 +0000 (Thu, 01 Jan 2009) Log Message: ----------- Welcome back, Options.respectJavaAcessibility Modified Paths: -------------- trunk/jython/Lib/test/test_java_visibility.py trunk/jython/src/org/python/core/PyJavaType.java Added Paths: ----------- trunk/jython/Lib/test/access_protected_class.py trunk/jython/Lib/test/access_protected_field.py trunk/jython/Lib/test/call_protected_method.py Added: trunk/jython/Lib/test/access_protected_class.py =================================================================== --- trunk/jython/Lib/test/access_protected_class.py (rev 0) +++ trunk/jython/Lib/test/access_protected_class.py 2009-01-01 00:19:56 UTC (rev 5824) @@ -0,0 +1,2 @@ +from java.awt.geom import Ellipse2D, EllipseIterator +EllipseIterator(Ellipse2D.Float(), None) Added: trunk/jython/Lib/test/access_protected_field.py =================================================================== --- trunk/jython/Lib/test/access_protected_field.py (rev 0) +++ trunk/jython/Lib/test/access_protected_field.py 2009-01-01 00:19:56 UTC (rev 5824) @@ -0,0 +1,4 @@ +from org.python.tests import Invisible +Invisible.protectedStaticField +Invisible().protectedField +Invisible.packageField Added: trunk/jython/Lib/test/call_protected_method.py =================================================================== --- trunk/jython/Lib/test/call_protected_method.py (rev 0) +++ trunk/jython/Lib/test/call_protected_method.py 2009-01-01 00:19:56 UTC (rev 5824) @@ -0,0 +1,5 @@ +from org.python.tests import Invisible +Invisible.protectedStaticMethod(7) +Invisible().protectedMethod(7) +Invisible.packageStaticMethod() +Invisible().packageMethod() Modified: trunk/jython/Lib/test/test_java_visibility.py =================================================================== --- trunk/jython/Lib/test/test_java_visibility.py 2008-12-31 21:57:35 UTC (rev 5823) +++ trunk/jython/Lib/test/test_java_visibility.py 2009-01-01 00:19:56 UTC (rev 5824) @@ -1,5 +1,7 @@ import array import unittest +import subprocess +import sys from test import test_support from java.lang import Class from java.util import HashMap, Observable, Observer @@ -165,10 +167,28 @@ self.assertEquals("OtherSubVisible[]", c.takeArray([OtherSubVisible()])) self.assertEquals("SubVisible[]", c.takeArray([SubVisible()])) +class RespectJavaAccessibilityTest(unittest.TestCase): + def run_accessibility_script(self, script, error=AttributeError): + fn = test_support.findfile(script) + self.assertRaises(error, execfile, fn) + self.assertEquals(subprocess.call([sys.executable, "-J-Dpython.cachedir.skip=true", + "-J-Dpython.security.respectJavaAccessibility=false", fn]), + 0) + + def test_method_access(self): + self.run_accessibility_script("call_protected_method.py") + + def test_field_access(self): + self.run_accessibility_script("access_protected_field.py") + + def test_protected_class(self): + self.run_accessibility_script("access_protected_class.py", TypeError) + def test_main(): test_support.run_unittest(VisibilityTest, JavaClassTest, - CoercionTest) + CoercionTest, + RespectJavaAccessibilityTest) if __name__ == "__main__": test_main() Modified: trunk/jython/src/org/python/core/PyJavaType.java =================================================================== --- trunk/jython/src/org/python/core/PyJavaType.java 2008-12-31 21:57:35 UTC (rev 5823) +++ trunk/jython/src/org/python/core/PyJavaType.java 2009-01-01 00:19:56 UTC (rev 5824) @@ -102,7 +102,7 @@ // PyReflected* can't call or access anything from non-public classes that aren't in // org.python.core if (!Modifier.isPublic(underlying_class.getModifiers()) && - !name.startsWith("org.python.core")) { + !name.startsWith("org.python.core") && Options.respectJavaAccessibility) { handleSuperMethodArgCollisions(); return; } @@ -110,7 +110,17 @@ // Add methods and determine bean properties declared on this class Map<String, PyBeanProperty> props = Generic.map(); Map<String, PyBeanEvent> events = Generic.map(); - for (Method meth : underlying_class.getMethods()) { + Method[] methods; + if (Options.respectJavaAccessibility) { + // returns just the public methods + methods = underlying_class.getMethods(); + } else { + methods = underlying_class.getDeclaredMethods(); + for (Method method : methods) { + method.setAccessible(true); + } + } + for (Method meth : methods) { if (!declaredOnMember(baseClass, meth) || ignore(meth)) { continue; } @@ -174,7 +184,7 @@ } // Add superclass methods - for (Method meth : underlying_class.getMethods()) { + for (Method meth : methods) { String nmethname = normalize(meth.getName()); PyReflectedFunction reflfunc = (PyReflectedFunction)dict.__finditem__(nmethname); if (reflfunc != null) { @@ -192,7 +202,17 @@ } // Add fields declared on this type - for (Field field : underlying_class.getFields()) { + Field[] fields; + if (Options.respectJavaAccessibility) { + // returns just the public fields + fields = underlying_class.getFields(); + } else { + fields = underlying_class.getDeclaredFields(); + for (Field field : fields) { + field.setAccessible(true); + } + } + for (Field field : fields) { if (!declaredOnMember(baseClass, field)) { continue; } @@ -258,7 +278,19 @@ } final PyReflectedConstructor reflctr = new PyReflectedConstructor("_new_impl"); - for (Constructor<?> ctr : underlying_class.getConstructors()) { + Constructor<?>[] constructors; + // No matter the security manager, trying to set the constructor on class to accessible + // blows up + if (Options.respectJavaAccessibility || Class.class == underlying_class) { + // returns just the public constructors + constructors = underlying_class.getConstructors(); + } else { + constructors = underlying_class.getDeclaredConstructors(); + for (Constructor<?> ctr : constructors) { + ctr.setAccessible(true); + } + } + for (Constructor<?> ctr : constructors) { reflctr.addConstructor(ctr); } if (PyObject.class.isAssignableFrom(underlying_class)) { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |