Java Reflection Generic Type from Field getGenericReturnType(Method method, Field field, boolean isAllowNull)

Here you can find the source of getGenericReturnType(Method method, Field field, boolean isAllowNull)

Description

Returns the generic type parameter of a return value by a field or method.

License

Open Source License

Parameter

Parameter Description
method method or null if field
field field or null if method
isAllowNull whether null is allowed as a return value or expected Object.class

Return

generic type parameter

Declaration

public static Class getGenericReturnType(Method method, Field field, boolean isAllowNull) 

Method Source Code

//package com.java2s;
/**************************************************************************************
 * Copyright (C) 2008 EsperTech, Inc. All rights reserved.                            *
 * http://esper.codehaus.org                                                          *
 * http://www.espertech.com                                                           *
 * ---------------------------------------------------------------------------------- *
 * The software in this package is published under the terms of the GPL license       *
 * a copy of which has been included with this distribution in the license.txt file.  *
 **************************************************************************************/

import java.lang.reflect.*;

public class Main {
    /**//from   w w  w.j  av  a  2  s  .  com
     * Returns the generic type parameter of a return value by a field or method.
     * @param method method or null if field
     * @param field field or null if method
     * @param isAllowNull whether null is allowed as a return value or expected Object.class
     * @return generic type parameter
     */
    public static Class getGenericReturnType(Method method, Field field, boolean isAllowNull) {
        if (method == null) {
            return getGenericFieldType(field, isAllowNull);
        } else {
            return getGenericReturnType(method, isAllowNull);
        }
    }

    /**
     * Returns the generic type parameter of a return value by a method.
     * @param method method or null if field
     * @param isAllowNull whether null is allowed as a return value or expected Object.class
     * @return generic type parameter
     */
    public static Class getGenericReturnType(Method method, boolean isAllowNull) {
        Type t = method.getGenericReturnType();
        Class result = getGenericType(t, 0);
        if (!isAllowNull && result == null) {
            return Object.class;
        }
        return result;
    }

    /**
     * Returns the generic type parameter of a return value by a field.
     * @param field field or null if method
     * @param isAllowNull whether null is allowed as a return value or expected Object.class
     * @return generic type parameter
     */
    public static Class getGenericFieldType(Field field, boolean isAllowNull) {
        Type t = field.getGenericType();
        Class result = getGenericType(t, 0);
        if (!isAllowNull && result == null) {
            return Object.class;
        }
        return result;
    }

    public static Class getGenericType(Type t, int index) {
        if (t == null) {
            return null;
        }
        if (!(t instanceof ParameterizedType)) {
            return null;
        }
        ParameterizedType ptype = (ParameterizedType) t;
        if ((ptype.getActualTypeArguments() == null) || (ptype.getActualTypeArguments().length < (index + 1))) {
            return Object.class;
        }
        Type typeParam = ptype.getActualTypeArguments()[index];
        if (!(typeParam instanceof Class)) {
            return Object.class;
        }
        return (Class) typeParam;
    }
}

Related

  1. getGenericMultivalueType(final Field p)
  2. getGenericParameterClass(Field field)
  3. getGenericParameterClass(Field field)
  4. getGenericParameters(Field f)
  5. getGenericParametersInternal(Type genericFieldType)
  6. getGenericsTypeFromCollectionField(Field field)
  7. getGenericType(Field field)
  8. getGenericType(Field field)
  9. getGenericType(Field field)