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
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.