Java Reflection Generic Type from Class getGenericTypeArgumentsOfInheritedType(final Object object, final Class inheritedType)

Here you can find the source of getGenericTypeArgumentsOfInheritedType(final Object object, final Class inheritedType)

Description

Attempts to return the actual generic type arguments of the chosen inherited type

Note: Requires the type argument be explicitly declared in the class definition:

class Foo extends Bar < String > ...

License

Apache License

Parameter

Parameter Description
object the object instance of the desired class.
inheritedType the inherited type (Class or Interface).

Return

A list of types or an empty list, if the actual type could not be determined.

Declaration

public static List<Class<?>> getGenericTypeArgumentsOfInheritedType(final Object object,
        final Class<?> inheritedType) 

Method Source Code

//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}&lt;{@code String}&gt;{@code ...} or
     * {@code class Foo implements Baz}&lt;{@code String}&gt;{@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;
    }
}

Related

  1. getGenericType(Class type, Class clazz)
  2. getGenericType(Object o, Class declaringClass, int idx)
  3. getGenericType(Type type, Class rawType, int index)
  4. getGenericTypeArgument(Class clazz, int index)
  5. getGenericTypeArgumentFromGenericSuperType(Type genericSuperclass, Class baseRequested)
  6. getGenericTypeClass(Class clazz, int index)
  7. getGenericTypeClasses(List> genericTypeClasses, Type... genericTypes)
  8. getGenericTypeForMapProperty(Class javaClass, String propertyName, boolean isKeyType)
  9. getGenericTypeOfInterface(Class clazz, Class specificInterface)