View Javadoc

    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements. See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache license, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License. You may obtain a copy of the License at
    8    *
    9    *      http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the license for the specific language governing permissions and
    15   * limitations under the license.
    16   */
    17  package org.apache.logging.log4j.core.config.plugins.validation;
    18
    19  import java.lang.annotation.Annotation;
    20  import java.lang.reflect.ParameterizedType;
    21  import java.lang.reflect.Type;
    22  import java.util.ArrayList;
    23  import java.util.Collection;
    24
    25  import org.apache.logging.log4j.core.util.ReflectionUtil;
    26
    27  /**
    28   * Utility class to locate an appropriate {@link ConstraintValidator} implementation for an annotation.
    29   *
    30   * @since 2.1
    31   */
    32  public final class ConstraintValidators {
    33
    34      private ConstraintValidators() {
    35      }
    36
    37      /**
    38       * Finds all relevant {@link ConstraintValidator} objects from an array of annotations. All validators will be
    39       * {@link ConstraintValidator#initialize(java.lang.annotation.Annotation) initialized} before being returned.
    40       *
    41       * @param annotations the annotations to find constraint validators for
    42       * @return a collection of ConstraintValidators for the given annotations
    43       */
    44      public static Collection<ConstraintValidator<?>> findValidators(final Annotation... annotations) {
    45          final Collection<ConstraintValidator<?>> validators =
    46              new ArrayList<ConstraintValidator<?>>();
    47          for (final Annotation annotation : annotations) {
    48              final Class<? extends Annotation> type = annotation.annotationType();
    49              if (type.isAnnotationPresent(Constraint.class)) {
    50                  final ConstraintValidator<?> validator = getValidator(annotation, type);
    51                  if (validator != null) {
    52                      validators.add(validator);
    53                  }
    54              }
    55          }
    56          return validators;
    57      }
    58
    59      private static <A extends Annotation> ConstraintValidator<A> getValidator(final A annotation,
    60                                                                                final Class<? extends A> type) {
    61          final Constraint constraint = type.getAnnotation(Constraint.class);
    62          final Class<? extends ConstraintValidator<?>> validatorClass = constraint.value();
    63          if (type.equals(getConstraintValidatorAnnotationType(validatorClass))) {
    64              @SuppressWarnings("unchecked") // I don't think we could be any more thorough in validation here
    65              final ConstraintValidator<A> validator = (ConstraintValidator<A>)
    66                  ReflectionUtil.instantiate(validatorClass);
    67              validator.initialize(annotation);
    68              return validator;
    69          }
    70          return null;
    71      }
    72
    73      private static Type getConstraintValidatorAnnotationType(final Class<? extends ConstraintValidator<?>> type) {
    74          for (final Type parentType : type.getGenericInterfaces()) {
    75              if (parentType instanceof ParameterizedType) {
    76                  final ParameterizedType parameterizedType = (ParameterizedType) parentType;
    77                  if (ConstraintValidator.class.equals(parameterizedType.getRawType())) {
    78                      return parameterizedType.getActualTypeArguments()[0];
    79                  }
    80              }
    81          }
    82          return Void.TYPE;
    83      }
    84  }