【问题标题】:Multiple @ImportStatic annotations on a single class using the Java Preon library使用 Java Preon 库对单个类进行多个 @ImportStatic 注释
【发布时间】:2015-05-06 14:46:43
【问题描述】:

我想知道是否可以通过 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;

【问题讨论】:

    标签: java annotations preon


    【解决方案1】:

    它使用如下语法出现:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-20
      • 2019-10-30
      • 2020-09-06
      • 2015-09-03
      • 1970-01-01
      相关资源
      最近更新 更多