@@ -27,7 +27,8 @@ public interface MutableAnnotationTarget extends AnnotationTarget {
27
27
<X extends Annotation > void addAnnotationUsage (AnnotationUsage <X > annotationUsage );
28
28
29
29
/**
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.
31
32
*/
32
33
default <A extends Annotation > MutableAnnotationUsage <A > applyAnnotationUsage (
33
34
AnnotationDescriptor <A > annotationType ,
@@ -36,12 +37,22 @@ default <A extends Annotation> MutableAnnotationUsage<A> applyAnnotationUsage(
36
37
}
37
38
38
39
/**
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.
40
43
*/
41
44
default <A extends Annotation > MutableAnnotationUsage <A > applyAnnotationUsage (
42
45
AnnotationDescriptor <A > annotationType ,
43
46
Consumer <MutableAnnotationUsage <A >> configuration ,
44
47
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
+
45
56
final MutableAnnotationUsage <A > usage = annotationType .createUsage ( this , configuration , buildingContext );
46
57
addAnnotationUsage ( usage );
47
58
return usage ;
0 commit comments