Java type erasure is going to require you pass a Class<T>
somewhere in order to get a new T
instance. Java class names start with a capital letter. As for your Javadoc, you could do something like @returns a new T instance
. For example,
public class MyClass<T> {
private Class<T> cls;
public MyClass(Class<T> cls) {
this.cls = cls;
}
/**
* @returns a new T instance.
* @throws IllegalAccessException
* @throws InstantiationException
* @throws SecurityException
* @throws NoSuchMethodException
* @throws InvocationTargetException
* @throws IllegalArgumentException
*/
public T getT() throws InstantiationException,
IllegalAccessException, IllegalArgumentException,
InvocationTargetException, NoSuchMethodException,
SecurityException {
return cls.getConstructor().newInstance();
}
}
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.