/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.logging.log4j.core.config.plugins.visitors;

import java.lang.annotation.Annotation;

import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.config.plugins.PluginVisitorStrategy;
import org.apache.logging.log4j.status.StatusLogger;

/**
 * Utility class to locate an appropriate PluginVisitor implementation for an annotation.
 */
public final class PluginVisitors {

    private static final Logger LOGGER = StatusLogger.getLogger();

    private PluginVisitors() {
    }

    /**
     * Creates a PluginVisitor instance for the given annotation class using metadata provided by the annotation's
     * {@link PluginVisitorStrategy} annotation. This instance must be further populated with
     * 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) {
            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) {
            LOGGER.error("Error loading PluginVisitor [{}] for annotation [{}].", visitorClass, annotation, e);
            return null;
        }
    }
}

@@ -1713,61 +1713,8 @@
 or.%0A
-     * @param %3CA%3E        the Plugin annotation type.%0A
     
@@ -1827,18 +1827,31 @@
  static 
-%3CA
+PluginVisitor%3C?
  extends
@@ -1862,33 +1862,16 @@
 otation%3E
- PluginVisitor%3CA%3E
  findVis
@@ -1887,17 +1887,36 @@
 l Class%3C
-A
+? extends Annotation
 %3E annota
@@ -1941,72 +1941,80 @@
 nal 
-PluginVisitorStrategy strategy = annotation.getAnnotation(Plug
+Class%3C? extends PluginVisitor%3C? extends Annotation%3E%3E visitorClass = f
 in
+d
 Visi
@@ -2028,14 +2028,19 @@
 tegy
-.class
+(annotation
 );%0A 
@@ -2050,24 +2050,28 @@
     if (
-strategy
+visitorClass
  == null
@@ -2223,118 +2223,415 @@
     
-@SuppressWarnings(%22unchecked%22)%0A        final Class%3C? extends PluginVisitor%3CA%3E%3E visitorClass = (Class%3C? extends
+try %7B%0A            return visitorClass.newInstance();%0A        %7D catch (final Exception e) %7B%0A            LOGGER.error(%22Error loading PluginVisitor %5B%7B%7D%5D for annotation %5B%7B%7D%5D.%22, visitorClass, annotation, e);%0A            return null;%0A        %7D%0A    %7D%0A%0A    private static Class%3C? extends PluginVisitor%3C? extends Annotation%3E%3E findVisitorStrategy(%0A        final Class%3C? extends Annotation%3E annotation) %7B%0A        final
  Plu
@@ -2644,13 +2644,16 @@
 itor
-%3CA%3E%3E)
+Strategy
  str
@@ -2661,75 +2661,219 @@
 tegy
-.value();%0A        try %7B%0A            return visitorClass.newInstance
+ = annotation.getAnnotation(PluginVisitorStrategy.class);%0A        if (strategy != null) %7B%0A            return strategy.value();%0A        %7D%0A        final Annotation%5B%5D annotations = annotation.getDeclaredAnnotations
 ();%0A
@@ -2876,39 +2876,35 @@
 ();%0A        
-%7D catch
+for
  (final 
 Exception e)
@@ -2895,135 +2895,222 @@
 nal 
-Exception e) %7B%0A            LOGGER.error(%22Error loading PluginVisitor %5B%7B%7D%5D for annotation %5B%7B%7D%5D.%22, visitorClass, annotation, e);%0A
+Annotation a : annotations) %7B%0A            final PluginVisitorStrategy fallbackStrategy = a.annotationType().getAnnotation(%0A                PluginVisitorStrategy.class);%0A            if (fallbackStrategy != null) %7B%0A 
     
@@ -3117,38 +3117,96 @@
         
+   
 return 
-null;%0A        %7D
+fallbackStrategy.value();%0A            %7D%0A        %7D%0A        return null;
 %0A    %7D%0A%7D

Add fallback visitor strategy lookup.