Java Reflection Generic Type from Field getGenericTypeArguments(final Field field)

Here you can find the source of getGenericTypeArguments(final Field field)

Description

Attempts to return the actual generic type arguments of a field.

License

Apache License

Parameter

Parameter Description
field the field of the desired class.

Return

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

Declaration

public static List<Class<?>> getGenericTypeArguments(final Field field) 

Method Source Code

//package com.java2s;
/*/*from  ww w  . j a v a  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.Field;

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 a field.
     * <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 field
     *            the field of the desired class.
     * @return A list of types or an empty list, if the actual type could not be
     *         determined.
     */
    public static List<Class<?>> getGenericTypeArguments(final Field field) {

        final List<Class<?>> genericTypeArguments = new ArrayList<Class<?>>();
        if (field.getGenericType() instanceof ParameterizedType) {

            final ParameterizedType parameterizedType = (ParameterizedType) field.getGenericType();

            for (final Type type : parameterizedType.getActualTypeArguments()) {

                if (type instanceof Class) {
                    final Class<?> genericTypeArgument = (Class<?>) type;

                    genericTypeArguments.add(genericTypeArgument);
                }
            }

        }

        return genericTypeArguments;
    }

    /**
     * Attempts to return the actual generic type arguments of an object.
     * <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.
     * @return A list of types or an empty list, if the actual type could not be
     *         determined.
     */
    public static List<Class<?>> getGenericTypeArguments(final Object object) {

        final Class<?> objectType = object.getClass();
        final List<Class<?>> genericTypeArguments = new ArrayList<Class<?>>();

        if (objectType.getGenericSuperclass() instanceof ParameterizedType) {

            final ParameterizedType parameterizedType = (ParameterizedType) objectType.getGenericSuperclass();

            for (final Type type : parameterizedType.getActualTypeArguments()) {

                if (type instanceof Class) {
                    final Class<?> genericTypeArgument = (Class<?>) type;

                    genericTypeArguments.add(genericTypeArgument);
                }
            }
        }

        for (final Type genericInterfaceType : objectType.getGenericInterfaces()) {

            if (genericInterfaceType instanceof ParameterizedType) {

                final ParameterizedType parameterizedType = (ParameterizedType) genericInterfaceType;

                for (final Type type : parameterizedType.getActualTypeArguments()) {

                    if (type instanceof Class) {
                        final Class<?> genericTypeArgument = (Class<?>) type;

                        if (!genericTypeArguments.contains(genericTypeArgument)) {

                            genericTypeArguments.add(genericTypeArgument);
                        }
                    }
                }
            }

        }

        return genericTypeArguments;
    }
}

Related

  1. getGenericType(Field field)
  2. getGenericType(Field field)
  3. getGenericType(Field field)
  4. getGenericType(Field field)
  5. getGenericType(Type type, Type fieldType)
  6. getGenericTypeOfCollectionField(Field field)
  7. getGenericTypes(Field field)
  8. getGenericTypes(Field field)
  9. getGenericTypes(Field field)