Here you can find the source of getGenericFieldType(Field field, boolean isAllowNull)
Parameter | Description |
---|---|
field | field or null if method |
isAllowNull | whether null is allowed as a return value or expected Object.class |
public static Class getGenericFieldType(Field field, 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 w w w .j av a 2s .c om*/ * 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; } }