Here you can find the source of getGenericTypeArgumentsOfInheritedType(final Object object, final Class> inheritedType)
Note: Requires the type argument be explicitly declared in the class definition:
class Foo extends Bar < String > ...
Parameter | Description |
---|---|
object | the object instance of the desired class. |
inheritedType | the inherited type (Class or Interface). |
public static List<Class<?>> getGenericTypeArgumentsOfInheritedType(final Object object, final Class<?> inheritedType)
//package com.java2s; /*/* w ww . j ava 2 s. c o m*/ * #%L * jutility-common * %% * Copyright (C) 2013 - 2014 jutility.org * %% * Licensed 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. * #L% */ import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; import java.util.ArrayList; import java.util.List; public class Main { /** * Attempts to return the actual generic type arguments of the chosen * inherited type * <p> * <strong> Note:</strong> Requires the type argument be explicitly declared * in the class definition: * </p> * <p> * {@code class Foo extends Bar}<{@code String}>{@code ...} or * {@code class Foo implements Baz}<{@code String}>{@code ...} * </p> * * @param object * the object instance of the desired class. * @param inheritedType * the inherited type (Class or Interface). * @return A list of types or an empty list, if the actual type could not be * determined. */ public static List<Class<?>> getGenericTypeArgumentsOfInheritedType(final Object object, final Class<?> inheritedType) { final Class<?> objectType = object.getClass(); final List<Class<?>> genericTypeArguments = new ArrayList<Class<?>>(); boolean found = false; if (objectType.getGenericSuperclass() instanceof ParameterizedType) { final ParameterizedType parameterizedType = (ParameterizedType) objectType.getGenericSuperclass(); if (parameterizedType.getRawType() == inheritedType) { found = true; for (final Type type : parameterizedType.getActualTypeArguments()) { if (type instanceof Class) { final Class<?> genericTypeArgument = (Class<?>) type; genericTypeArguments.add(genericTypeArgument); } } } } if (!found) { for (final Type genericInterfaceType : objectType.getGenericInterfaces()) { if (genericInterfaceType instanceof ParameterizedType) { final ParameterizedType parameterizedType = (ParameterizedType) genericInterfaceType; if (parameterizedType.getRawType() == inheritedType) { for (final Type type : parameterizedType.getActualTypeArguments()) { if (type instanceof Class) { final Class<?> genericTypeArgument = (Class<?>) type; if (!genericTypeArguments.contains(genericTypeArgument)) { genericTypeArguments.add(genericTypeArgument); } } } } break; } } } return genericTypeArguments; } }