Java Reflection Generic Type from Field getGenericType(Field field)

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

Description

get Generic Type

License

Open Source License

Declaration

public static Type getGenericType(Field field) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2010 Robert "Unlogic" Olofsson (unlogic@unlogic.se).
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v3
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/lgpl-3.0-standalone.html
 ******************************************************************************/

import java.lang.reflect.Field;

import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.WildcardType;

public class Main {
    public static Type getGenericType(Field field) {

        Type type = ((ParameterizedType) field.getGenericType()).getActualTypeArguments()[0];

        if (type instanceof WildcardType) {

            //TODO do a little bit more research on this part...
            return ((WildcardType) type).getUpperBounds()[0];

        } else {// ww  w.  j a v  a2s  .c  o m

            return type;
        }
    }

    public static Object getGenericType(Method method) {

        return method.getGenericParameterTypes()[0];
    }
}

Related

  1. getGenericParametersInternal(Type genericFieldType)
  2. getGenericReturnType(Method method, Field field, boolean isAllowNull)
  3. getGenericsTypeFromCollectionField(Field field)
  4. getGenericType(Field field)
  5. getGenericType(Field field)
  6. getGenericType(Field field)
  7. getGenericType(Field field)
  8. getGenericType(Field field)
  9. getGenericType(Type type, Type fieldType)