Skip to content

Commit

Permalink
Hibernate Validator - Work around Jandex issue in enclosingTarget()
Browse files Browse the repository at this point in the history
This is not ideal and we will need a better fix in Jandex but for now we
need a quick workaround.
  • Loading branch information
gsmet committed May 27, 2021
1 parent fe07bc0 commit af5db04
Showing 1 changed file with 28 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -273,26 +273,35 @@ public void build(HibernateValidatorRecorder recorder, RecorderContext recorderC
contributeClass(classNamesToBeValidated, indexView, annotation.target().asClass().name());
// no need for reflection in the case of a class level constraint
} else if (annotation.target().kind() == AnnotationTarget.Kind.TYPE) {
// container element constraints
AnnotationTarget enclosingTarget = annotation.target().asType().enclosingTarget();
if (enclosingTarget.kind() == AnnotationTarget.Kind.FIELD) {
contributeClass(classNamesToBeValidated, indexView, enclosingTarget.asField().declaringClass().name());
reflectiveFields.produce(new ReflectiveFieldBuildItem(enclosingTarget.asField()));
if (annotation.target().asType().target() != null) {
contributeClassMarkedForCascadingValidation(classNamesToBeValidated, indexView,
consideredAnnotation,
annotation.target().asType().target());
// there is a bug in Jandex regarding enclosingTarget() when annotations are on constructors
// (enclosing target is a ClassInfo instead of a MethodInfo and enclosingTarget() tries to catch it to a MethodInfo
// leading to a ClassCastException)
// for now we swallow the exception, pending a bugfix in Jandex
// see https://github.com/quarkusio/quarkus/issues/17491
try {
// container element constraints
AnnotationTarget enclosingTarget = annotation.target().asType().enclosingTarget();
if (enclosingTarget.kind() == AnnotationTarget.Kind.FIELD) {
contributeClass(classNamesToBeValidated, indexView, enclosingTarget.asField().declaringClass().name());
reflectiveFields.produce(new ReflectiveFieldBuildItem(enclosingTarget.asField()));
if (annotation.target().asType().target() != null) {
contributeClassMarkedForCascadingValidation(classNamesToBeValidated, indexView,
consideredAnnotation,
annotation.target().asType().target());
}
} else if (enclosingTarget.kind() == AnnotationTarget.Kind.METHOD) {
contributeClass(classNamesToBeValidated, indexView, enclosingTarget.asMethod().declaringClass().name());
reflectiveMethods.produce(new ReflectiveMethodBuildItem(enclosingTarget.asMethod()));
if (annotation.target().asType().target() != null) {
contributeClassMarkedForCascadingValidation(classNamesToBeValidated, indexView,
consideredAnnotation,
annotation.target().asType().target());
}
contributeMethodsWithInheritedValidation(methodsWithInheritedValidation, indexView,
enclosingTarget.asMethod());
}
} else if (enclosingTarget.kind() == AnnotationTarget.Kind.METHOD) {
contributeClass(classNamesToBeValidated, indexView, enclosingTarget.asMethod().declaringClass().name());
reflectiveMethods.produce(new ReflectiveMethodBuildItem(enclosingTarget.asMethod()));
if (annotation.target().asType().target() != null) {
contributeClassMarkedForCascadingValidation(classNamesToBeValidated, indexView,
consideredAnnotation,
annotation.target().asType().target());
}
contributeMethodsWithInheritedValidation(methodsWithInheritedValidation, indexView,
enclosingTarget.asMethod());
} catch (Exception e) {
// ignore
}
}
}
Expand Down

0 comments on commit af5db04

Please sign in to comment.