How to get parameterized type

Viewed 2439

Please follow the code snippet

public class A<T> {

private T genericInstance() {
    T target = null;
    try {
        Class<T> clazz = getGenericClass();
        target = clazz.newInstance();
    } catch (Exception e) {
        log.error(e.getMessage(), e);
    }
    return target;
}

@SuppressWarnings("unchecked")
private Class<T> getGenericClass() {
    ParameterizedType type = (ParameterizedType) getClass().getGenericSuperclass();
    Type[] types = type.getActualTypeArguments();
    return (Class<T>) types[0];
}

}

while I execute the code getGenericClass() its return the the Generic Super class but my requirement is to get class of T and want to instantiate it dynamically.

1 Answers
Related