繁体   English   中英

使用运行时给定的类型创建通用映射

[英]Create a generic map using type given at runtime

标题可能有点难以理解,但让我简单地描述一下我的问题。

我们假设我有一个这样的注释:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface Identifier {

}

现在,我创建了一个用它来注释其任何字段的类:

public class Student {
    private String name;
    private String surname;
    @Identifier
    private String idNumber;
    ...
}

最后,在运行时,我想创建一个键类型为typeof(field annotated with @Identifier)Map typeof(field annotated with @Identifier)和值类型Student 请注意,任何字段都可以使用@Identifier进行注释。

有任何想法吗?

编辑

好的,让我澄清一点:

class Student {
    private String name;
    private String surname;
    @Identifier
    private String idNumber;
}

class Foo {
    @Identifier
    private Integer x;
}

//  Now, what I want to have are two maps:

SortedMap students;     //  key type: String
                        //  value type: Student
SortedMap foos;         //  key type: Integer
                        //  value type: Foo

提前致谢!

我还不确定你想做什么。

在运行时我想创建一个键类型为typeof的Map(带有@Identifier注释的字段)和值类型Student

您可以创建原始MapMap<Object, Object> 您可以获取使用@Identifier注释的字段类型。 我不确定你对学生价值类型是什么意思所以我认为你的意思是Student类型,即。 它的Class对象。

public static void main(String[] args) throws Exception {
    Class<?> clazz = Student.class;
    Map<Object, Object> map = new HashMap<>();
    Field[] fields = clazz.getDeclaredFields();
    for (Field field : fields) {
        Identifier annotation = field.getAnnotation(Identifier.class);
        if (annotation != null) {
            map.put(field.getType(), clazz);
        }
    }
    System.out.println(map);
}

在您的问题中使用您的示例类,这将打印出来

{class java.lang.String=class com.spring.Student}

因此,带注释的字段类型将映射到类类型。

您将无法拥有Map<String,Student>因为您在编译时不知道String类型(可能甚至不是Student )。 你可以尝试强制转换,但是你要为自己设置一些ClassCastException

因此,您将获得一个方法(在我的示例中为myMethod ),该方法将传递可能包含使用@Identifier注释的字段的对象。

很抱歉破解了你的泡泡但是没有办法在运行时保留通用信息。 你可以得到的最接近的是Map<Field, Class<?>> ,它保存了你想要的类型的键值对。 这是你如何做到的:

public Map<Field, Class<?>> myMethod(Object obj) {
    Map<Field, Class<?>> result = new HashMap<Field, Class<?>>();
    for(Field field : obj.getClass().getDeclaredFields()) {
        Identifier identifier = field.getAnnotation(Identifier.class);
        if(identifier != null) {
            result.put(field, obj.getClass());
            return result;
        }
    }
    return result;
}

在我的示例中,结果将是空Map或具有一个键值对的Map 我建议您应该为结果使用单独的类型而不是Map 当然,如果你想要一个Field以外的东西,你可以篡改代码,例如你可以使用FieldgetType()getGenericType()方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM