Java Reflection Generic Type from Field getGenericFieldTypeFromPosition(Field field, int position)

Here you can find the source of getGenericFieldTypeFromPosition(Field field, int position)

Description

get Generic Field Type From Position

License

Apache License

Declaration

public static Class<?> getGenericFieldTypeFromPosition(Field field, int position) 

Method Source Code

//package com.java2s;
/**//from w w  w  . ja va 2  s . c o m
 *
 *     Copyright (C) norad.fr
 *
 *     Licensed under the Apache License, Version 2.0 (the "License");
 *     you may not use this file except in compliance with the License.
 *     You may obtain a copy of the License at
 *
 *             http://www.apache.org/licenses/LICENSE-2.0
 *
 *     Unless required by applicable law or agreed to in writing, software
 *     distributed under the License is distributed on an "AS IS" BASIS,
 *     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *     See the License for the specific language governing permissions and
 *     limitations under the License.
 */

import java.lang.reflect.Field;

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

public class Main {
    public static Class<?> getGenericFieldTypeFromPosition(Field field, int position) {
        try {
            Type genericType = field.getGenericType();
            ParameterizedType type = (ParameterizedType) genericType;
            return (Class<?>) type.getActualTypeArguments()[position];
        } catch (Exception e) {
            return null;
        }
    }
}

Related

  1. getGenericClasses(Field field)
  2. getGenericElementType(Field field)
  3. getGenericFieldClassType(Class clz, String propertyName)
  4. getGenericFieldType(Field field, boolean isAllowNull)
  5. getGenericFieldType(Object target, String fieldName)
  6. getGenericlyTypeCount(Field field)
  7. getGenericMultivalueType(final Field p)
  8. getGenericMultivalueType(final Field p)
  9. getGenericParameterClass(Field field)