From a1d52b0bb1db8e57be3ed108aa5888e637fc5a14 Mon Sep 17 00:00:00 2001 From: Gavin Date: Sat, 31 Dec 2022 16:37:57 +0100 Subject: [PATCH] minor cleanups to HCANNHelper --- .../boot/model/internal/HCANNHelper.java | 80 +++++++++---------- 1 file changed, 36 insertions(+), 44 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/boot/model/internal/HCANNHelper.java b/hibernate-core/src/main/java/org/hibernate/boot/model/internal/HCANNHelper.java index 5e9daf0d38..871bebd472 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/model/internal/HCANNHelper.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/model/internal/HCANNHelper.java @@ -22,6 +22,7 @@ import org.hibernate.annotations.common.reflection.java.JavaXMember; */ @Internal public final class HCANNHelper { + public static boolean hasAnnotation( AnnotatedElement element, Class annotationToCheck) { @@ -29,12 +30,7 @@ public final class HCANNHelper { return false; } - //noinspection RedundantIfStatement - if ( element.isAnnotationPresent( annotationToCheck ) ) { - return true; - } - - return false; + return element.isAnnotationPresent( annotationToCheck ); } public static boolean hasAnnotation( @@ -46,7 +42,7 @@ public final class HCANNHelper { } return element.isAnnotationPresent( annotationToCheck ) - || element.isAnnotationPresent( annotationToCheck2 ); + || element.isAnnotationPresent( annotationToCheck2 ); } public static boolean hasAnnotation( @@ -56,12 +52,7 @@ public final class HCANNHelper { return false; } - //noinspection RedundantIfStatement - if ( element.isAnnotationPresent( annotationToCheck ) ) { - return true; - } - - return false; + return element.isAnnotationPresent( annotationToCheck ); } public static boolean hasAnnotation( @@ -72,16 +63,13 @@ public final class HCANNHelper { return false; } - //noinspection RedundantIfStatement - if ( element.isAnnotationPresent( annotationToCheck ) - || element.isAnnotationPresent( annotationToCheck2 ) ) { - return true; - } - - return false; + return element.isAnnotationPresent( annotationToCheck ) + || element.isAnnotationPresent( annotationToCheck2); } - public static boolean hasAnnotation(XAnnotatedElement element, Class... annotationsToCheck) { + public static boolean hasAnnotation( + XAnnotatedElement element, + Class... annotationsToCheck) { assert annotationsToCheck != null && annotationsToCheck.length > 0; if ( element == null ) { @@ -101,47 +89,51 @@ public final class HCANNHelper { * @deprecated Prefer using {@link #annotatedElementSignature(JavaXMember)} */ @Deprecated - public static String annotatedElementSignature(XProperty xProperty) { - return getUnderlyingMember( xProperty ).toString(); + public static String annotatedElementSignature(XProperty property) { + return getUnderlyingMember( property ).toString(); } - public static String annotatedElementSignature(final JavaXMember jxProperty) { - return getUnderlyingMember( jxProperty ).toString(); + public static String annotatedElementSignature(final JavaXMember member) { + return getUnderlyingMember( member ).toString(); } /** * @deprecated Prefer using {@link #getUnderlyingMember(JavaXMember)} */ @Deprecated - public static Member getUnderlyingMember(XProperty xProperty) { - if (xProperty instanceof JavaXMember) { - JavaXMember jx = (JavaXMember)xProperty; - return jx.getMember(); + public static Member getUnderlyingMember(XProperty property) { + if ( property instanceof JavaXMember ) { + JavaXMember member = (JavaXMember) property; + return member.getMember(); } else { throw new org.hibernate.HibernateException( "Can only extract Member from a XProperty which is a JavaXMember" ); } } - public static Member getUnderlyingMember(final JavaXMember jxProperty) { - return jxProperty.getMember(); + public static Member getUnderlyingMember(final JavaXMember member) { + return member.getMember(); } /** - * Locate an annotation on an annotated member, allowing for composed annotations (meta-annotations). + * Return an annotation of the given type which annotates the given + * annotated program element, or which meta-annotates an annotation + * of the given annotated program element. * * @implNote Searches only one level deep */ - public static T findAnnotation(XAnnotatedElement xAnnotatedElement, Class annotationType) { + public static T findAnnotation( + XAnnotatedElement annotatedElement, + Class annotationType) { // first, see if we can find it directly... - final T direct = xAnnotatedElement.getAnnotation( annotationType ); + final T direct = annotatedElement.getAnnotation( annotationType ); if ( direct != null ) { return direct; } // or as composed... - for ( int i = 0; i < xAnnotatedElement.getAnnotations().length; i++ ) { - final Annotation annotation = xAnnotatedElement.getAnnotations()[ i ]; + final Annotation[] annotations = annotatedElement.getAnnotations(); + for ( Annotation annotation : annotations ) { if ( annotationType.equals( annotation.getClass() ) ) { // we would have found this on the direct search, so no need // to check its meta-annotations @@ -149,9 +141,9 @@ public final class HCANNHelper { } // we only check one level deep - final T metaAnn = annotation.annotationType().getAnnotation( annotationType ); - if ( metaAnn != null ) { - return metaAnn; + final T metaAnnotation = annotation.annotationType().getAnnotation( annotationType ); + if ( metaAnnotation != null ) { + return metaAnnotation; } } @@ -159,17 +151,17 @@ public final class HCANNHelper { } /** - * Locate the annotation, relative to `xAnnotatedElement`, which contains - * the passed type of annotation. + * Return an annotation of the given annotated program element which + * is annotated by the given type of meta-annotation. * * @implNote Searches only one level deep */ public static A findContainingAnnotation( - XAnnotatedElement xAnnotatedElement, + XAnnotatedElement annotatedElement, Class annotationType) { - for ( int i = 0; i < xAnnotatedElement.getAnnotations().length; i++ ) { - final Annotation annotation = xAnnotatedElement.getAnnotations()[ i ]; + final Annotation[] annotations = annotatedElement.getAnnotations(); + for ( Annotation annotation : annotations ) { // annotation = @Sequence final T metaAnn = annotation.annotationType().getAnnotation( annotationType );