Skip to content

[ji] eliminate JavaClass usage in favor of Java wrappers#6513

Merged
kares merged 41 commits intojruby:masterfrom
kares:ji-java-class
Mar 22, 2021
Merged

[ji] eliminate JavaClass usage in favor of Java wrappers#6513
kares merged 41 commits intojruby:masterfrom
kares:ji-java-class

Conversation

@kares
Copy link
Member

@kares kares commented Dec 29, 2020

JRuby's Java integration has been using a custom JavaClass ruby class to represent a java.lang.Class instance.
However this isn't super easy to work with (personally I have always find it quite confusing given the existing complexity of proxy modules/classes) nor has been 100% consistent e.g. obj.java_class depending on the proxy type (interface impl, Java sub-class in Ruby) might have returned a proxy instead of the JavaClass instance (returned from java.lang.String.java_class).

The proposal here is to start returning java.lang.Class instance Java proxy wrappers everywhere (all java_class calls), for compatibility all of the custom JavaClass Ruby API is supported on java.lang.Class and others (there's JavaField, JavaConstructor instances JavaClass methods where returning which will be 'emulated' using java.lang.reflect.Field, java.lang.reflect.Constructor).

@kares kares added this to the JRuby 9.3.0.0 milestone Dec 29, 2020
mostly wider converting arguments (not sure it's worth it)
@headius
Copy link
Member

headius commented Dec 30, 2020

I have made quick attempts at this in the past and I support this change. Anything we can do to eliminate that legacy hierarchy of JavaClass and friends would be great!

@kares
Copy link
Member Author

kares commented Dec 31, 2020

@headius definitely - this has been very difficult to work with.
I think all in all this is quite close and existing specs/tests pass for the most part with java_class simply returning java.lang.Classinstance proxies. the only compatibility 'issue' atm is not mapping exceptions to a Ruby hierarchy.

anyhow, we will need to make a decision whether, despite no longer returning JavaClass instances, we keep the Java::JavaClass constant around for (Ruby) consumers or rather point to JavaClass = java.lang.Class in which case there's the opportunity to remove all of the JavaClass JavaMethod native implementations.

as noted the breakage currently is going to be things like JavaClass.for_name which did map Java exceptions like ClassNotFoundException into a Ruby NameError - simply can not handle that level of compatibility since it would break java.lang.Class.for_name consumers. believe we should remove JavaClass native implementations and for users seeking compatibility there could be a migration script.

@headius
Copy link
Member

headius commented Jan 5, 2021

@kares Perhaps the decorations you are adding to make Class look like JavaClass could go in a module? Class could include or prepend the module to pick up the additions.

The breakage of things like for_name will just have to be accepted, I think. These shim classes have been deprecated for a long time (though they didn't warn or anything) and I hope there are not many cases out there that expect them to exist.

will match the format Class#inspect uses for anonymous classes
deprecate (old) other variants load...Verbose and load...Quiet
with exception wrapping ~ JavaClass.for_name did
previously due JavaClass caching the alternate loading
did not trigger a full class setup (which correctly fails
since class-loader only loads the one class) leading to
an illegal access error trying to setup TestHelper's
inner classes
this behavior is no longer supported
@kares kares force-pushed the ji-java-class branch 2 times, most recently from 07326ad to e21f010 Compare March 1, 2021 07:12
@headius
Copy link
Member

headius commented Mar 17, 2021

I would love to see this land soon. Time to start wrapping up 9.3 for reals.

kares added 3 commits March 22, 2021 09:04
there's still an issue of potential double initializing a proxy class
if we start first with initializing a java.lang.Class proxy it ends up
setting up two proxy instances
not relying on getRuntime() will allow unplugging these classes
@kares kares marked this pull request as ready for review March 22, 2021 08:20
@kares kares merged commit 95a2303 into jruby:master Mar 22, 2021
robbavey added a commit to elastic/logstash that referenced this pull request May 16, 2022
Jruby commit jruby/jruby#6513 included cleanup to eliminate JavaClass usage
and will log any use of such to stdout. As part of plugin registration, a check is made against the
plugin class to determine if it meets the requirements of a Java plugin. From running tests against
native Java plugins, this check doesn't appear to be required
robbavey added a commit to elastic/logstash that referenced this pull request May 17, 2022
Jruby commit jruby/jruby#6513 included cleanup to eliminate JavaClass usage
and will log any use of such to stdout. As part of plugin registration, a check is made against the
plugin class to determine if it meets the requirements of a Java plugin. From running tests against
native Java plugins, this check doesn't appear to be required
robbavey added a commit to elastic/logstash that referenced this pull request May 18, 2022
Jruby commit jruby/jruby#6513 included cleanup to eliminate JavaClass usage
and will log any use of such to stdout. As part of plugin registration, a check is made against the
plugin class to determine if it meets the requirements of a Java plugin. From running tests against
native Java plugins, this check doesn't appear to be required
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants