Java Reflection Generic Type from Field getGenericFieldClassType(Class clz, String propertyName)

Here you can find the source of getGenericFieldClassType(Class clz, String propertyName)

Description

get Generic Field Class Type

License

Apache License

Declaration

public static Class<?> getGenericFieldClassType(Class<?> clz, String propertyName)
            throws NoSuchFieldException, SecurityException 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.lang.reflect.Field;

import java.lang.reflect.ParameterizedType;

public class Main {
    public static Class<?> getGenericFieldClassType(Class<?> clz, String propertyName)
            throws NoSuchFieldException, SecurityException {
        Field field = clz.getDeclaredField(propertyName);
        field.setAccessible(true);//from  ww  w  .  j  a  v a 2 s.  c  o m
        ParameterizedType parameterizedType = (ParameterizedType) field.getGenericType();
        Class<?> genericClass = (Class<?>) parameterizedType.getActualTypeArguments()[0];
        return genericClass;
    }
}

Related

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