Java Reflection Generic Type getGenerics(Type genericType)

Here you can find the source of getGenerics(Type genericType)

Description

Returns the generic types represented in the given type.

License

Apache License

Return

the generic types in order of declaration or an empty array if the type is not genericized

Declaration

public static List<? extends Type> getGenerics(Type genericType) 

Method Source Code

//package com.java2s;
/*//from   www. j a  v a 2s .c  o  m
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

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

import java.util.ArrayList;

import java.util.List;

public class Main {
    /**
     * Returns the generic types represented in the given type. Usage as
     * follows: <code>
     * JavaIntrospectionHelper.getGenerics(field.getGenericType());
     * <p/>
     * JavaIntrospectionHelper.getGenerics(m.getGenericParameterTypes()[0];); </code>
     *
     * @return the generic types in order of declaration or an empty array if
     *         the type is not genericized
     */
    public static List<? extends Type> getGenerics(Type genericType) {
        List<Type> classes = new ArrayList<Type>();
        if (genericType instanceof ParameterizedType) {
            ParameterizedType ptype = (ParameterizedType) genericType;
            // get the type arguments
            Type[] targs = ptype.getActualTypeArguments();
            for (Type targ : targs) {
                classes.add(targ);
            }
        }
        return classes;
    }
}

Related

  1. getGenericArguments(Type type)
  2. getGenericElementType(Type type)
  3. getGenericFirst(Object obj)
  4. getGenerics(Type t)
  5. getGenericString(AccessibleObject ao)
  6. getGenericSuperType(Type t)
  7. getGenericType(@Nullable Type genericType)