I am trying to use Reflection in Java but I am getting a weird error. What are the possible issues when I get an error that says:
java.lang.IllegalAccessException: Class com.myapp.core.utils.EventDispatcher can not access a member of class appApp$1 with modifiers "public"
at sun.reflect.Reflection.ensureMemberAccess(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
I just trying to create my own EventDispatcher class and inside it, the part that I used reflection, which is also the line of code that causes the problem is:
public void dispatchEvent(Event e, String callMethName) {
IEventListener list = ((IEventListener)listeners[i]);
list.getClass().getMethod(callMethName, Event.class).invoke(list, e);
}
On my main class, I have something that calls addListener which will just add the listener into a list in the EventDispatcher class this way:
try {
obj.addListener("onTestHandler", new MyTestEventListener(){
@Override
public void onTestHandler(Event e) {
System.out.println("hello!");
}
});
} catch (SecurityException e) {
e.printStackTrace();
}
So the first parameter that says “onTestHandler” will pass into the EventDispatcher class and eventually as part of the parameter callMethName in the dispatchEvent method, which will call the method dynamically.
The passing of methods and everything is correct. It is the part that has the reflection somehow has problems. It seems to be able to find the method. But for some reason, throws an IllegalAccessException and cannot call the method.
Why is it like this?
Thanks.
I suspect that the anonymous class (
appApp$1) that implementsMyTestEventListenerhas package visibility and that the reflection code is in another package.For this code:
This code will fail because the runtime type returned by
newFoo()is not a public class:This can be overcome by setting the method as accessible:
Or by using the method from the public interface: