Skip to content

Commit 73c1f1c

Browse files
committed
#38 - Add AnnotationTarget#getSingleAnnotationUsage
1 parent 1ccd2f4 commit 73c1f1c

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

src/main/java/org/hibernate/models/spi/AnnotationTarget.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,32 @@ default void forAllAnnotationUsages(Consumer<AnnotationUsage<?>> consumer) {
105105
*/
106106
<A extends Annotation> AnnotationUsage<A> getAnnotationUsage(Class<A> type);
107107

108+
/**
109+
* Form of {@linkplain #getAnnotationUsage(AnnotationDescriptor)} which returns {@code null} instead of
110+
* throwing {@linkplain AnnotationAccessException} when more than one usage of the requested
111+
* annotation exists.
112+
*/
113+
default <A extends Annotation> AnnotationUsage<A> getSingleAnnotationUsage(AnnotationDescriptor<A> descriptor) {
114+
try {
115+
return getAnnotationUsage( descriptor );
116+
}
117+
catch (AnnotationAccessException ignore) {
118+
return null;
119+
}
120+
}
121+
122+
/**
123+
* Form of {@link #getSingleAnnotationUsage(AnnotationDescriptor)} accepting the annotation {@linkplain Class}
124+
*/
125+
default <A extends Annotation> AnnotationUsage<A> getSingleAnnotationUsage(Class<A> type) {
126+
try {
127+
return getAnnotationUsage( type );
128+
}
129+
catch (AnnotationAccessException ignore) {
130+
return null;
131+
}
132+
}
133+
108134
/**
109135
* Form of {@linkplain #getAnnotationUsage} which also considers meta-annotations -
110136
* annotations on the classes of each {@linkplain #getAllAnnotationUsages() local annotation}.

src/test/java/org/hibernate/models/annotations/AnnotationUsageTests.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import org.hibernate.annotations.Cache;
66
import org.hibernate.annotations.CacheConcurrencyStrategy;
77
import org.hibernate.internal.util.MutableInteger;
8+
import org.hibernate.models.AnnotationAccessException;
89
import org.hibernate.models.UnknownAnnotationAttributeException;
910
import org.hibernate.models.spi.MutableAnnotationUsage;
1011
import org.hibernate.models.internal.SourceModelBuildingContextImpl;
@@ -369,6 +370,32 @@ void testForEachAnnotation(Index index) {
369370
counter.set( 0 );
370371
classDetails.forEachAnnotationUsage( NamedQuery.class, entityAnnotationUsage -> counter.increment() );
371372
assertThat( counter.get() ).isEqualTo( 2 );
373+
}
374+
375+
@Test
376+
void testGetSingleUsageWithJandex() {
377+
testGetSingleUsage( buildJandexIndex( SimpleEntity.class ) );
378+
}
379+
380+
@Test
381+
void testGetSingleUsageWithoutJandex() {
382+
testGetSingleUsage( null );
383+
}
384+
385+
void testGetSingleUsage(Index index) {
386+
final SourceModelBuildingContextImpl buildingContext = createBuildingContext( index, SimpleEntity.class );
387+
final ClassDetailsRegistry classDetailsRegistry = buildingContext.getClassDetailsRegistry();
388+
final ClassDetails classDetails = classDetailsRegistry.getClassDetails( SimpleEntity.class.getName() );
389+
390+
try {
391+
classDetails.getAnnotationUsage( NamedQuery.class );
392+
fail( "Expecting an AnnotationAccessException to be thrown" );
393+
}
394+
catch (AnnotationAccessException expected) {
395+
// this is expected
396+
}
372397

398+
final AnnotationUsage<NamedQuery> singleAnnotationUsage = classDetails.getSingleAnnotationUsage( NamedQuery.class );
399+
assertThat( singleAnnotationUsage ).isNull();
373400
}
374401
}

0 commit comments

Comments
 (0)