Skip to content

Commit 9ab6c85

Browse files
committed
#57 - Have MutableAnnotationTarget#applyAnnotationUsage operate as get-or-make
1 parent e7e336a commit 9ab6c85

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ public interface MutableAnnotationTarget extends AnnotationTarget {
2727
<X extends Annotation> void addAnnotationUsage(AnnotationUsage<X> annotationUsage);
2828

2929
/**
30-
* Creates a usage and adds it to this target.
30+
* Applies a usage of the given {@code annotationType} to this target. Will return
31+
* an existing usage, if one, or create a new usage.
3132
*/
3233
default <A extends Annotation> MutableAnnotationUsage<A> applyAnnotationUsage(
3334
AnnotationDescriptor<A> annotationType,
@@ -36,12 +37,22 @@ default <A extends Annotation> MutableAnnotationUsage<A> applyAnnotationUsage(
3637
}
3738

3839
/**
39-
* Creates a usage and adds it to this target, allowing for configuration of the created usage
40+
* Applies a usage of the given {@code annotationType} to this target, allowing
41+
* for configuration of the applied usage. Will return an existing usage, if one,
42+
* or create a new usage.
4043
*/
4144
default <A extends Annotation> MutableAnnotationUsage<A> applyAnnotationUsage(
4245
AnnotationDescriptor<A> annotationType,
4346
Consumer<MutableAnnotationUsage<A>> configuration,
4447
SourceModelBuildingContext buildingContext) {
48+
final MutableAnnotationUsage<A> existing = (MutableAnnotationUsage<A>) getAnnotationUsage( annotationType );
49+
if ( existing != null ) {
50+
if ( configuration != null ) {
51+
configuration.accept( existing );
52+
}
53+
return existing;
54+
}
55+
4556
final MutableAnnotationUsage<A> usage = annotationType.createUsage( this, configuration, buildingContext );
4657
addAnnotationUsage( usage );
4758
return usage;

src/test/java/org/hibernate/models/dynamic/SimpleDynamicModelTests.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,32 @@ void testSimpleBasics() {
5050
"TheEntity",
5151
name -> new DynamicClassDetails( name, buildingContext )
5252
);
53-
entityDetails.applyAnnotationUsage( JpaAnnotations.ENTITY, buildingContext );
53+
final MutableAnnotationUsage<Entity> created = entityDetails.applyAnnotationUsage(
54+
JpaAnnotations.ENTITY,
55+
buildingContext
56+
);
57+
final MutableAnnotationUsage<Entity> preExisting = entityDetails.applyAnnotationUsage(
58+
JpaAnnotations.ENTITY,
59+
buildingContext
60+
);
61+
assertThat( created ).isSameAs( preExisting );
5462

5563
entityDetails.applyAttribute(
5664
"id",
5765
integerTypeDetails,
5866
false,
5967
false,
60-
fieldDetails -> fieldDetails.applyAnnotationUsage( JpaAnnotations.ID, buildingContext ),
68+
fieldDetails -> {
69+
final MutableAnnotationUsage<Id> first = fieldDetails.applyAnnotationUsage(
70+
JpaAnnotations.ID,
71+
buildingContext
72+
);
73+
final MutableAnnotationUsage<Id> second = fieldDetails.applyAnnotationUsage(
74+
JpaAnnotations.ID,
75+
buildingContext
76+
);
77+
assertThat( first ).isSameAs( second );
78+
},
6179
buildingContext
6280
);
6381

0 commit comments

Comments
 (0)