Skip to content

Can't call methods of private subclasses with java_method in java 12 #5841

Closed

Description

@yamam

Environment

jruby 9.2.8.0 (2.5.3) 2019-08-12 a1ac7ff OpenJDK 64-Bit Server VM 12.0.2+10 on 12.0.2+10 +jit [mswin32-x86_64]

JavaTest.java

public class JavaTest {

    public interface PublicInterface {
        public void publicMethod(Integer a);
        public void publicMethod(Long a);
    }

    public PublicInterface publicMember = new PrivateSubclass();

    private class PrivateSubclass implements PublicInterface {
        public void publicMethod(Integer a) {System.out.println("publicMethod(Integer)");}
        public void publicMethod(Long a) {System.out.println("publicMethod(Long)");}
    }
}

test.rb

java_import 'JavaTest'
JavaTest.new.publicMember.java_method(:publicMethod, [java.lang.Integer.java_class]).call(1)

Expected Behavior

You can call methods of private subclasses with java_method in java 8.

$ javac JavaTest.java
$ jruby test.rb
publicMethod(Integer)

Actual Behavior

You can't call methods of private subclasses with java_method in java 12.

$ jruby test.rb
2019-08-20T09:36:29.010+09:00 [main] WARN FilenoUtil : Native subprocess control requires open access to sun.nio.ch
Pass '--add-opens java.base/sun.nio.ch=org.jruby.dist' or '=org.jruby.core' to enable.
TypeError: illegal access on 'publicMethod': class org.jruby.javasupport.JavaMethod (in module org.jruby.dist) cannot access a member of class JavaTest$PrivateSubclass with modifiers "public"
    call at org/jruby/RubyMethod.java:120
  <main> at test.rb:2

Activity

  1. headius commented on Aug 20, 2019

    @headius
    Member

    Looks like this is a problem with how we determine whether we should set methods accessible.

    The logic here basically turns off setAccessible when on Java 9 or higher:

    private static boolean calculateSetAccessibleDefault() {

    I'm going to look at changing this property back to always true and modify the logic that uses it to use our trySetAccessible logic.

  2. headius commented on Aug 20, 2019

    @headius
    Member

    It looks like all places that use the JI_SETACCESSIBLE option (with the default "false" on Java 9+) are already using our safe trySetAccessible that's aware of modules. Changing this calculated default to "true" appears to work fine.

    I'm going to push it to a PR to make sure it doesn't affect too much other code.

  3. added a commit that references this issue on Aug 21, 2019
    02c5040
  4. added this to the JRuby 9.2.9.0 milestone on Aug 21, 2019
  5. added a commit that references this issue on Aug 25, 2019
    00bc57a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions