24
24
25
25
import org .springframework .core .SpringProperties ;
26
26
import org .springframework .core .annotation .AnnotatedElementUtils ;
27
- import org .springframework .core .annotation .AnnotationAttributes ;
28
27
import org .springframework .core .annotation .AnnotationUtils ;
29
28
import org .springframework .core .annotation .MergedAnnotation ;
30
29
import org .springframework .core .annotation .MergedAnnotationCollectors ;
@@ -112,7 +111,7 @@ private static <T extends Annotation> T findMergedAnnotation(Class<?> clazz, Cla
112
111
113
112
AnnotationDescriptor <T > descriptor =
114
113
findAnnotationDescriptor (clazz , annotationType , searchEnclosingClass , new HashSet <>());
115
- return (descriptor != null ? descriptor .synthesizeAnnotation () : null );
114
+ return (descriptor != null ? descriptor .getAnnotation () : null );
116
115
}
117
116
118
117
/**
@@ -234,8 +233,7 @@ private static <T extends Annotation> AnnotationDescriptor<T> findAnnotationDesc
234
233
if (!AnnotationUtils .isInJavaLangAnnotationPackage (composedType .getName ()) && visited .add (composedAnn )) {
235
234
descriptor = findAnnotationDescriptor (composedType , annotationType , searchEnclosingClass , visited );
236
235
if (descriptor != null ) {
237
- return new AnnotationDescriptor <>(
238
- clazz , descriptor .getDeclaringClass (), composedAnn , descriptor .getAnnotation ());
236
+ return new AnnotationDescriptor <>(clazz , descriptor .getDeclaringClass (), descriptor .getAnnotation ());
239
237
}
240
238
}
241
239
}
@@ -244,8 +242,7 @@ private static <T extends Annotation> AnnotationDescriptor<T> findAnnotationDesc
244
242
for (Class <?> ifc : clazz .getInterfaces ()) {
245
243
descriptor = findAnnotationDescriptor (ifc , annotationType , searchEnclosingClass , visited );
246
244
if (descriptor != null ) {
247
- return new AnnotationDescriptor <>(clazz , descriptor .getDeclaringClass (),
248
- descriptor .getComposedAnnotation (), descriptor .getAnnotation ());
245
+ return new AnnotationDescriptor <>(clazz , descriptor .getDeclaringClass (), descriptor .getAnnotation ());
249
246
}
250
247
}
251
248
@@ -340,7 +337,7 @@ private static UntypedAnnotationDescriptor findAnnotationDescriptorForTypes(@Nul
340
337
composedAnnotation .annotationType (), annotationTypes , visited );
341
338
if (descriptor != null ) {
342
339
return new UntypedAnnotationDescriptor (clazz , descriptor .getDeclaringClass (),
343
- composedAnnotation , descriptor .getAnnotation (), annotationTypes );
340
+ descriptor .getAnnotation (), annotationTypes );
344
341
}
345
342
}
346
343
}
@@ -350,7 +347,7 @@ private static UntypedAnnotationDescriptor findAnnotationDescriptorForTypes(@Nul
350
347
UntypedAnnotationDescriptor descriptor = findAnnotationDescriptorForTypes (ifc , annotationTypes , visited );
351
348
if (descriptor != null ) {
352
349
return new UntypedAnnotationDescriptor (clazz , descriptor .getDeclaringClass (),
353
- descriptor .getComposedAnnotation (), descriptor . getAnnotation (), annotationTypes );
350
+ descriptor .getAnnotation (), annotationTypes );
354
351
}
355
352
}
356
353
@@ -439,19 +436,16 @@ private static void assertNonEmptyAnnotationTypeArray(Class<?>[] annotationTypes
439
436
/**
440
437
* Descriptor for an {@link Annotation}, including the {@linkplain
441
438
* #getDeclaringClass() class} on which the annotation is <em>declared</em>
442
- * as well as the actual {@linkplain #getAnnotation() annotation} instance.
443
- * <p>If the annotation is used as a meta-annotation, the descriptor also includes
444
- * the {@linkplain #getComposedAnnotation() composed annotation} on which the
445
- * annotation is present. In such cases, the <em>root declaring class</em> is
446
- * not directly annotated with the annotation but rather indirectly via the
447
- * composed annotation.
439
+ * as well as the {@linkplain #getAnnotation() merged annotation} instance.
440
+ * <p>If the annotation is used as a meta-annotation, the <em>root declaring
441
+ * class</em> is not directly annotated with the annotation but rather
442
+ * indirectly via a composed annotation.
448
443
* <p>Given the following example, if we are searching for the {@code @Transactional}
449
444
* annotation <em>on</em> the {@code TransactionalTests} class, then the
450
445
* properties of the {@code AnnotationDescriptor} would be as follows.
451
446
* <ul>
452
447
* <li>rootDeclaringClass: {@code TransactionalTests} class object</li>
453
448
* <li>declaringClass: {@code TransactionalTests} class object</li>
454
- * <li>composedAnnotation: {@code null}</li>
455
449
* <li>annotation: instance of the {@code Transactional} annotation</li>
456
450
* </ul>
457
451
* <p><pre style="code">
@@ -465,7 +459,6 @@ private static void assertNonEmptyAnnotationTypeArray(Class<?>[] annotationTypes
465
459
* <ul>
466
460
* <li>rootDeclaringClass: {@code UserRepositoryTests} class object</li>
467
461
* <li>declaringClass: {@code RepositoryTests} class object</li>
468
- * <li>composedAnnotation: instance of the {@code RepositoryTests} annotation</li>
469
462
* <li>annotation: instance of the {@code Transactional} annotation</li>
470
463
* </ul>
471
464
* <p><pre style="code">
@@ -486,31 +479,23 @@ public static class AnnotationDescriptor<T extends Annotation> {
486
479
487
480
private final Class <?> declaringClass ;
488
481
489
- @ Nullable
490
- private final Annotation composedAnnotation ;
491
-
492
482
private final T annotation ;
493
483
494
- private final AnnotationAttributes annotationAttributes ;
495
-
496
484
AnnotationDescriptor (Class <?> rootDeclaringClass , T annotation ) {
497
- this (rootDeclaringClass , rootDeclaringClass , null , annotation );
485
+ this (rootDeclaringClass , rootDeclaringClass , annotation );
498
486
}
499
487
500
- AnnotationDescriptor (Class <?> rootDeclaringClass , Class <?> declaringClass ,
501
- @ Nullable Annotation composedAnnotation , T annotation ) {
502
-
488
+ @ SuppressWarnings ("unchecked" )
489
+ AnnotationDescriptor (Class <?> rootDeclaringClass , Class <?> declaringClass , T annotation ) {
503
490
Assert .notNull (rootDeclaringClass , "'rootDeclaringClass' must not be null" );
504
491
Assert .notNull (declaringClass , "'declaringClass' must not be null" );
505
492
Assert .notNull (annotation , "Annotation must not be null" );
506
493
this .rootDeclaringClass = rootDeclaringClass ;
507
494
this .declaringClass = declaringClass ;
508
- this .composedAnnotation = composedAnnotation ;
509
- this .annotation = annotation ;
510
- AnnotationAttributes attributes = AnnotatedElementUtils .findMergedAnnotationAttributes (
511
- rootDeclaringClass , annotation .annotationType ().getName (), false , false );
512
- Assert .state (attributes != null , "No annotation attributes" );
513
- this .annotationAttributes = attributes ;
495
+ this .annotation = (T ) AnnotatedElementUtils .findMergedAnnotation (
496
+ rootDeclaringClass , annotation .annotationType ());
497
+ Assert .state (this .annotation != null ,
498
+ () -> "Failed to find merged annotation for " + annotation );
514
499
}
515
500
516
501
public Class <?> getRootDeclaringClass () {
@@ -521,47 +506,22 @@ public Class<?> getDeclaringClass() {
521
506
return this .declaringClass ;
522
507
}
523
508
524
- T getAnnotation () {
525
- return this .annotation ;
526
- }
527
-
528
509
/**
529
- * Synthesize the merged {@link #getAnnotationAttributes AnnotationAttributes}
530
- * in this descriptor back into an annotation of the target
531
- * {@linkplain #getAnnotationType annotation type}.
532
- * @see #getAnnotationAttributes()
533
- * @see #getAnnotationType()
534
- * @see AnnotationUtils#synthesizeAnnotation(java.util.Map, Class, java.lang.reflect.AnnotatedElement)
510
+ * Get the merged annotation for this descriptor.
535
511
*/
536
- public T synthesizeAnnotation () {
537
- return AnnotationUtils .synthesizeAnnotation (
538
- getAnnotationAttributes (), getAnnotationType (), getRootDeclaringClass ());
512
+ public T getAnnotation () {
513
+ return this .annotation ;
539
514
}
540
515
541
516
@ SuppressWarnings ("unchecked" )
542
517
Class <T > getAnnotationType () {
543
518
return (Class <T >) this .annotation .annotationType ();
544
519
}
545
520
546
- AnnotationAttributes getAnnotationAttributes () {
547
- return this .annotationAttributes ;
548
- }
549
-
550
- @ Nullable
551
- Annotation getComposedAnnotation () {
552
- return this .composedAnnotation ;
553
- }
554
-
555
- @ Nullable
556
- Class <? extends Annotation > getComposedAnnotationType () {
557
- return (this .composedAnnotation != null ? this .composedAnnotation .annotationType () : null );
558
- }
559
-
560
521
/**
561
- * Find the next {@link AnnotationDescriptor} for the specified
562
- * {@linkplain #getAnnotationType() annotation type} in the hierarchy
563
- * above the {@linkplain #getRootDeclaringClass() root declaring class}
564
- * of this descriptor.
522
+ * Find the next {@link AnnotationDescriptor} for the specified annotation
523
+ * type in the hierarchy above the {@linkplain #getRootDeclaringClass()
524
+ * root declaring class} of this descriptor.
565
525
* <p>If a corresponding annotation is found in the superclass hierarchy
566
526
* of the root declaring class, that will be returned. Otherwise, an
567
527
* attempt will be made to find a corresponding annotation in the
@@ -583,10 +543,9 @@ public AnnotationDescriptor<T> next() {
583
543
}
584
544
585
545
/**
586
- * Find <strong>all</strong> annotations of the specified
587
- * {@linkplain #getAnnotationType() annotation type} that are present or
588
- * meta-present on the {@linkplain #getRootDeclaringClass() root declaring
589
- * class} of this descriptor.
546
+ * Find <strong>all</strong> annotations of the specified annotation type
547
+ * that are present or meta-present on the {@linkplain #getRootDeclaringClass()
548
+ * root declaring class} of this descriptor.
590
549
* @return the set of all merged, synthesized {@code Annotations} found,
591
550
* or an empty set if none were found
592
551
*/
@@ -609,7 +568,6 @@ public String toString() {
609
568
return new ToStringCreator (this )
610
569
.append ("rootDeclaringClass" , this .rootDeclaringClass .getName ())
611
570
.append ("declaringClass" , this .declaringClass .getName ())
612
- .append ("composedAnnotation" , this .composedAnnotation )
613
571
.append ("annotation" , this .annotation )
614
572
.toString ();
615
573
}
@@ -628,14 +586,13 @@ public static class UntypedAnnotationDescriptor extends AnnotationDescriptor<Ann
628
586
UntypedAnnotationDescriptor (Class <?> rootDeclaringClass , Annotation annotation ,
629
587
Class <? extends Annotation >[] annotationTypes ) {
630
588
631
- this (rootDeclaringClass , rootDeclaringClass , null , annotation , annotationTypes );
589
+ this (rootDeclaringClass , rootDeclaringClass , annotation , annotationTypes );
632
590
}
633
591
634
592
UntypedAnnotationDescriptor (Class <?> rootDeclaringClass , Class <?> declaringClass ,
635
- @ Nullable Annotation composedAnnotation , Annotation annotation ,
636
- Class <? extends Annotation >[] annotationTypes ) {
593
+ Annotation annotation , Class <? extends Annotation >[] annotationTypes ) {
637
594
638
- super (rootDeclaringClass , declaringClass , composedAnnotation , annotation );
595
+ super (rootDeclaringClass , declaringClass , annotation );
639
596
this .annotationTypes = annotationTypes ;
640
597
}
641
598
0 commit comments