Here you can find the source of getGenericReturnTypeMap(Method method, boolean isAllowNull)
Parameter | Description |
---|---|
method | method or null if field |
isAllowNull | whether null is allowed as a return value or expected Object.class |
public static Class getGenericReturnTypeMap(Method method, boolean isAllowNull)
//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 ww w. j av a 2 s.c o m*/ * Returns the second 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 getGenericReturnTypeMap(Method method, Field field, boolean isAllowNull) { if (method == null) { return getGenericFieldTypeMap(field, isAllowNull); } else { return getGenericReturnTypeMap(method, isAllowNull); } } /** * Returns the second generic type parameter of a return value by a field or 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 getGenericReturnTypeMap(Method method, boolean isAllowNull) { Type t = method.getGenericReturnType(); Class result = getGenericType(t, 1); if (!isAllowNull && result == null) { return Object.class; } return result; } /** * Returns the generic type parameter of a return value by a field or method. * @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 getGenericFieldTypeMap(Field field, boolean isAllowNull) { Type t = field.getGenericType(); Class result = getGenericType(t, 1); if (!isAllowNull && result == null) { return Object.class; } return result; } /** * 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; } 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; } /** * 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; } }