Example usage for java.lang.reflect Field getGenericType

List of usage examples for java.lang.reflect Field getGenericType

Introduction

In this page you can find the example usage for java.lang.reflect Field getGenericType.

Prototype

public Type getGenericType() 

Source Link

Document

Returns a Type object that represents the declared type for the field represented by this Field object.

Usage

From source file:FieldSpy.java

public static void main(String... args) {
    try {// www .  j  a v  a 2  s. co m
        Class<?> c = Class.forName("FieldSpy");
        Field f = c.getField("name");
        System.out.format("Type: %s%n", f.getType());
        System.out.format("GenericType: %s%n", f.getGenericType());

        // production code should handle these exceptions more gracefully
    } catch (ClassNotFoundException x) {
        x.printStackTrace();
    } catch (NoSuchFieldException x) {
        x.printStackTrace();
    }
}

From source file:Main.java

public static Class<?> getGenericType(Field f) {
    Type type = f.getGenericType();
    if (type instanceof ParameterizedType) {
        type = ((ParameterizedType) type).getActualTypeArguments()[0];
        if (type instanceof Class<?>)
            return (Class<?>) type;
    } else if (type instanceof Class<?>)
        return (Class<?>) type;
    return null;//from w  w w.  j a  va 2 s. c o  m
}

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   ww w  . j  a v a2  s  . c  o  m*/
    return null;
}

From source file:Main.java

public static Class<?> getCollectionType(Field field) {
    if (field.getGenericType() instanceof ParameterizedType) {
        ParameterizedType type = (ParameterizedType) field.getGenericType();
        return (Class<?>) type.getActualTypeArguments()[0];
    }// w  ww .j  a v  a 2s  .  co  m
    return Object.class;
}

From source file:Main.java

public static Type getFieldType(Class<?> clazz, String fieldName) {
    try {//from w w  w .  j a  va2s.c om
        Field field = clazz.getField(fieldName);

        return field.getGenericType();
    } catch (Exception ex) {
        return null;
    }
}

From source file:Main.java

/**
 * getChildClass/*from  w w w  .j  av a 2s .  c om*/
 *
 * @param field List<Child> children type
 * @return thie child class
 */
public static Class getChildClass(Field field) {
    Type t = field.getGenericType();
    Type actualType = ((ParameterizedType) t).getActualTypeArguments()[0];
    Class subclass = null;
    try {
        subclass = Class.forName(actualType.getClass().getName());
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
    System.out.println(subclass.getSimpleName());
    return subclass;
}

From source file:Main.java

public static Class<?>[] getFieldGenericArgs(Field field) {
    Type genericType = field.getGenericType();
    if (genericType instanceof ParameterizedType) {
        return getGenericArgs((ParameterizedType) genericType);
    } else {//from ww  w  . j av a 2  s  .c om
        return new Class<?>[0];
    }
}

From source file:Main.java

static Class<?> getParametricClass(Field field) {
    ParameterizedType paramType = (ParameterizedType) field.getGenericType();
    return getParametricClass(paramType);
}

From source file:com.cognifide.qa.bb.mapper.field.PageObjectProviderHelper.java

/**
 * Gets generic type from field (if field type is {@link ParameterizedType})
 * /*w w  w .  ja v a  2s .com*/
 * @param field class field
 * @return generic type
 */
public static Class<?> getGenericType(Field field) {
    Type type = field.getGenericType();
    if (type instanceof ParameterizedType) {
        Type firstParameter = ((ParameterizedType) type).getActualTypeArguments()[0];
        if (!(firstParameter instanceof WildcardType)) {
            return (Class<?>) firstParameter;
        }
    }
    return null;
}

From source file:com.github.jasonruckman.sidney.core.type.Types.java

public static TypeBindings binding(Field field, TypeBindings parentBindings) {
    Type t = field.getGenericType();
    if (t == null) {
        t = field.getType();// www  . ja va 2s.  co  m
    }
    JavaType javaType = TypeFactory.defaultInstance().constructType(t, parentBindings);
    return new TypeBindings(TypeFactory.defaultInstance(), javaType);
}