-
Bug
-
Resolution: Not an Issue
-
P3
-
9
-
None
MethodHandles.Lookup.findVirtual() works fine for class's private method
import java.lang.invoke.MethodHandles;
import java.lang.invoke.*;
import java.lang.invoke.MethodType;
public class PrivateClassMethodLookupTest{
public static void main(String[] args) throws Throwable{
new PrivateClassMethodLookupTest().m();
MethodHandle mh = MethodHandles.lookup()
.findVirtual(PrivateClassMethodLookupTest.class, "m", MethodType.methodType(void.class));
mh.invoke(new PrivateClassMethodLookupTest());
}
private void m() { System.out.println("in m");}
}
(working fine)
while
MethodHandles.Lookup.findVirtual() throws IllegalAccessException if the method MH is being created for is private and declared in an interface. With in interface methods have full privilege then why can't use findVirtual.
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
interface PrivateInterfaceMethodLookupTest {
default void boot() throws Throwable {
System.out.println("main");
m();
MethodHandles.Lookup lookup = MethodHandles.lookup();
MethodHandle mh= lookup
.findVirtual(PrivateInterfaceMethodLookupTest.class, "m", MethodType.methodType(void.class));
// mh.invokeExact(this);
}
private void m() { System.out.println("in m");
}
}
public class TestIm implements PrivateInterfaceMethodLookupTest{
public static void main(String args[]) throws Throwable
{
TestIm a =new TestIm();
a.boot();
}
}
If above behavior is correct then while accessing interface's private method with MethodHandles.lookup().unreflect() should also throw the same exception.
public interface Unreflect {
public static void main(String[] args) throws Throwable {
Method pm = Unreflect.class.getDeclaredMethod("m");
// pm.setAccessible(true);
MethodHandle pmh = MethodHandles.lookup().unreflect(pm);
// pmh.invoke(new Unreflect());
}
private void m() {
System.out.println("in m");
}
}
import java.lang.invoke.MethodHandles;
import java.lang.invoke.*;
import java.lang.invoke.MethodType;
public class PrivateClassMethodLookupTest{
public static void main(String[] args) throws Throwable{
new PrivateClassMethodLookupTest().m();
MethodHandle mh = MethodHandles.lookup()
.findVirtual(PrivateClassMethodLookupTest.class, "m", MethodType.methodType(void.class));
mh.invoke(new PrivateClassMethodLookupTest());
}
private void m() { System.out.println("in m");}
}
(working fine)
while
MethodHandles.Lookup.findVirtual() throws IllegalAccessException if the method MH is being created for is private and declared in an interface. With in interface methods have full privilege then why can't use findVirtual.
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
interface PrivateInterfaceMethodLookupTest {
default void boot() throws Throwable {
System.out.println("main");
m();
MethodHandles.Lookup lookup = MethodHandles.lookup();
MethodHandle mh= lookup
.findVirtual(PrivateInterfaceMethodLookupTest.class, "m", MethodType.methodType(void.class));
// mh.invokeExact(this);
}
private void m() { System.out.println("in m");
}
}
public class TestIm implements PrivateInterfaceMethodLookupTest{
public static void main(String args[]) throws Throwable
{
TestIm a =new TestIm();
a.boot();
}
}
If above behavior is correct then while accessing interface's private method with MethodHandles.lookup().unreflect() should also throw the same exception.
public interface Unreflect {
public static void main(String[] args) throws Throwable {
Method pm = Unreflect.class.getDeclaredMethod("m");
// pm.setAccessible(true);
MethodHandle pmh = MethodHandles.lookup().unreflect(pm);
// pmh.invoke(new Unreflect());
}
private void m() {
System.out.println("in m");
}
}