I have a class in which i have intialized hashmap in static block. Passing the key, I have retrived the value which is a class. In order to create object for this class. I have used the constructor class to get the constructor and passed arguments and created object.
I have two class in hashmap. To create objectfor EchoExpression I need to pass two arguments and for OutExpression class i need to pass only one argument(String).
Based on the class returned by the key I need to execute which constructor to get and implement, whether the constructor with one argument or two argument.
In EchoExpression, the constructor contains two arguments.
eg:
JXPathExpression check = new JXPathExpression(String expression, Class<?> type)
String belongs to String.class but what class does the Class type argument belongs too? so that i can use it in getting the constructor
public class ExampleFactory {
private static HashMap<String,Class<?>> hmap = new HashMap<String,Class<?>>();
static
{
hmap.put("echo", EchoExpression.class);
hmap.put("Out", OutExpression.class);
}
public void getExpo(String key,String expression)
{
Class aClass =map.get(key);
//Constructor implementation for OutExpression where only one argument string is passed
Constructor constructor = aClass.getConstructor(new Class[]{String.class});
Object object= constructor.newInstance(expression);
//Need constructor for passing two arguments string and class<?> for EchoExpression
return null;
}
}
For such a thing you should in all cases try to have unified constructor parameters or a way to store the parameters per class.
Now for your questions. Class<?>
is a reference to a unknown class. Basically to any class. When using it its more or less equal to Class<Object>
because all classes got Object
as parent.
For using constructors with multiple arguments you first need to fetch the fitting constructor. At this point its already possible to fetch errors that happen in case the class does not support instances with this configuration of arguments. Fetching the constructor works this way:
aClass.getConstructor(new Class[]{String.class, Object.class, ...});
This works with as many argument types as you like. Creating the new instance of the class then works this way:
constructor.newInstance(theString, theObject, ...);
The function newInstanace
is implemented as many arguments as needed. So depending on how many arguments the constructor that was requested requires the newInstance
function will be able to work with it.
For all what you intend... maybe a proposal: Maybe its a option not to load the new objects using reflection but rather by storing instances of those objects and returning copies of the objects created using copy constructors or the clone
method. In many cases this is less difficult to get running.
Class<?>
because this basically says "any class type". Means a class reference to any class, no limits set. Everything else would require a explicit cast - Nitram 2012-04-05 07:06
Maybe what you are looking for is Class.class like in:
Constructor constructor = aClass.getConstructor(new Class[]{String.class, Class.class});