org.apache.bval.jsr.util.AnnotationsManager Maven / Gradle / Ivy
/*
* 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.bval.jsr.util;
import java.lang.annotation.Annotation;
import java.lang.annotation.Repeatable;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.Parameter;
import java.util.Collection;
import java.util.Collections;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.validation.Constraint;
import javax.validation.ConstraintDeclarationException;
import javax.validation.ConstraintDefinitionException;
import javax.validation.ConstraintTarget;
import javax.validation.OverridesAttribute;
import javax.validation.Payload;
import javax.validation.ValidationException;
import javax.validation.constraintvalidation.ValidationTarget;
import org.apache.bval.jsr.ApacheValidatorFactory;
import org.apache.bval.jsr.ConstraintAnnotationAttributes;
import org.apache.bval.jsr.ConstraintAnnotationAttributes.Worker;
import org.apache.bval.jsr.ConstraintCached.ConstraintValidatorInfo;
import org.apache.bval.jsr.metadata.Meta;
import org.apache.bval.util.Exceptions;
import org.apache.bval.util.Lazy;
import org.apache.bval.util.ObjectUtils;
import org.apache.bval.util.StringUtils;
import org.apache.bval.util.Validate;
import org.apache.bval.util.reflection.Reflection;
import org.apache.commons.weaver.privilizer.Privilizing;
import org.apache.commons.weaver.privilizer.Privilizing.CallTo;
/**
* Manages (constraint) annotations according to the BV spec.
*
* @since 2.0
*/
@Privilizing(@CallTo(Reflection.class))
public class AnnotationsManager {
private static final class OverriddenAnnotationSpecifier {
final Class extends Annotation> annotationType;
final boolean impliesSingleComposingConstraint;
final int constraintIndex;
OverriddenAnnotationSpecifier(OverridesAttribute annotation) {
this(annotation.constraint(), annotation.constraintIndex());
}
OverriddenAnnotationSpecifier(Class extends Annotation> annotationType, int constraintIndex) {
super();
this.annotationType = annotationType;
this.impliesSingleComposingConstraint = constraintIndex < 0;
this.constraintIndex = Math.max(constraintIndex, 0);
}
@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (obj == null || !obj.getClass().equals(getClass())) {
return false;
}
final OverriddenAnnotationSpecifier other = (OverriddenAnnotationSpecifier) obj;
return Objects.equals(annotationType, other.annotationType) && constraintIndex == other.constraintIndex;
}
@Override
public int hashCode() {
return Objects.hash(annotationType, constraintIndex);
}
}
private class Composition {
Optional> validWorker(
ConstraintAnnotationAttributes attr, Class type) {
return Optional.of(type).map(attr::analyze).filter(Worker::isValid);
}
final Lazy
© 2015 - 2025 Weber Informatics LLC | Privacy Policy