-
Type:
Bug
-
Resolution: Duplicate
-
Priority:
P4
-
None
-
Affects Version/s: 8, 25
-
Component/s: core-libs
-
generic
-
generic
A DESCRIPTION OF THE PROBLEM :
If a public class implements a non-public interface that declares a default public method, this method cannot be accessed through reflection, within the public class. Attempting to access the default public method from outside the package will produce a java.lang.IllegalAccessException.
An analogous case has been reported inJDK-6342411, and to make the behaviour consistent with this case, I suggest that some kind of bridge method is added to the class file of the public class, such that the method can be accessed through reflection (as it is a public method within a public class).
Sidenote: calling the public method directly from the public class (in this case and inJDK-6342411) is fine and does not throw an exception.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Simply compiling the given code and executing the main method in ClientTest should produce the same behaviour.
If instead, in the ClientTest code, we simply use im.f(), we get the expected result.
ACTUAL -
Exception in thread "main" java.lang.IllegalAccessException: class foo.client.ClientTest cannot access a member of interface foo.DefaultInterface with modifiers "public"
at java.base/jdk.internal.reflect.Reflection.newIllegalAccessException(Reflection.java:400)
at java.base/java.lang.reflect.AccessibleObject.checkAccess(AccessibleObject.java:670)
at java.base/java.lang.reflect.Method.invoke(Method.java:556)
at foo.client.ClientTest.main(ClientTest.java:11)
---------- BEGIN SOURCE ----------
package foo;
interface DefaultInterface {
default public void f() {
System.out.println("Hi");
}
}
----------------------------------
package foo;
public class Impl implements DefaultInterface{
}
----------------------------------
package foo.client;
import java.lang.reflect.Method;
import foo.Impl;
public class ClientTest {
public static void main(String[] args) throws Exception {
Impl im = new Impl();
Method m = Impl.class.getMethod("f");
m.invoke(im);
}
}
---------- END SOURCE ----------
If a public class implements a non-public interface that declares a default public method, this method cannot be accessed through reflection, within the public class. Attempting to access the default public method from outside the package will produce a java.lang.IllegalAccessException.
An analogous case has been reported in
Sidenote: calling the public method directly from the public class (in this case and in
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Simply compiling the given code and executing the main method in ClientTest should produce the same behaviour.
If instead, in the ClientTest code, we simply use im.f(), we get the expected result.
ACTUAL -
Exception in thread "main" java.lang.IllegalAccessException: class foo.client.ClientTest cannot access a member of interface foo.DefaultInterface with modifiers "public"
at java.base/jdk.internal.reflect.Reflection.newIllegalAccessException(Reflection.java:400)
at java.base/java.lang.reflect.AccessibleObject.checkAccess(AccessibleObject.java:670)
at java.base/java.lang.reflect.Method.invoke(Method.java:556)
at foo.client.ClientTest.main(ClientTest.java:11)
---------- BEGIN SOURCE ----------
package foo;
interface DefaultInterface {
default public void f() {
System.out.println("Hi");
}
}
----------------------------------
package foo;
public class Impl implements DefaultInterface{
}
----------------------------------
package foo.client;
import java.lang.reflect.Method;
import foo.Impl;
public class ClientTest {
public static void main(String[] args) throws Exception {
Impl im = new Impl();
Method m = Impl.class.getMethod("f");
m.invoke(im);
}
}
---------- END SOURCE ----------
- duplicates
-
JDK-4283544 Field and Method cannot check accessibility against referenced class
-
- Open
-
- relates to
-
JDK-6342411 Add bridge method to allow reflective access to public method in non-public class
-
- Closed
-