From c9b141898828e65cc6cd4fd11783c25acbdf50ff Mon Sep 17 00:00:00 2001 From: John Verhaeg Date: Wed, 19 Sep 2012 05:19:07 -0500 Subject: [PATCH] HHH-7619: Modified ConfiguredClass to only look for type-level annotation for access type --- .../annotations/entity/ConfiguredClass.java | 2 +- .../source/annotations/util/JandexHelper.java | 52 ++++++++++++++++++- 2 files changed, 51 insertions(+), 3 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/internal/source/annotations/entity/ConfiguredClass.java b/hibernate-core/src/main/java/org/hibernate/metamodel/internal/source/annotations/entity/ConfiguredClass.java index b1750e1bc9..172aa21f39 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/internal/source/annotations/entity/ConfiguredClass.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/internal/source/annotations/entity/ConfiguredClass.java @@ -232,7 +232,7 @@ public class ConfiguredClass { // default to the hierarchy access type to start with AccessType accessType = defaultAccessType; - AnnotationInstance accessAnnotation = JandexHelper.getSingleAnnotation( classInfo, JPADotNames.ACCESS ); + AnnotationInstance accessAnnotation = JandexHelper.getSingleAnnotation( classInfo, JPADotNames.ACCESS, ClassInfo.class ); if ( accessAnnotation != null && accessAnnotation.target().getClass().equals( ClassInfo.class ) ) { accessType = JandexHelper.getEnumValue( accessAnnotation, "value", AccessType.class ); } diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/internal/source/annotations/util/JandexHelper.java b/hibernate-core/src/main/java/org/hibernate/metamodel/internal/source/annotations/util/JandexHelper.java index 74f87f78ad..b215a7bbed 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/internal/source/annotations/util/JandexHelper.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/internal/source/annotations/util/JandexHelper.java @@ -29,6 +29,7 @@ import java.io.InputStream; import java.lang.reflect.Array; import java.util.ArrayList; import java.util.HashMap; +import java.util.Iterator; import java.util.List; import java.util.Map; @@ -189,6 +190,41 @@ public class JandexHelper { return getSingleAnnotation( classInfo.annotations(), annotationName ); } + /** + * @param classInfo the class info from which to retrieve the annotation instance + * @param annotationName the annotation to retrieve from the class info + * @param target the annotation target + * + * @return the single annotation defined on the class with the supplied target or {@code null} in case the annotation is not specified at all + * + * @throws org.hibernate.AssertionFailure in case there is there is more than one annotation of this type. + */ + public static AnnotationInstance getSingleAnnotation( + ClassInfo classInfo, + DotName annotationName, + Class< ? extends AnnotationTarget > target ) { + List annotationList = classInfo.annotations().get( annotationName ); + if ( annotationList == null ) { + return null; + } + annotationList = new ArrayList< AnnotationInstance >( annotationList ); + for ( Iterator< AnnotationInstance > iter = annotationList.iterator(); iter.hasNext(); ) { + if ( !target.isInstance( iter.next().target() ) ) { + iter.remove(); + } + } + if ( annotationList.isEmpty() ) { + return null; + } + if ( annotationList.size() == 1 ) { + return annotationList.get( 0 ); + } + throw new AssertionFailure( + "Found more than one instance of the annotation " + + annotationList.get( 0 ).name().toString() + + ". Expected was one." ); + } + /** * @param annotations List of annotation instances keyed against their dot name. * @param annotationName the annotation to retrieve from map @@ -215,6 +251,19 @@ public class JandexHelper { } } + /** + * @param classInfo the class info from which to retrieve the annotation instance + * @param annotationName the annotation to check + * + * @return returns {@code true} if the annotations contains only a single instance of specified annotation or {@code false} + * otherwise. + * + * @throws org.hibernate.AssertionFailure in case there is there is more than one annotation of this type. + */ + public static boolean containsSingleAnnotation( ClassInfo classInfo, DotName annotationName ) { + return containsSingleAnnotation( classInfo.annotations(), annotationName ); + } + /** * @param annotations List of annotation instances keyed against their dot name. * @param annotationName the annotation to check @@ -223,8 +272,7 @@ public class JandexHelper { * * @throws org.hibernate.AssertionFailure in case there is there is more than one annotation of this type. */ - public static boolean containsSingleAnnotations(Map> annotations, DotName annotationName) - throws AssertionFailure { + public static boolean containsSingleAnnotation( Map> annotations, DotName annotationName ) { return getSingleAnnotation( annotations, annotationName ) != null; }