是否可以在不知道泛型类型的参数的情况下创建其实例?我只有'class'类型,它是从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);
编辑:
就像我有“ java.lang.Integer”并想要获取MappedField
最佳答案
成功编译后,泛型类型将从类和接口中删除。在运行时无法检测到泛型类型。
参考链接:
Use reflection to create a generic parameterized class in Java