Java Reflection Generic Type from Field getGenericType(Type type, Type fieldType)

Here you can find the source of getGenericType(Type type, Type fieldType)

Description

get Generic Type

License

Open Source License

Declaration

public static Type getGenericType(Type type, Type fieldType) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.lang.reflect.Array;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.GenericArrayType;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.util.Arrays;

public class Main {
    public static Type getGenericType(Type type, Type fieldType) {
        if (type instanceof ParameterizedType) {
            Class<?> klass = getTypeClass(type);
            if (fieldType instanceof TypeVariable) {
                TypeVariable<?>[] typeVariables = klass.getTypeParameters();
                for (int i = 0; i < typeVariables.length; i++) {
                    TypeVariable<?> typeVariable = typeVariables[i];
                    if (typeVariable.equals(fieldType)) {
                        fieldType = ((ParameterizedType) type).getActualTypeArguments()[i];
                        break;
                    }/*from ww w . ja  v a 2s .  co m*/
                }
            } else if (fieldType instanceof ParameterizedType) {
                Type[] typeArguements = ((ParameterizedType) fieldType).getActualTypeArguments();
                int hashCode = Arrays.hashCode(typeArguements);
                for (int i = 0; i < typeArguements.length; i++) {
                    typeArguements[i] = getGenericType(type, typeArguements[i]);
                }
                if (hashCode != Arrays.hashCode(typeArguements)) {
                    try {
                        Field field = fieldType.getClass().getDeclaredField("actualTypeArguments");
                        field.setAccessible(true);
                        field.set(fieldType, typeArguements);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        return fieldType;
    }

    @SuppressWarnings("unchecked")
    public static <T> Class<T> getTypeClass(Type type) {
        Class<T> klass = null;
        if (type instanceof Class<?>) {
            klass = (Class<T>) type;
        } else if (type instanceof ParameterizedType) {
            klass = (Class<T>) ((ParameterizedType) type).getRawType();
        } else if (type instanceof GenericArrayType) {
            klass = (Class<T>) Array
                    .newInstance(getTypeClass(((GenericArrayType) type).getGenericComponentType()), 0).getClass();
        }
        return klass;
    }

    public static <T> T newInstance(Class<T> klass) {
        T t = null;
        try {
            Constructor<T> constructor = klass.getDeclaredConstructor();
            if (constructor != null) {
                constructor.setAccessible(true);
                t = constructor.newInstance();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return t;
    }
}

Related

  1. getGenericType(Field field)
  2. getGenericType(Field field)
  3. getGenericType(Field field)
  4. getGenericType(Field field)
  5. getGenericType(Field field)
  6. getGenericTypeArguments(final Field field)
  7. getGenericTypeOfCollectionField(Field field)
  8. getGenericTypes(Field field)
  9. getGenericTypes(Field field)