You able to invoke it because you have access to private methods from the same class, where main
is running
Try run this code:
package com.company;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
public class PrivateClassMethodLookupTest {
public static void main(String[] args) throws Throwable {
new PrivateClassMethodLookupTest.Inner().m();
MethodHandle mh = MethodHandles.lookup()
.findVirtual(PrivateClassMethodLookupTest.Inner.class, "m", MethodType.methodType(void.class));
mh.invoke(new PrivateClassMethodLookupTest.Inner());
}
static class Inner {
private void m() {
System.out.println("in m");
}
}
}
To call private methods you should use Reflection API and change method access type:
package com.company;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.reflect.Method;
public class PrivateClassMethodLookupTest {
public static void main(String[] args) throws Throwable {
new PrivateClassMethodLookupTest.Inner().m();
Method declaredMethod = PrivateClassMethodLookupTest.Inner.class.getDeclaredMethod("m");
declaredMethod.setAccessible(true);
MethodHandle mh = MethodHandles.lookup().unreflect(declaredMethod);
mh.invoke(new PrivateClassMethodLookupTest.Inner());
}
static class Inner {
private void m() {
System.out.println("in m");
}
}
}