Java Reflection Generic Type from Field getGenericlyTypeCount(Field field)

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

Description

get Genericly Type Count

License

Open Source License

Declaration

public static int getGenericlyTypeCount(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 int getGenericlyTypeCount(Field field) {

        if (field.getGenericType() instanceof ParameterizedType) {

            ParameterizedType type = (ParameterizedType) field.getGenericType();

            return type.getActualTypeArguments().length;
        }/*ww  w  .  j a v  a2 s . c  o m*/

        return 0;
    }

    public static int getGenericlyTypeCount(Method method) {

        return method.getGenericParameterTypes().length;
    }

    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 {

            return type;
        }
    }

    public static Object getGenericType(Method method) {

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

Related

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