Java Reflection Generic Type from Class getGenericTypeClasses(List> genericTypeClasses, Type... genericTypes)

Here you can find the source of getGenericTypeClasses(List> genericTypeClasses, Type... genericTypes)

Description

get Generic Type Classes

License

Apache License

Declaration

public static List<Class<?>> getGenericTypeClasses(List<Class<?>> genericTypeClasses, Type... genericTypes) 

Method Source Code


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

import java.lang.reflect.GenericArrayType;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.lang.reflect.WildcardType;

import java.util.List;

public class Main {
    public static List<Class<?>> getGenericTypeClasses(List<Class<?>> genericTypeClasses, Type... genericTypes) {

        for (Type genericType : genericTypes) {
            if (genericType instanceof ParameterizedType) {
                getGenericTypeClasses(genericTypeClasses,
                        ((ParameterizedType) genericType).getActualTypeArguments());
            } else if (genericType instanceof GenericArrayType) {
                getGenericTypeClasses(genericTypeClasses,
                        ((GenericArrayType) genericType).getGenericComponentType());
            } else if (genericType instanceof TypeVariable) {
                getGenericTypeClasses(genericTypeClasses, ((TypeVariable<?>) genericType).getBounds());
            } else if (genericType instanceof WildcardType) {
                getGenericTypeClasses(genericTypeClasses, ((WildcardType) genericType).getUpperBounds());
                getGenericTypeClasses(genericTypeClasses, ((WildcardType) genericType).getLowerBounds());
            } else if (genericType instanceof Class) {
                Class<?> classType = (Class<?>) genericType;
                if (classType.isArray()) {
                    getGenericTypeClasses(genericTypeClasses, classType.getComponentType());
                } else {
                    genericTypeClasses.add(classType);
                }//  w  w  w  .  j  ava  2 s . c o m
            } else {
                throw new IllegalStateException("unknown type:" + genericType);
            }
        }

        return genericTypeClasses;
    }
}

Related

  1. getGenericType(Type type, Class rawType, int index)
  2. getGenericTypeArgument(Class clazz, int index)
  3. getGenericTypeArgumentFromGenericSuperType(Type genericSuperclass, Class baseRequested)
  4. getGenericTypeArgumentsOfInheritedType(final Object object, final Class inheritedType)
  5. getGenericTypeClass(Class clazz, int index)
  6. getGenericTypeForMapProperty(Class javaClass, String propertyName, boolean isKeyType)
  7. getGenericTypeOfInterface(Class clazz, Class specificInterface)
  8. getGenericTypeOfParameter(Class clazz, String method, int parameterIndex)
  9. getGenericTypeParameter(Class aClass, Class genericClass, int index)