Search in sources :

Example 1 with ExecutableTypes

use of org.apache.bval.jsr.util.ExecutableTypes in project tomee by apache.

the class BValInterceptor method computeIsMethodValidated.

private <T> boolean computeIsMethodValidated(Class<T> targetClass, Method method) {
    final Signature signature = Signature.of(method);
    AnnotatedMethod<?> declaringMethod = null;
    for (final Class<?> c : Reflection.hierarchy(targetClass, Interfaces.INCLUDE)) {
        final AnnotatedType<?> annotatedType = CDI.current().getBeanManager().createAnnotatedType(c);
        final AnnotatedMethod<?> annotatedMethod = annotatedType.getMethods().stream().filter(am -> Signature.of(am.getJavaMember()).equals(signature)).findFirst().orElse(null);
        if (annotatedMethod != null) {
            declaringMethod = annotatedMethod;
        }
    }
    if (declaringMethod == null) {
        return false;
    }
    final Collection<ExecutableType> declaredExecutableTypes;
    if (declaringMethod.isAnnotationPresent(ValidateOnExecution.class)) {
        final List<ExecutableType> validatedTypesOnMethod = Arrays.asList(declaringMethod.getAnnotation(ValidateOnExecution.class).type());
        // implicit directly on method -> early return:
        if (validatedTypesOnMethod.contains(ExecutableType.IMPLICIT)) {
            return true;
        }
        declaredExecutableTypes = validatedTypesOnMethod;
    } else {
        final AnnotatedType<?> declaringType = declaringMethod.getDeclaringType();
        if (declaringType.isAnnotationPresent(ValidateOnExecution.class)) {
            // IMPLICIT is meaningless at class level:
            declaredExecutableTypes = removeFrom(Arrays.asList(declaringType.getAnnotation(ValidateOnExecution.class).type()), ExecutableType.IMPLICIT);
        } else {
            final Package pkg = declaringType.getJavaClass().getPackage();
            if (pkg != null && pkg.isAnnotationPresent(ValidateOnExecution.class)) {
                // presumably IMPLICIT is likewise meaningless at package level:
                declaredExecutableTypes = removeFrom(Arrays.asList(pkg.getAnnotation(ValidateOnExecution.class).type()), ExecutableType.IMPLICIT);
            } else {
                declaredExecutableTypes = null;
            }
        }
    }
    final ExecutableType methodType = Methods.isGetter(method) ? ExecutableType.GETTER_METHODS : ExecutableType.NON_GETTER_METHODS;
    return Optional.ofNullable(declaredExecutableTypes).map(ExecutableTypes::interpret).orElse(globalConfiguration.getGlobalExecutableTypes()).contains(methodType);
}
Also used : ExecutableType(javax.validation.executable.ExecutableType) ExecutableTypes(org.apache.bval.jsr.util.ExecutableTypes) ValidateOnExecution(javax.validation.executable.ValidateOnExecution) Signature(org.apache.bval.jsr.metadata.Signature)

Aggregations

ExecutableType (javax.validation.executable.ExecutableType)1 ValidateOnExecution (javax.validation.executable.ValidateOnExecution)1 Signature (org.apache.bval.jsr.metadata.Signature)1 ExecutableTypes (org.apache.bval.jsr.util.ExecutableTypes)1