minor cleanups to HCANNHelper

This commit is contained in:
Gavin 2022-12-31 16:37:57 +01:00
parent 327342b39e
commit a1d52b0bb1

View File

@ -22,6 +22,7 @@
*/ */
@Internal @Internal
public final class HCANNHelper { public final class HCANNHelper {
public static boolean hasAnnotation( public static boolean hasAnnotation(
AnnotatedElement element, AnnotatedElement element,
Class<? extends Annotation> annotationToCheck) { Class<? extends Annotation> annotationToCheck) {
@ -29,12 +30,7 @@ public static boolean hasAnnotation(
return false; return false;
} }
//noinspection RedundantIfStatement return element.isAnnotationPresent( annotationToCheck );
if ( element.isAnnotationPresent( annotationToCheck ) ) {
return true;
}
return false;
} }
public static boolean hasAnnotation( public static boolean hasAnnotation(
@ -46,7 +42,7 @@ public static boolean hasAnnotation(
} }
return element.isAnnotationPresent( annotationToCheck ) return element.isAnnotationPresent( annotationToCheck )
|| element.isAnnotationPresent( annotationToCheck2 ); || element.isAnnotationPresent( annotationToCheck2 );
} }
public static boolean hasAnnotation( public static boolean hasAnnotation(
@ -56,12 +52,7 @@ public static boolean hasAnnotation(
return false; return false;
} }
//noinspection RedundantIfStatement return element.isAnnotationPresent( annotationToCheck );
if ( element.isAnnotationPresent( annotationToCheck ) ) {
return true;
}
return false;
} }
public static boolean hasAnnotation( public static boolean hasAnnotation(
@ -72,16 +63,13 @@ public static boolean hasAnnotation(
return false; return false;
} }
//noinspection RedundantIfStatement return element.isAnnotationPresent( annotationToCheck )
if ( element.isAnnotationPresent( annotationToCheck ) || element.isAnnotationPresent( annotationToCheck2);
|| element.isAnnotationPresent( annotationToCheck2 ) ) {
return true;
}
return false;
} }
public static boolean hasAnnotation(XAnnotatedElement element, Class<? extends Annotation>... annotationsToCheck) { public static boolean hasAnnotation(
XAnnotatedElement element,
Class<? extends Annotation>... annotationsToCheck) {
assert annotationsToCheck != null && annotationsToCheck.length > 0; assert annotationsToCheck != null && annotationsToCheck.length > 0;
if ( element == null ) { if ( element == null ) {
@ -101,47 +89,51 @@ public static boolean hasAnnotation(XAnnotatedElement element, Class<? extends A
* @deprecated Prefer using {@link #annotatedElementSignature(JavaXMember)} * @deprecated Prefer using {@link #annotatedElementSignature(JavaXMember)}
*/ */
@Deprecated @Deprecated
public static String annotatedElementSignature(XProperty xProperty) { public static String annotatedElementSignature(XProperty property) {
return getUnderlyingMember( xProperty ).toString(); return getUnderlyingMember( property ).toString();
} }
public static String annotatedElementSignature(final JavaXMember jxProperty) { public static String annotatedElementSignature(final JavaXMember member) {
return getUnderlyingMember( jxProperty ).toString(); return getUnderlyingMember( member ).toString();
} }
/** /**
* @deprecated Prefer using {@link #getUnderlyingMember(JavaXMember)} * @deprecated Prefer using {@link #getUnderlyingMember(JavaXMember)}
*/ */
@Deprecated @Deprecated
public static Member getUnderlyingMember(XProperty xProperty) { public static Member getUnderlyingMember(XProperty property) {
if (xProperty instanceof JavaXMember) { if ( property instanceof JavaXMember ) {
JavaXMember jx = (JavaXMember)xProperty; JavaXMember member = (JavaXMember) property;
return jx.getMember(); return member.getMember();
} }
else { else {
throw new org.hibernate.HibernateException( "Can only extract Member from a XProperty which is a JavaXMember" ); throw new org.hibernate.HibernateException( "Can only extract Member from a XProperty which is a JavaXMember" );
} }
} }
public static Member getUnderlyingMember(final JavaXMember jxProperty) { public static Member getUnderlyingMember(final JavaXMember member) {
return jxProperty.getMember(); 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 * @implNote Searches only one level deep
*/ */
public static <T extends Annotation> T findAnnotation(XAnnotatedElement xAnnotatedElement, Class<T> annotationType) { public static <T extends Annotation> T findAnnotation(
XAnnotatedElement annotatedElement,
Class<T> annotationType) {
// first, see if we can find it directly... // first, see if we can find it directly...
final T direct = xAnnotatedElement.getAnnotation( annotationType ); final T direct = annotatedElement.getAnnotation( annotationType );
if ( direct != null ) { if ( direct != null ) {
return direct; return direct;
} }
// or as composed... // or as composed...
for ( int i = 0; i < xAnnotatedElement.getAnnotations().length; i++ ) { final Annotation[] annotations = annotatedElement.getAnnotations();
final Annotation annotation = xAnnotatedElement.getAnnotations()[ i ]; for ( Annotation annotation : annotations ) {
if ( annotationType.equals( annotation.getClass() ) ) { if ( annotationType.equals( annotation.getClass() ) ) {
// we would have found this on the direct search, so no need // we would have found this on the direct search, so no need
// to check its meta-annotations // to check its meta-annotations
@ -149,9 +141,9 @@ public static <T extends Annotation> T findAnnotation(XAnnotatedElement xAnnotat
} }
// we only check one level deep // we only check one level deep
final T metaAnn = annotation.annotationType().getAnnotation( annotationType ); final T metaAnnotation = annotation.annotationType().getAnnotation( annotationType );
if ( metaAnn != null ) { if ( metaAnnotation != null ) {
return metaAnn; return metaAnnotation;
} }
} }
@ -159,17 +151,17 @@ public static <T extends Annotation> T findAnnotation(XAnnotatedElement xAnnotat
} }
/** /**
* Locate the annotation, relative to `xAnnotatedElement`, which contains * Return an annotation of the given annotated program element which
* the passed type of annotation. * is annotated by the given type of meta-annotation.
* *
* @implNote Searches only one level deep * @implNote Searches only one level deep
*/ */
public static <A extends Annotation, T extends Annotation> A findContainingAnnotation( public static <A extends Annotation, T extends Annotation> A findContainingAnnotation(
XAnnotatedElement xAnnotatedElement, XAnnotatedElement annotatedElement,
Class<T> annotationType) { Class<T> annotationType) {
for ( int i = 0; i < xAnnotatedElement.getAnnotations().length; i++ ) { final Annotation[] annotations = annotatedElement.getAnnotations();
final Annotation annotation = xAnnotatedElement.getAnnotations()[ i ]; for ( Annotation annotation : annotations ) {
// annotation = @Sequence // annotation = @Sequence
final T metaAnn = annotation.annotationType().getAnnotation( annotationType ); final T metaAnn = annotation.annotationType().getAnnotation( annotationType );