Clojure

Reflection on internal classes fails under Java 9

Details

  • Type: Defect Defect
  • Status: Open Open
  • Priority: Major Major
  • Resolution: Unresolved
  • Affects Version/s: Release 1.8
  • Fix Version/s: None
  • Component/s: None
  • Approval:
    Triaged

Description

With the module system (jigsaw) as it is currently implemented in Java 9 early access builds (9-ea+144), calling a method via reflection is only allowed if the Method was retrieved for a class/interface in a package that is exported by its containing module. Reflector.java currently uses only target.getClass() to locate the Method, so reflective method invocation on a non-exported class will fail even if the method is provided by an exported parent interface or superclass.

The current workaround is to export the package to the unnamed module (where an application that doesn't explicitly use the module system runs) when invoking java/javac:

java --add-exports=java.xml/com.sun.xml.internal.stream=ALL-UNNAMED --add-exports=java.xml/com.sun.xml.internal.stream.writers=ALL-UNNAMED --add-exports=java.xml/com.sun.org.apache.xerces.internal.impl=ALL-UNNAMED ...

It's possible that this will be addressed in jigsaw before the release of Java 9. If not, Reflector.java could be modified to walk the ancestor chain if the initial invocation fails. Note that even with that change, accessing methods that are only defined on the non-exported class (i.e. methods that don't override a method from an exported superclass/interface) will require an --add-exports option.

For more details, see https://groups.google.com/d/msg/clojure-dev/Tp_WuEEcdWI/LMMQVAUYBwAJ

Activity

Hide
Toby Crawley added a comment -
Show
Toby Crawley added a comment - This is the root cause of http://dev.clojure.org/jira/browse/DXML-32
Alex Miller made changes -
Field Original Value New Value
Approval Triaged [ 10120 ]
Affects Version/s Release 1.8 [ 10254 ]
Labels interop
Alex Miller made changes -
Labels interop interop reflection
Hide
Toby Crawley added a comment -

I don't see any indication that Java 9 will change in our favor, so I'm attaching a patch to fix this in Clojure itself.

Show
Toby Crawley added a comment - I don't see any indication that Java 9 will change in our favor, so I'm attaching a patch to fix this in Clojure itself.
Toby Crawley made changes -
Attachment tcrawley.CLJ-2066.2017-02-14.patch [ 16436 ]

People

Vote (1)
Watch (6)

Dates

  • Created:
    Updated: