Java Reflection Generic Type from Field getGenericArgument(Field field, int index)

Here you can find the source of getGenericArgument(Field field, int index)

Description

get Generic Argument

License

Apache License

Declaration

public static Class getGenericArgument(Field field, int index) 

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 getGenericArgument(Field field, int index) {
        final Class entryType;
        Type genericType = field.getGenericType();
        if (genericType instanceof ParameterizedType) {
            Type[] typeArgs = ((ParameterizedType) genericType).getActualTypeArguments();
            if (typeArgs != null && typeArgs.length > index) {
                if (typeArgs[index] instanceof Class) {
                    entryType = (Class) typeArgs[index];
                } else {
                    entryType = Object.class;
                }/*from  ww w  .  jav  a  2 s. c  o m*/
            } else {
                entryType = Object.class;
            }
        } else {
            entryType = Object.class;
        }
        return entryType;
    }
}

Related

  1. getGenericClass(Field f, int n)
  2. getGenericClasses(Field field)
  3. getGenericElementType(Field field)
  4. getGenericFieldClassType(Class clz, String propertyName)