Java equivalent of typeof (SomeClass)

I'm trying to implement

Hashtable<string, -Typeof one Class-> 

in java. But I do not know how to do this. I tried

Hashtable<String, AbstractRestCommand.class>

but it seems wrong.

Btw. I want this to create a new instance of the class to reflect at runtime.

So my question is: how to do this?

Edit:

I have an abstract class "AbstractRestCommand". Now I would like to create a Hashtable with many such commands:

        Commands.put("PUT",  -PutCommand-);
    Commands.put("DELETE", -DeleteCommand-);

where PutCommand and DeleteCommand extend AbstractRestCommand, so I can create a new instance with

String com = "PUT"
AbstractRestCommand command = Commands[com].forName().newInstance();
...
+3
source share
4 answers

Do you want to create a string-to-class mapping? This can be done as follows:

Map<String, Class<?>> map = new HashMap<String, Class<?>>();
map.put("foo", AbstractRestCommand.class);

, , :

Map<String, Class<? extends AbstractRestCommand>> map =
                    new HashMap<String, Class<? extends AbstractRestCommand>>();
map.put("PUT", PutCommand.class);
map.put("DELETE", DeleteCommand.class);
...
Class<? extends AbstractRestCommand> cmdType = map.get(cmdName);
if(cmdType != null)
{
    AbstractRestCommand command = cmdType.newInstance();
    if(command != null)
        command.execute();
}
+4

, :

Hashtable<String, ? extends AbstractRestCommand>
+1

Try:

Hashtable<string, Object>

Edit:

After reading your edit, you can simply:

Hashtable<String, AbstractRestCommand>
+1
source

Of course you just need to

Hashtable<String, AbstractRestCommand>
+1
source

All Articles