Gubaer / josm-scripting-plugin

Task automation in the OpenStreetMap editor JOSM
https://gubaer.github.io/josm-scripting-plugin
GNU General Public License v3.0
26 stars 9 forks source link

`IllegalAccessException` on JDK ≥16 #95

Closed AndrewKvalheim closed 2 years ago

AndrewKvalheim commented 2 years ago

With plugin version 30798 in JOSM 18387 on JDK 11, using the clipboard API (as in the example) causes a warning:

WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.mozilla.javascript.MemberBox (file:/home/user/.local/share/JOSM/plugins/scripting.jar) to method sun.awt.X11.XToolkit.getSystemClipboard()
WARNING: Please consider reporting this to the maintainers of org.mozilla.javascript.MemberBox
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release

On JDK 17, strong encapsulation is enabled by default so it now fails:

org.mozilla.javascript.WrappedException: Wrapped java.lang.IllegalAccessException: class org.mozilla.javascript.MemberBox cannot access class sun.awt.X11.XToolkit (in module java.desktop) because module java.desktop does not export sun.awt.X11 to unnamed module @65c2e8e8 (jar:file:/home/user/.local/share/JOSM/plugins/scripting.jar!/js/clipboard.js#53)
    at org.mozilla.javascript.Context.throwAsScriptRuntimeEx(Context.java:1893)
    at org.mozilla.javascript.MemberBox.invoke(MemberBox.java:134)

I’ve found that starting JOSM with the following Java options allows it to work—

—but I’m not sure what an appropriate solution would look like, e.g. using some other API internally, adding those permissions via some kind of plugin metadata or initialization step, adding them in JOSM, just documenting that they’re necessary, contacting the maintainers of org.mozilla.javascript.MemberBox as instructed above, updating to a newer version of that library, etc.

Gubaer commented 2 years ago

I'm currently working on a new version of the scripting plugin, which will replace Rhino with GraalJS.

Unfortunately, the version with GraalJS will be less convenient than the current version. Due to class loading issues, I can't ship GraalJS as part of the plugin jar. In the future, users will have to start JOSM with GraalJS already on the classpath. They will either have to use the GraalVM java executable or java from another JDK, which is started with the GraalJS .jar dependencies on the classpath.

Gubaer commented 2 years ago

I today released v0.2.0 of the plugin. It's the first release to support GraalJS as scripting engine. Mozilla Rhino is now deprecated and I plan to remove it together with the JavaScript API V1 end of 2022.

Clipboard access seems to work with GraalJS and the JavaScript API V2. Here's an example:

image

AndrewKvalheim commented 2 years ago

Amazing, thank you.