Java Reflection Generic Type from Field getGenericElementType(Field field)

Here you can find the source of getGenericElementType(Field field)

Description

Get the element type for a Collection if possible using the JDK 1.5 generics API or null if not possible.

License

Open Source License

Declaration

public static Class getGenericElementType(Field field) 

Method Source Code

//package com.java2s;
/*//from w  w  w.  j av  a  2  s  .  c om
 * Copyright (c) 1998 - 2005 Versant Corporation
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 * Versant Corporation - initial API and implementation
 */

import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class Main {
    /**
     * Are we running on jdk 1.5 or higher?
     */
    private static boolean isJDK_1_5 = false;

    /**
     * Get the element type for a Collection if possible using the JDK 1.5
     * generics API or null if not possible.
     */
    public static Class getGenericElementType(Field field) {
        return getType(field, 0);
    }

    /**
     * Get the class type of Collections and Maps with jdk1.5 generics if at all
     * possible
     */
    private static Class getType(Field field, int index) {
        if (isJDK_1_5) {
            // do reflection on the jdk1.5 reflection methods
            Class clazz = null;
            try {
                Class tmp = /*CHFC*/field.getClass()/*RIGHTPAR*/;
                Method methodGetGenericType = tmp.getMethod("getGenericType", new Class[] {});
                if (methodGetGenericType == null)
                    return null;
                Object type = methodGetGenericType.invoke(field, new Object[] {});
                if (type == null)
                    return null;
                tmp = /*CHFC*/type.getClass()/*RIGHTPAR*/;
                Method methodActualTypeArguments = tmp.getMethod("getActualTypeArguments", new Class[] {});
                if (methodActualTypeArguments == null)
                    return null;
                Object typeArray = methodActualTypeArguments.invoke(type, new Object[] {});
                if (typeArray == null)
                    return null;
                Object[] types = (Object[]) typeArray;
                clazz = (Class) types[index];
            } catch (Exception e) {
                /*hide it all*/
            }
            return clazz;
        } else {
            return null;
        }
    }
}

Related

  1. getGenericArgument(Field field, int index)
  2. getGenericClass(Field f, int n)
  3. getGenericClasses(Field field)
  4. getGenericFieldClassType(Class clz, String propertyName)
  5. getGenericFieldType(Field field, boolean isAllowNull)
  6. getGenericFieldType(Object target, String fieldName)
  7. getGenericFieldTypeFromPosition(Field field, int position)