Java Reflection Generic Type from Field getGenericType(Field field)

Here you can find the source of getGenericType(Field field)

Description

get Generic Type

License

Apache License

Declaration

public static Class getGenericType(Field field) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.util.Collection;

public class Main {
    public static Class getGenericType(Field field) {
        if (!Collection.class.isAssignableFrom(field.getType())) {
            return field.getType();
        }/*from  w  ww .  j  a v a 2 s . c o m*/
        ParameterizedType type = (ParameterizedType) field.getGenericType();
        return (Class) type.getActualTypeArguments()[0];
    }

    public static Class getGenericType(Method method) {
        if (!Collection.class.isAssignableFrom(method.getReturnType())) {
            return method.getReturnType();
        }
        ParameterizedType type = (ParameterizedType) method.getGenericReturnType();
        return (Class) type.getActualTypeArguments()[0];
    }
}

Related

  1. getGenericParameters(Field f)
  2. getGenericParametersInternal(Type genericFieldType)
  3. getGenericReturnType(Method method, Field field, boolean isAllowNull)
  4. getGenericsTypeFromCollectionField(Field field)
  5. getGenericType(Field field)
  6. getGenericType(Field field)
  7. getGenericType(Field field)
  8. getGenericType(Field field)
  9. getGenericType(Field field)