java - 如何获取字段的泛型参数类

标签 java generics reflection

我正在使用反射库 [1] 来获取 Field。 对于声明为的字段 public Map<Integer, Boolean> nameF; 我想获取它的字符串表示形式: “ map 名称F”。 虽然“Map”和“nameF”很容易获得,但我无法获得“Integer”和“Boolean”类型。

[1] https://github.com/ronmamo/reflections

package main;

import org.reflections.Reflections;
import org.reflections.scanners.FieldAnnotationsScanner;
import org.reflections.scanners.SubTypesScanner;
import org.reflections.scanners.TypeAnnotationsScanner;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.AnnotatedType;
import java.lang.reflect.Field;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.util.Map;
import java.util.Set;

public class Test {

    @Target({ElementType.FIELD})
    @Retention(RetentionPolicy.CLASS)
    public @interface MyAnnotation {
    }

    public class TestClass {

        @MyAnnotation
        public Map<Integer, Boolean> fieldFoo;

    }

    public static void main(String[] args) {
        Reflections reflections = new Reflections(Test.class.getCanonicalName(),
                new SubTypesScanner(false),
                new TypeAnnotationsScanner(),
                new FieldAnnotationsScanner());
        {
            Set<Field> annotated = reflections.getFieldsAnnotatedWith(MyAnnotation.class);
            for (Field controller : annotated) {
                System.out.println("#1:" + controller.getDeclaringClass().getCanonicalName() + " " + controller.getName() + " " + controller.getType().getCanonicalName() + " ");
                for (TypeVariable<? extends Class<?>> elem : controller.getType().getTypeParameters()) {
                    System.out.println("#2:" + elem);
                    for (Type bound : elem.getBounds()) {
                        System.out.println("#3:" + bound);
                        System.out.println("#4:" + bound.getTypeName());
                    }
                    for (AnnotatedType bound : elem.getAnnotatedBounds()) {
                        System.out.println("#5:" + bound);
                        System.out.println("#6:" + bound.getType());
                    }
                }
                System.out.println("#7:" + controller.getClass().getGenericSuperclass());

            }

        }


    }
}

上面的代码结果如下

#1:main.Test.TestClass fieldFoo java.util.Map 
#2:K
#3:class java.lang.Object
#4:java.lang.Object
#5:sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedTypeBaseImpl@ba4d54
#6:class java.lang.Object
#2:V
#3:class java.lang.Object
#4:java.lang.Object
#5:sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedTypeBaseImpl@12bc6874
#6:class java.lang.Object
#7:class java.lang.reflect.AccessibleObject

最佳答案

使用Field.getGenericType()然后转换 Type返回ParameterizedType然后调用ParameterizedType.getActualTypeArguments()对于您的示例,这将返回一个包含 2 的数组; IntegerBoolean 。基于您的代码的示例:

public static void main(String[] args) {
    Reflections reflections = new Reflections(Test.class.getCanonicalName(),
            new SubTypesScanner(false),
            new TypeAnnotationsScanner(),
            new FieldAnnotationsScanner());
    {
        Set<Field> annotated = reflections.getFieldsAnnotatedWith(MyAnnotation.class);
        for (Field controller : annotated) {
            Type genericType = controller.getGenericType();
            if(genericType instanceof ParameterizedType){
                for(Type genericTypeArg: ((ParameterizedType)genericType).getActualTypeArguments()) {
                    System.out.println("Generic Type Arg: "+genericTypeArg.getTypeName());
                }
            } else {
                System.out.println("Can't determine generic type");
            }
        }
    }
}

关于java - 如何获取字段的泛型参数类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53883272/

相关文章:

java - JMockit 依赖构造函数

scala - 类型上限允许子类型但不允许父类型

java - 如何使用 consumer 和 supplier 代替 java 8 中的 Reflection

c# - 如何检查某个程序集是否存在?

java - java 的 parking 场,减少空间

用于手动序列化的 Java 接口(interface)

java - "*"在正则表达式中有什么作用?

c# - 比较 byte[] 和 T

java - Java泛型中类型参数的前向引用

Java 反射 : How can I get the all getter methods of a java class and invoke them