Reputation: 1789
Can I create an instance of a generic class without knowing its type parameter? I have only 'class' type, which have been obtained from the Class.forName (classname);
//generic class
public class MappedField<T> extends Field {
private T value;
private String sourceName;
public MappedField(String name, String sourceName, FieldType type){
super(name, type);
this.sourceName = sourceName;
}
public MappedField(String name, String sourceName, FieldType type, boolean key){
super(name, type, key);
this.sourceName = sourceName;
}
}
//here's my situation
String columnName = field.getSourceName() != null ? field.getSourceName() : field.getName();
Class c = Class.forName(field.getType().getFullClassName());
//I need like that if `field.getType() = "Integer"`: `MappedField<Integer> objField = new MappedField<Integer>(field)`;
MappedField objField = new MappedField(field);
objField.setValue(getField(c, rset, columnName));
object.getValues().add(objField);
EDIT: It's like I have "java.lang.Integer" and want get MappedField
Upvotes: 0
Views: 853
Reputation: 3139
The Generic type will erased from the classes and interfaces on successful compilation.On runtime it is not possible to detect the generic type.
Refer the link: Use reflection to create a generic parameterized class in Java
Upvotes: 2