使用Java Preon库在单个类上进行多个@ImportStatic注释

时间:2015-05-06 14:46:44

标签: java annotations preon

我想知道是否有办法为Preon的单个类使用多个@ImportStatic注释?

我试过了:

@ImportStatic(classA.class)
@ImportStatic(classB.class)

// Also tried:

@ImportStatic(classA.class classB.class)

// And: 
@ImportStatic(classA.class,classB.class)

但这些都不是有效的......

我有一个规范要求我查看外(父)类的枚举值(classA),如果它与特定值匹配,那么我还必须对其中一个父对象进行枚举检查在继续读取下一个字段之前枚举值(classB)。

编辑: 这是我第一次尝试解决方案,基本上复制ImportStatic并制作一个ImportStatic2注释......必须看看它的工作情况。

diff --git a/preon-binding/src/main/java/org/codehaus/preon/el/ImportSupportingObjectResolverContext.java b/preon-binding/src/main/java/org/codehaus/preon/el/ImportSupportingObjectResolverContext.java
index b737719..8f4946c 100644
--- a/preon-binding/src/main/java/org/codehaus/preon/el/ImportSupportingObjectResolverContext.java
+++ b/preon-binding/src/main/java/org/codehaus/preon/el/ImportSupportingObjectResolverContext.java
@@ -89,14 +89,23 @@ public class ImportSupportingObjectResolverContext implements

     public static ObjectResolverContext decorate(ObjectResolverContext context,
                                                  Class<?> type) {
-        if (type.isAnnotationPresent(ImportStatic.class)) {
+        if (type.isAnnotationPresent(ImportStatic.class) || type.isAnnotationPresent(ImportStatic2.class)) {
             ImportSupportingObjectResolverContext replacement = new ImportSupportingObjectResolverContext();
             Map<String, Reference<Resolver>> references = new HashMap<String, Reference<Resolver>>();
+           if (type.isAnnotationPresent(ImportStatic.class)) {
             for (Class<?> imported : type.getAnnotation(ImportStatic.class)
                     .value()) {
                 references.put(imported.getSimpleName(), new ClassReference(
                         imported, replacement));
             }
+            }
+           if (type.isAnnotationPresent(ImportStatic2.class)) {
+            for (Class<?> imported : type.getAnnotation(ImportStatic2.class)
+                    .value()) {
+                references.put(imported.getSimpleName(), new ClassReference(
+                        imported, replacement));
+            }
+            }
             replacement.context = context;
             replacement.references = references;
             return replacement;

1 个答案:

答案 0 :(得分:0)

使用如下语法显示:

@ImportStatic({First.class,Second.class})