Example usage for java.lang.reflect ParameterizedType getActualTypeArguments

List of usage examples for java.lang.reflect ParameterizedType getActualTypeArguments

Introduction

In this page you can find the example usage for java.lang.reflect ParameterizedType getActualTypeArguments.

Prototype

Type[] getActualTypeArguments();

Source Link

Document

Returns an array of Type objects representing the actual type arguments to this type.

Usage

From source file:Main.java

public static void main(String args[]) {

    Type t = IntegerClass.class.getGenericSuperclass();
    System.out.println(t);//w  ww.  jav a  2 s . c  om

    ParameterizedType p = (ParameterizedType) t;
    System.out.println(p.getActualTypeArguments()[0]);
}

From source file:Main.java

public static void main(String args[]) throws Exception {

    Type type = StringList.class.getGenericSuperclass();
    System.out.println(type);//from  w w w.  j a  v  a  2  s .c  o m
    ParameterizedType pt = (ParameterizedType) type;
    System.out.println(pt.getActualTypeArguments()[0]);

}

From source file:MainClass.java

public static void main(String args[]) throws Exception {

    Type type = StringList.class.getGenericSuperclass();
    System.out.println(type); // java.util.ArrayList<java.lang.String>
    ParameterizedType pt = (ParameterizedType) type;
    System.out.println(pt.getActualTypeArguments()[0]);

}

From source file:GenericReflect.java

public static void main(String[] args) {
    TypeVariable[] tv = List.class.getTypeParameters();
    System.out.println(tv[0].getName()); // E

    class StringList extends ArrayList<String> {
    }/*from   ww w . j  a  va 2 s. co  m*/

    Type type = StringList.class.getGenericSuperclass();
    System.out.println(type);
    ParameterizedType pt = (ParameterizedType) type;
    System.out.println(pt.getActualTypeArguments()[0]);
}

From source file:Main.java

private static Class<?> getParametricClass(ParameterizedType parametType) {
    return (Class<?>) parametType.getActualTypeArguments()[0];
}

From source file:Main.java

private static Class<?> getEntityClass(Field field) {
    Type genType = field.getGenericType();
    if (genType instanceof ParameterizedType) {
        ParameterizedType pt = (ParameterizedType) genType;
        Type type = pt.getActualTypeArguments()[0];
        return (Class<?>) type;
    }//from w  w w.  j  a va2  s .c o m
    return null;
}

From source file:Main.java

public static <T> Class<T> generateType(Class<?> klass) {
    Type type = klass.getGenericSuperclass();
    if (type instanceof ParameterizedType) {
        ParameterizedType paramType = (ParameterizedType) type;
        Type[] actualTypes = paramType.getActualTypeArguments();
        if (actualTypes != null && actualTypes.length > 0) {
            return (Class<T>) actualTypes[0];
        }//from w  w  w  . j  a  v  a2  s .  c o  m
    }

    return null;
}

From source file:Main.java

public static Class<?>[] getGenericArgs(ParameterizedType genericType) {
    Type[] typeArr = genericType.getActualTypeArguments();
    Class<?>[] argsArr = new Class<?>[typeArr.length];
    for (int i = 0; i < typeArr.length; i++) {
        Type t = typeArr[i];//w w  w.  j av a2s  .c om
        if (t instanceof ParameterizedType) {
            // TODO full hierarchy
            t = ((ParameterizedType) t).getRawType();
        }
        String clsName = t.toString().replaceFirst("^(class|interface) ", "");
        argsArr[i] = classForName(clsName);
    }
    return argsArr;
}

From source file:Main.java

static boolean isGenericTypeAssignableFrom(ParameterizedType type, Class<?> clazz) {
    Type[] actualTypes = type.getActualTypeArguments();
    for (Type actualType : actualTypes) {
        Class<?> aClazz = null;
        try {/*from w  ww .java2  s  .com*/
            aClazz = getClass(actualType);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        if (aClazz != null && clazz.isAssignableFrom(aClazz)) {
            return true;
        }
    }
    return false;
}

From source file:Main.java

public static <T> Class<T> getTypeByIndex(Class<T> clz, int index) {
    if (clz == null) {
        return null;
    }//from ww w.  j a  v a  2 s .c  o  m
    Type type = clz.getGenericSuperclass();
    if (type == null) {
        return null;
    }
    if (type instanceof ParameterizedType) {
        ParameterizedType parameterizedType = (ParameterizedType) type;
        Type[] types = parameterizedType.getActualTypeArguments();
        if (types != null && types.length > 0) {
            if (index >= 0 && index < types.length) {
                return (Class<T>) types[index];
            }
        }
    }
    return null;
}