简体   繁体   中英

Multiple @ImportStatic annotations on a single class using the Java Preon library

I'm wondering if there is a way to use multiple @ImportStatic annotations for a single class with Preon?

I tried:

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

// Also tried:

@ImportStatic(classA.class classB.class)

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

None of these are valid however...

I have a specification that asks me to look at the enum value (classA) of the outer (parent) class, if it matches a specific value, then I must also do an enum check on on one of the parents other object's enum values (classB) before proceeding to read the next field.

EDIT: Here's my first attempt at a solution, basically copy ImportStatic and make an ImportStatic2 annotation... Have to see how well it works.

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;

使用以下语法可以正常工作:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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