Java Reflection Generic Type from Class getGenericParameters(Class clazz)

Here you can find the source of getGenericParameters(Class clazz)

Description

get Generic Parameters

License

Open Source License

Declaration

public static Class<?>[] getGenericParameters(Class<?> clazz) 

Method Source Code

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

import java.lang.reflect.Field;

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

public class Main {
    public static Class<?>[] getGenericParameters(Class<?> clazz) {
        return getGenericParametersInternal(clazz.getGenericSuperclass());
    }/*from www . j  a  v  a 2 s  .  co  m*/

    public static Class<?>[] getGenericParameters(Field field) {
        return getGenericParametersInternal(field.getGenericType());
    }

    private static Class<?>[] getGenericParametersInternal(Type genericFieldType) {
        if (genericFieldType instanceof ParameterizedType) {
            ParameterizedType aType = (ParameterizedType) genericFieldType;
            Type[] fieldArgTypes = aType.getActualTypeArguments();
            Class<?>[] clazzes = new Class<?>[fieldArgTypes.length];
            for (int i = 0; i < clazzes.length; i++) {
                clazzes[i] = (Class<?>) fieldArgTypes[i];
            }
            return clazzes;
        }
        return new Class<?>[] {};
    }
}

Related

  1. getGenericInterfacesActualTypes(Collection types, Class aClass)
  2. getGenericParameter(Class clazz, int index)
  3. getGenericParameter(Type class1, int number)
  4. getGenericParameterClass(Class actualClass, int parameterIndex)
  5. getGenericParameterClass(Class clazz)
  6. getGenericParameterType(Class clazz, Integer parameterIndex)
  7. getGenericParameterType0(Class cls, Class genericSuper, int paramIndex)
  8. getGenerics(Class classType)
  9. getGenericsClass(Class cls)