I've been given a framework that is supposed to initialize one of my classes using reflection. In its main method (massim.javaagents.App
) it does this:
Constructor<?> c = null;
Agent ret = null;
c = aClass.getConstructor(new Class[]{String.class,String.class});
ret = (Agent)(c.newInstance(agentName,team));
Note that I cannot change this code, nor the package it is contained in in any way. My class looks like this
package redrovers;
// ...
public class RedAgent extends Agent
{
public RedAgent(String name, String team)
{
super(name, team);
}
}
But when it tries to load it...
Exception in thread "main" java.lang.AssertionError: java.lang.IllegalAccessException: Class massim.javaagents.Agent can not access a member of class redrovers.RedAgent with modifiers "public"
at massim.javaagents.Agent.createAgentFromClass(Agent.java:129)
at massim.javaagents.AgentsInterpreter.addEnvironment(AgentsInterpreter.java:234)
at massim.javaagents.App.main(App.java:53)
Caused by: java.lang.IllegalAccessException: Class massim.javaagents.Agent can not access a member of class redrovers.RedAgent with modifiers "public"
at sun.reflect.Reflection.ensureMemberAccess(Reflection.java:102)
at java.lang.reflect.AccessibleObject.slowCheckMemberAccess(AccessibleObject.java:296)
at java.lang.reflect.AccessibleObject.checkAccess(AccessibleObject.java:288)
at java.lang.reflect.Constructor.newInstance(Constructor.java:413)
at massim.javaagents.Agent.createAgentFromClass(Agent.java:125)
... 2 more
Because the code is in two different packages, I'm running it like this:
$ java -ea -cp redrovers/build/redrovers.jar:javaagents/target/javaagents-2.1.jar massim.javaagents.App
Mainly I don't understand how a public modifier could be preventing access. Does reflection not work with classes from different packages? Do I need to put my RedAgent class in the same package somehow?
Aucun commentaire:
Enregistrer un commentaire