Java Reflection Generic Type getGenericType(@Nullable Type genericType)

Here you can find the source of getGenericType(@Nullable Type genericType)

Description

Retrieves the generic subtype of the given type.

License

Open Source License

Parameter

Parameter Description
genericType the type to get the generic type from

Return

the generic type, or null if not applicable

Declaration

@Nullable
public static Class<?> getGenericType(@Nullable Type genericType) 

Method Source Code


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

import javax.annotation.Nullable;

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

public class Main {
    /**//from w ww . j a  v  a  2  s . co  m
     * Retrieves the generic subtype of the given type.
     *
     * @param genericType the type to get the generic type from
     * @return the generic type, or null if not applicable
     */
    @Nullable
    public static Class<?> getGenericType(@Nullable Type genericType) {
        if (genericType != null && genericType instanceof ParameterizedType) {
            Type[] types = ((ParameterizedType) genericType).getActualTypeArguments();
            if (types.length > 0 && types[0] instanceof Class<?>) {
                return (Class<?>) types[0];
            }
        }
        return null;
    }
}

Related

  1. getGenericFirst(Object obj)
  2. getGenerics(Type genericType)
  3. getGenerics(Type t)
  4. getGenericString(AccessibleObject ao)
  5. getGenericSuperType(Type t)
  6. getGenericType(Member member, int index)
  7. getGenericType(Method setter)
  8. getGenericType(Object target)
  9. getGenericType(Type genType, int index)