Java Reflection Generic Type from Field getGenericParameterClass(Field field)

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

Description

get Generic Parameter Class

License

Apache License

Declaration

public static Class getGenericParameterClass(Field field) 

Method Source Code


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

import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

public class Main {
    public static Class getGenericParameterClass(Field field) {
        if (!hasGenericParameter(field)) {
            return null;
        }/*www .  j a  v  a2s.c  o m*/
        Type genericType = field.getGenericType();
        return (Class) ((ParameterizedType) genericType).getActualTypeArguments()[0];
    }

    private static boolean hasGenericParameter(Field field) {
        return field.getGenericType() instanceof ParameterizedType;
    }
}

Related

  1. getGenericFieldTypeFromPosition(Field field, int position)
  2. getGenericlyTypeCount(Field field)
  3. getGenericMultivalueType(final Field p)
  4. getGenericMultivalueType(final Field p)
  5. getGenericParameterClass(Field field)
  6. getGenericParameters(Field f)
  7. getGenericParametersInternal(Type genericFieldType)
  8. getGenericReturnType(Method method, Field field, boolean isAllowNull)
  9. getGenericsTypeFromCollectionField(Field field)