You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by ma...@apache.org on 2014/09/14 07:01:25 UTC

[07/15] git commit: Add fallback visitor strategy lookup.

Add fallback visitor strategy lookup.

  - Weaken findVisitor() return type due to lack of concrete generics usage.
  - Look for an annotated annotation visitor strategy if none is found for the main one.
  - Part of API improvements for LOG4J2-825.


Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo
Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/6ce46875
Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/6ce46875
Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/6ce46875

Branch: refs/heads/master
Commit: 6ce46875270233da329afaa65cd37cde4f421277
Parents: d1766fc
Author: Matt Sicker <ma...@apache.org>
Authored: Sat Sep 13 19:54:51 2014 -0500
Committer: Matt Sicker <ma...@apache.org>
Committed: Sat Sep 13 19:54:51 2014 -0500

----------------------------------------------------------------------
 .../config/plugins/visitors/PluginVisitors.java | 26 +++++++++++++++-----
 1 file changed, 20 insertions(+), 6 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/6ce46875/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/visitors/PluginVisitors.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/visitors/PluginVisitors.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/visitors/PluginVisitors.java
index 1392c60..3ef3203 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/visitors/PluginVisitors.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/visitors/PluginVisitors.java
@@ -39,17 +39,14 @@ public final class PluginVisitors {
      * data to be useful. Such data is passed through both the setters and the visit method.
      *
      * @param annotation the Plugin annotation class to find a PluginVisitor for.
-     * @param <A>        the Plugin annotation type.
      * @return a PluginVisitor instance if one could be created, or {@code null} otherwise.
      */
-    public static <A extends Annotation> PluginVisitor<A> findVisitor(final Class<A> annotation) {
-        final PluginVisitorStrategy strategy = annotation.getAnnotation(PluginVisitorStrategy.class);
-        if (strategy == null) {
+    public static PluginVisitor<? extends Annotation> findVisitor(final Class<? extends Annotation> annotation) {
+        final Class<? extends PluginVisitor<? extends Annotation>> visitorClass = findVisitorStrategy(annotation);
+        if (visitorClass == null) {
             LOGGER.debug("No PluginVisitorStrategy found on annotation [{}]. Ignoring.", annotation);
             return null;
         }
-        @SuppressWarnings("unchecked")
-        final Class<? extends PluginVisitor<A>> visitorClass = (Class<? extends PluginVisitor<A>>) strategy.value();
         try {
             return visitorClass.newInstance();
         } catch (final Exception e) {
@@ -57,4 +54,21 @@ public final class PluginVisitors {
             return null;
         }
     }
+
+    private static Class<? extends PluginVisitor<? extends Annotation>> findVisitorStrategy(
+        final Class<? extends Annotation> annotation) {
+        final PluginVisitorStrategy strategy = annotation.getAnnotation(PluginVisitorStrategy.class);
+        if (strategy != null) {
+            return strategy.value();
+        }
+        final Annotation[] annotations = annotation.getDeclaredAnnotations();
+        for (final Annotation a : annotations) {
+            final PluginVisitorStrategy fallbackStrategy = a.annotationType().getAnnotation(
+                PluginVisitorStrategy.class);
+            if (fallbackStrategy != null) {
+                return fallbackStrategy.value();
+            }
+        }
+        return null;
+    }
 }