Dynamically instantiating abstract subclass
First I have to say I am real new to Java. So all apologize if my question sound stupid but I haven't been able to find a solution so far...
I would like to dynamically instantiate a abstract class subclass.
This is the code
public class CommandMap implements Handler.Callback
{
private HashMap <Integer, Class<? extends AbstractCommand> > __commandHashMap;
protected CommandMap()
{
__commandHashMap = new HashMap<Integer, Class<? extends AbstractCommand>>();
}
/**
* Map an ID to a command
* @param what Id used in the Message sent
* @param command Command to be executed
*/
public void mapWhat(Integer what, Class<? extends AbstractCommand> command)
{
if ( !__commandHashMap.containsKey(what) )
{
__commandHashMap.put(what, command);
}
}
/**
* Unmap the id/command pair
* @param what Id
*/
public void unmapWhat(Integer what)
{
if ( __commandHashMap.containsKey(what) )
{
__commandHashMap.remove(what);
}
}
public boolean handleMessage (Message message)
{
// call the corresponding command
if ( __commandHashMap.containsKey(message.what) )
{
Class<? extends AbstractCommand> commandClass = __commandHashMap.get(message.what);
AbstractCommand command = commandClass.getClass().newInstance();
}
return true; // for now
}
}
When doing so the part
AbstractCommand command = commandClass.getClass().newInstance();
is giving me an error (illegalAccessException and InstantiationException) in my IDE (not at compile time as I haven't tried it yet)
so I surround it with try/catch like this
public boolean handleMessage (Message message)
{
// call the corresponding command
if ( __commandHashMap.containsKey(message.what) )
{
Class<? extends AbstractCommand> commandClass = __commandHashMap.get(message.what);
try
{
AbstractCommand command = commandClass.getClass().newInstance();
}
catch (IllegalAccessException e)
{
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
catch (InstantiationException e)
{
e.printStackTrace(); //To cha开发者_JS百科nge body of catch statement use File | Settings | File Templates.
}
}
return true; // for now
}
but then it tells me that the type Class (sent by the newInstance() ) is obvisouly not of type AbstractCommand.
When trying to cast Class to AbstractCommand doing
AbstractCommand command = (AbstractCommand) commandClass.getClass().newInstance();
It tells me that Class cannot be cast to AbstractCommand.
So I was wondering what I am doing wrong ?
Thanks again for any help you could provide.
I think what you want instead of
commandClass.getClass().newInstance()
is
commandClass.newInstance()
commandClass is, itself, a Class. Therefore calling getClass() on it will return java.lang.Class, and if you were to instantiate that (you couldn't, but if you could) it wouldn't be assignable to an AbstractCommand. But removing the extra getClass(), you have the class of the command, and when you instantiate it, you'll get an AbstractCommand instance. Everything else seems fine, I think.
精彩评论