I'm making a game engine and an integral part of the game is that it populates worlds based on maps loaded from a XML file, a method "Actor load(Map<String, String> code)" in a class ActorLoader takes a map and returns an actor based on this map. When you make a game using this engine you have a main package and a subpackage called actors that has the classes for the actors.
Before, ActorLoader was abstract and was subclassed with load overloaded in the main project to something like this
String role = code.get("Role");
if(role.equals("Wulf")){
return new Wulf(code);
}else if(role.equals("Obstacle")){
return new Obstacle(code);
}else if(role.equals("Bullit")){
return new Bullit(code);
}else if(role.equals("Switch")){
return new Switch(code);
}else if(role.equals("Spikes")){
return new Spikes(code);
}else if(role.equals("Exit")){
return new Exit(code);
}else if(role.equals("Camera")){
return new Camera(code);
}else if(role.equals("Door")){
return new Door(code);
}else if(role.equals("Scenery")){
return new Scenery(code);
}else if(role.equals("Physics")){
return new Physics(code);
}else if(role.equals("RobotBunny")){
return new RobotBunny(code);
}else{
throw new NoSuchRoleException();
}
Basically, each actor you add to the game needs a new branch in the if statement. But somebody looked at my code and suggested I use reflection instead and now I've got this, it doesn't do anything yet beside getting a class, I'm just experimenting.
try {
Class c = Class.forName(nameOfMainPackage + ".actors." + code.get("Role"));
} catch (ClassNotFoundException ex) {}
Now, this gets the correct class, and from here I can get get a constructor from that class to make the actor, so I was planning to put this code in the base ActorLoader class so it doesn't needed to be overloaded. But there is one small thing, would I have to tell the ActorLoader what the main package is or is there a way I can "ask" Java what it is?