Java Reflection Generic Type getGenericElementType(Type type)

Here you can find the source of getGenericElementType(Type type)

Description

Retrieves type parameter from the parameterized type (either Map or Collection) specification.

License

Apache License

Parameter

Parameter Description
type generic type valued specification

Return

parameter type from the provided generic specification

Declaration

public static Type[] getGenericElementType(Type type) 

Method Source Code

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

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

public class Main {
    /**/*w w  w. j av  a2s . c  om*/
     * Retrieves type parameter from the parameterized type (either
     * <code>Map</code> or <code>Collection</code>) specification.
     * 
     * @param type
     *            generic type valued specification
     * @return parameter type from the provided generic specification
     */
    public static Type[] getGenericElementType(Type type) {
        if (isGenericType(type)) {
            return ((ParameterizedType) type).getActualTypeArguments();
        }
        return null;
    }

    /**
     * Checks whether the specified type is a generic type.
     * 
     * @param type
     *            type to be checked.
     * @return true if the type is generic, otherwise false.
     */
    public static boolean isGenericType(Type type) {
        return type instanceof ParameterizedType;
    }
}

Related

  1. getGenericArguments(Type type)
  2. getGenericFirst(Object obj)
  3. getGenerics(Type genericType)
  4. getGenerics(Type t)
  5. getGenericString(AccessibleObject ao)