From a7cde119d6ef2830e026cef9958ff688a2b60587 Mon Sep 17 00:00:00 2001 From: Georgios Andrianakis Date: Tue, 8 Jan 2019 15:08:45 +0200 Subject: [PATCH] Avoid adding the same class to be validated multiple times When using Bean Validation (which have to use the recorder.classProxy), it makes sense to create the initial set from the names of the classes that need to be validated and then create the class proxies. Doing it the other way around negates the use of the Set since each proxy is different even if the target class is the same --- .../BeanValidationProcessor.java | 32 +++++++++++-------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/extensions/bean-validation/deployment/src/main/java/org/jboss/shamrock/beanvalidation/BeanValidationProcessor.java b/extensions/bean-validation/deployment/src/main/java/org/jboss/shamrock/beanvalidation/BeanValidationProcessor.java index 247b1cc2dd467..7952beac6cb65 100644 --- a/extensions/bean-validation/deployment/src/main/java/org/jboss/shamrock/beanvalidation/BeanValidationProcessor.java +++ b/extensions/bean-validation/deployment/src/main/java/org/jboss/shamrock/beanvalidation/BeanValidationProcessor.java @@ -109,34 +109,38 @@ public void build(ValidatorTemplate template, RecorderContext recorder, // Also consider elements that are marked with @ValidateOnExecution consideredAnnotations.add(VALIDATE_ON_EXECUTION); - Set> classesToBeValidated = new HashSet<>(); + Set classNamesToBeValidated = new HashSet<>(); for (DotName consideredAnnotation : consideredAnnotations) { Collection annotationInstances = indexView.getAnnotations(consideredAnnotation); for (AnnotationInstance annotation : annotationInstances) { if (annotation.target().kind() == AnnotationTarget.Kind.FIELD) { - contributeClass(classesToBeValidated, recorder, indexView, annotation.target().asField().declaringClass().name()); + contributeClass(classNamesToBeValidated, indexView, annotation.target().asField().declaringClass().name()); reflectiveFields.produce(new ReflectiveFieldBuildItem(annotation.target().asField())); - contributeClassMarkedForCascadingValidation(classesToBeValidated, recorder, indexView, consideredAnnotation, annotation.target().asField().type()); + contributeClassMarkedForCascadingValidation(classNamesToBeValidated, indexView, consideredAnnotation, annotation.target().asField().type()); } else if (annotation.target().kind() == AnnotationTarget.Kind.METHOD) { - contributeClass(classesToBeValidated, recorder, indexView, annotation.target().asMethod().declaringClass().name()); + contributeClass(classNamesToBeValidated, indexView, annotation.target().asMethod().declaringClass().name()); // we need to register the method for reflection as it could be a getter reflectiveMethods.produce(new ReflectiveMethodBuildItem(annotation.target().asMethod())); - contributeClassMarkedForCascadingValidation(classesToBeValidated, recorder, indexView, consideredAnnotation, annotation.target().asMethod().returnType()); + contributeClassMarkedForCascadingValidation(classNamesToBeValidated, indexView, consideredAnnotation, annotation.target().asMethod().returnType()); } else if (annotation.target().kind() == AnnotationTarget.Kind.METHOD_PARAMETER) { - contributeClass(classesToBeValidated, recorder, indexView, annotation.target().asMethodParameter().method().declaringClass().name()); + contributeClass(classNamesToBeValidated, indexView, annotation.target().asMethodParameter().method().declaringClass().name()); // a getter does not have parameters so it's a pure method: no need for reflection in this case - contributeClassMarkedForCascadingValidation(classesToBeValidated, recorder, indexView, consideredAnnotation, + contributeClassMarkedForCascadingValidation(classNamesToBeValidated, indexView, consideredAnnotation, // FIXME this won't work in the case of synthetic parameters annotation.target().asMethodParameter().method().parameters().get(annotation.target().asMethodParameter().position())); } else if (annotation.target().kind() == AnnotationTarget.Kind.CLASS) { - contributeClass(classesToBeValidated, recorder, indexView, annotation.target().asClass().name()); + contributeClass(classNamesToBeValidated, indexView, annotation.target().asClass().name()); // no need for reflection in the case of a class level constraint } } } + Set> classesToBeValidated = new HashSet<>(); + for (DotName className : classNamesToBeValidated) { + classesToBeValidated.add(recorder.classProxy(className.toString())); + } template.initializeValidatorFactory(classesToBeValidated); // Add the annotations transformer to add @MethodValidated annotations on the methods requiring validation @@ -159,8 +163,8 @@ private static void contributeBuiltinConstraints(Set constraintCollecto } } - private static void contributeClass(Set> classCollector, RecorderContext recorder, IndexView indexView, DotName className) { - classCollector.add(recorder.classProxy(className.toString())); + private static void contributeClass(Set classNamesCollector, IndexView indexView, DotName className) { + classNamesCollector.add(className); for (ClassInfo subclass : indexView.getAllKnownSubclasses(className)) { if (Modifier.isAbstract(subclass.flags())) { // we can avoid adding the abstract classes here: either they are parent classes @@ -168,19 +172,19 @@ private static void contributeClass(Set> classCollector, RecorderContex // without any proper implementation and we can ignore them. continue; } - classCollector.add(recorder.classProxy(subclass.name().toString())); + classNamesCollector.add(subclass.name()); } } - private static void contributeClassMarkedForCascadingValidation(Set> classCollector, RecorderContext recorder, IndexView indexView, - DotName consideredAnnotation, Type type) { + private static void contributeClassMarkedForCascadingValidation(Set classNamesCollector, + IndexView indexView, DotName consideredAnnotation, Type type) { if (VALID != consideredAnnotation) { return; } DotName className = getClassName(type); if (className != null) { - contributeClass(classCollector, recorder, indexView, className); + contributeClass(classNamesCollector, indexView, className); } }