Last active
February 1, 2022 01:54
-
-
Save byteit101/0c72c4b57212dbd778b6a6e41f0389f4 to your computer and use it in GitHub Desktop.
JRuby Class loader proxy
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
# FXML requires loading both ruby and java classes. | |
# JRuby has no such single classloader builtin, so we proxy them all | |
# This is a minimal classloader only for classes, resources not supported | |
class PolyglotClassLoader < java.lang.ClassLoader | |
def initialize() | |
super(JRuby.runtime.jruby_class_loader) | |
end | |
java_signature "java.lang.Class findClass(java.lang.String name)" | |
def findClass(a) | |
# TODO: full Ruby modules? | |
begin # TODO: become_java! idempotent? | |
return Object.const_get(a.split(".").last).tap{|x| x.become_java!}.java_class | |
rescue NameError | |
raise java.lang.ClassNotFoundException.new("Could not find ruby class #{a}!") # Must be a java CNF, not a Ruby Name Error | |
end | |
end | |
become_java! | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment