Browse Source

Introduce AnnotationUtils.isSynthesizedAnnotation(Annotation)

Since SynthesizedAnnotation will be deprecated (and potentially
completely removed) in Spring Framework 6.0, this commit introduces
AnnotationUtils.isSynthesizedAnnotation(Annotation) in 5.3.x to allow
people to migrate away from relying on SynthesizedAnnotation.

Closes gh-29054
pull/29132/head
Sam Brannen 3 years ago
parent
commit
6a68bd58f9
  1. 4
      spring-core/src/main/java/org/springframework/core/annotation/AliasFor.java
  2. 4
      spring-core/src/main/java/org/springframework/core/annotation/AnnotationConfigurationException.java
  3. 16
      spring-core/src/main/java/org/springframework/core/annotation/AnnotationUtils.java
  4. 2
      spring-core/src/test/java/org/springframework/core/annotation/AnnotationBackCompatibilityTests.java
  5. 8
      spring-core/src/test/java/org/springframework/core/annotation/MergedAnnotationCollectorsTests.java
  6. 66
      spring-core/src/test/java/org/springframework/core/annotation/MergedAnnotationsTests.java

4
spring-core/src/main/java/org/springframework/core/annotation/AliasFor.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2015 the original author or authors. * Copyright 2002-2022 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -173,7 +173,7 @@ import java.lang.annotation.Target;
* @author Sam Brannen * @author Sam Brannen
* @since 4.2 * @since 4.2
* @see MergedAnnotations * @see MergedAnnotations
* @see SynthesizedAnnotation * @see AnnotationUtils#isSynthesizedAnnotation(Annotation)
*/ */
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD) @Target(ElementType.METHOD)

4
spring-core/src/main/java/org/springframework/core/annotation/AnnotationConfigurationException.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2015 the original author or authors. * Copyright 2002-2022 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -25,7 +25,7 @@ import org.springframework.core.NestedRuntimeException;
* @author Sam Brannen * @author Sam Brannen
* @since 4.2 * @since 4.2
* @see AnnotationUtils * @see AnnotationUtils
* @see SynthesizedAnnotation * @see AnnotationUtils#isSynthesizedAnnotation(java.lang.annotation.Annotation)
*/ */
@SuppressWarnings("serial") @SuppressWarnings("serial")
public class AnnotationConfigurationException extends NestedRuntimeException { public class AnnotationConfigurationException extends NestedRuntimeException {

16
spring-core/src/main/java/org/springframework/core/annotation/AnnotationUtils.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2020 the original author or authors. * Copyright 2002-2022 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -1188,7 +1188,7 @@ public abstract class AnnotationUtils {
public static <A extends Annotation> A synthesizeAnnotation( public static <A extends Annotation> A synthesizeAnnotation(
A annotation, @Nullable AnnotatedElement annotatedElement) { A annotation, @Nullable AnnotatedElement annotatedElement) {
if (annotation instanceof SynthesizedAnnotation || AnnotationFilter.PLAIN.matches(annotation)) { if (isSynthesizedAnnotation(annotation) || AnnotationFilter.PLAIN.matches(annotation)) {
return annotation; return annotation;
} }
return MergedAnnotation.from(annotatedElement, annotation).synthesize(); return MergedAnnotation.from(annotatedElement, annotation).synthesize();
@ -1282,6 +1282,18 @@ public abstract class AnnotationUtils {
return synthesized; return synthesized;
} }
/**
* Determine if the supplied {@link Annotation} has been <em>synthesized</em>
* by Spring (i.e. wrapped in a dynamic proxy) with additional functionality
* such as attribute alias handling.
* @param annotation the annotation to check
* @return {@code true} if the supplied annotation is a synthesized annotation
* @since 5.3.23
*/
public static boolean isSynthesizedAnnotation(@Nullable Annotation annotation) {
return (annotation instanceof SynthesizedAnnotation);
}
/** /**
* Clear the internal annotation metadata cache. * Clear the internal annotation metadata cache.
* @since 4.3.15 * @since 4.3.15

2
spring-core/src/test/java/org/springframework/core/annotation/AnnotationBackCompatibilityTests.java

@ -45,7 +45,7 @@ class AnnotationBackCompatibilityTests {
@Test @Test
void defaultValue() { void defaultValue() {
DefaultValueAnnotation synthesized = MergedAnnotations.from(WithDefaultValue.class).get(DefaultValueAnnotation.class).synthesize(); DefaultValueAnnotation synthesized = MergedAnnotations.from(WithDefaultValue.class).get(DefaultValueAnnotation.class).synthesize();
assertThat(synthesized).isInstanceOf(SynthesizedAnnotation.class); assertThat(AnnotationUtils.isSynthesizedAnnotation(synthesized)).as("synthesized annotation").isTrue();
Object defaultValue = AnnotationUtils.getDefaultValue(synthesized, "enumValue"); Object defaultValue = AnnotationUtils.getDefaultValue(synthesized, "enumValue");
assertThat(defaultValue).isEqualTo(TestEnum.ONE); assertThat(defaultValue).isEqualTo(TestEnum.ONE);
} }

8
spring-core/src/test/java/org/springframework/core/annotation/MergedAnnotationCollectorsTests.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2019 the original author or authors. * Copyright 2002-2022 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -45,7 +45,7 @@ class MergedAnnotationCollectorsTests {
MergedAnnotationCollectors.toAnnotationSet()); MergedAnnotationCollectors.toAnnotationSet());
assertThat(set).isInstanceOf(LinkedHashSet.class).flatExtracting( assertThat(set).isInstanceOf(LinkedHashSet.class).flatExtracting(
TestAnnotation::value).containsExactly("a", "b", "c"); TestAnnotation::value).containsExactly("a", "b", "c");
assertThat(set).allMatch(SynthesizedAnnotation.class::isInstance); assertThat(set).allMatch(AnnotationUtils::isSynthesizedAnnotation);
} }
@Test @Test
@ -55,7 +55,7 @@ class MergedAnnotationCollectorsTests {
assertThat(Arrays.stream(array).map( assertThat(Arrays.stream(array).map(
annotation -> ((TestAnnotation) annotation).value())).containsExactly("a", annotation -> ((TestAnnotation) annotation).value())).containsExactly("a",
"b", "c"); "b", "c");
assertThat(array).allMatch(SynthesizedAnnotation.class::isInstance); assertThat(array).allMatch(AnnotationUtils::isSynthesizedAnnotation);
} }
@Test @Test
@ -64,7 +64,7 @@ class MergedAnnotationCollectorsTests {
MergedAnnotationCollectors.toAnnotationArray(TestAnnotation[]::new)); MergedAnnotationCollectors.toAnnotationArray(TestAnnotation[]::new));
assertThat(Arrays.stream(array).map(TestAnnotation::value)).containsExactly("a", assertThat(Arrays.stream(array).map(TestAnnotation::value)).containsExactly("a",
"b", "c"); "b", "c");
assertThat(array).allMatch(SynthesizedAnnotation.class::isInstance); assertThat(array).allMatch(AnnotationUtils::isSynthesizedAnnotation);
} }
@Test @Test

66
spring-core/src/test/java/org/springframework/core/annotation/MergedAnnotationsTests.java

@ -1395,9 +1395,10 @@ class MergedAnnotationsTests {
RequestMapping synthesizedWebMapping = MergedAnnotation.from(webMapping).synthesize(); RequestMapping synthesizedWebMapping = MergedAnnotation.from(webMapping).synthesize();
RequestMapping synthesizedAgainWebMapping = MergedAnnotation.from(synthesizedWebMapping).synthesize(); RequestMapping synthesizedAgainWebMapping = MergedAnnotation.from(synthesizedWebMapping).synthesize();
assertThat(synthesizedWebMapping).isInstanceOf(SynthesizedAnnotation.class); assertSynthesized(synthesizedWebMapping);
assertThat(synthesizedAgainWebMapping).isInstanceOf(SynthesizedAnnotation.class); assertSynthesized(synthesizedAgainWebMapping);
assertThat(synthesizedWebMapping).isEqualTo(synthesizedAgainWebMapping); assertThat(synthesizedWebMapping).isEqualTo(synthesizedAgainWebMapping);
assertThat(synthesizedWebMapping).isSameAs(synthesizedAgainWebMapping);
assertThat(synthesizedWebMapping.name()).isEqualTo("foo"); assertThat(synthesizedWebMapping.name()).isEqualTo("foo");
assertThat(synthesizedWebMapping.path()).containsExactly("/test"); assertThat(synthesizedWebMapping.path()).containsExactly("/test");
assertThat(synthesizedWebMapping.value()).containsExactly("/test"); assertThat(synthesizedWebMapping.value()).containsExactly("/test");
@ -1412,7 +1413,7 @@ class MergedAnnotationsTests {
Id synthesizedId = MergedAnnotation.from(id).synthesize(); Id synthesizedId = MergedAnnotation.from(id).synthesize();
assertThat(id).isEqualTo(synthesizedId); assertThat(id).isEqualTo(synthesizedId);
// It doesn't make sense to synthesize @Id since it declares zero attributes. // It doesn't make sense to synthesize @Id since it declares zero attributes.
assertThat(synthesizedId).isNotInstanceOf(SynthesizedAnnotation.class); assertNotSynthesized(synthesizedId);
assertThat(id).isSameAs(synthesizedId); assertThat(id).isSameAs(synthesizedId);
GeneratedValue generatedValue = method.getAnnotation(GeneratedValue.class); GeneratedValue generatedValue = method.getAnnotation(GeneratedValue.class);
@ -1420,7 +1421,7 @@ class MergedAnnotationsTests {
GeneratedValue synthesizedGeneratedValue = MergedAnnotation.from(generatedValue).synthesize(); GeneratedValue synthesizedGeneratedValue = MergedAnnotation.from(generatedValue).synthesize();
assertThat(generatedValue).isEqualTo(synthesizedGeneratedValue); assertThat(generatedValue).isEqualTo(synthesizedGeneratedValue);
// It doesn't make sense to synthesize @GeneratedValue since it declares zero attributes with aliases. // It doesn't make sense to synthesize @GeneratedValue since it declares zero attributes with aliases.
assertThat(synthesizedGeneratedValue).isNotInstanceOf(SynthesizedAnnotation.class); assertNotSynthesized(synthesizedGeneratedValue);
assertThat(generatedValue).isSameAs(synthesizedGeneratedValue); assertThat(generatedValue).isSameAs(synthesizedGeneratedValue);
} }
@ -1430,19 +1431,19 @@ class MergedAnnotationsTests {
MergedAnnotations mergedAnnotations = MergedAnnotations.from(directlyAnnotatedField); MergedAnnotations mergedAnnotations = MergedAnnotations.from(directlyAnnotatedField);
RootAnnotation rootAnnotation = mergedAnnotations.get(RootAnnotation.class).synthesize(); RootAnnotation rootAnnotation = mergedAnnotations.get(RootAnnotation.class).synthesize();
assertThat(rootAnnotation.flag()).isFalse(); assertThat(rootAnnotation.flag()).isFalse();
assertThat(rootAnnotation).isNotInstanceOf(SynthesizedAnnotation.class); assertNotSynthesized(rootAnnotation);
Field metaAnnotatedField = ReflectionUtils.findField(DomainType.class, "metaAnnotated"); Field metaAnnotatedField = ReflectionUtils.findField(DomainType.class, "metaAnnotated");
mergedAnnotations = MergedAnnotations.from(metaAnnotatedField); mergedAnnotations = MergedAnnotations.from(metaAnnotatedField);
rootAnnotation = mergedAnnotations.get(RootAnnotation.class).synthesize(); rootAnnotation = mergedAnnotations.get(RootAnnotation.class).synthesize();
assertThat(rootAnnotation.flag()).isTrue(); assertThat(rootAnnotation.flag()).isTrue();
assertThat(rootAnnotation).isInstanceOf(SynthesizedAnnotation.class); assertSynthesized(rootAnnotation);
Field metaMetaAnnotatedField = ReflectionUtils.findField(DomainType.class, "metaMetaAnnotated"); Field metaMetaAnnotatedField = ReflectionUtils.findField(DomainType.class, "metaMetaAnnotated");
mergedAnnotations = MergedAnnotations.from(metaMetaAnnotatedField); mergedAnnotations = MergedAnnotations.from(metaMetaAnnotatedField);
rootAnnotation = mergedAnnotations.get(RootAnnotation.class).synthesize(); rootAnnotation = mergedAnnotations.get(RootAnnotation.class).synthesize();
assertThat(rootAnnotation.flag()).isTrue(); assertThat(rootAnnotation.flag()).isTrue();
assertThat(rootAnnotation).isInstanceOf(SynthesizedAnnotation.class); assertSynthesized(rootAnnotation);
} }
@Test // gh-28704 @Test // gh-28704
@ -1450,10 +1451,10 @@ class MergedAnnotationsTests {
MergedAnnotations mergedAnnotations = MergedAnnotations.from(SecurityConfig.class); MergedAnnotations mergedAnnotations = MergedAnnotations.from(SecurityConfig.class);
EnableWebSecurity enableWebSecurity = mergedAnnotations.get(EnableWebSecurity.class).synthesize(); EnableWebSecurity enableWebSecurity = mergedAnnotations.get(EnableWebSecurity.class).synthesize();
assertThat(enableWebSecurity).isNotInstanceOf(SynthesizedAnnotation.class); assertNotSynthesized(enableWebSecurity);
EnableGlobalAuthentication enableGlobalAuthentication = mergedAnnotations.get(EnableGlobalAuthentication.class).synthesize(); EnableGlobalAuthentication enableGlobalAuthentication = mergedAnnotations.get(EnableGlobalAuthentication.class).synthesize();
assertThat(enableGlobalAuthentication).isNotInstanceOf(SynthesizedAnnotation.class); assertNotSynthesized(enableGlobalAuthentication);
} }
/** /**
@ -1472,8 +1473,8 @@ class MergedAnnotationsTests {
RequestMapping synthesizedWebMapping1 = mergedAnnotation1.synthesize(); RequestMapping synthesizedWebMapping1 = mergedAnnotation1.synthesize();
RequestMapping synthesizedWebMapping2 = MergedAnnotation.from(webMapping).synthesize(); RequestMapping synthesizedWebMapping2 = MergedAnnotation.from(webMapping).synthesize();
assertThat(synthesizedWebMapping1).isInstanceOf(SynthesizedAnnotation.class); assertSynthesized(synthesizedWebMapping1);
assertThat(synthesizedWebMapping2).isInstanceOf(SynthesizedAnnotation.class); assertSynthesized(synthesizedWebMapping2);
assertThat(synthesizedWebMapping1).isEqualTo(synthesizedWebMapping2); assertThat(synthesizedWebMapping1).isEqualTo(synthesizedWebMapping2);
// Synthesizing an annotation from a different MergedAnnotation results in a different synthesized annotation instance. // Synthesizing an annotation from a different MergedAnnotation results in a different synthesized annotation instance.
@ -1595,14 +1596,11 @@ class MergedAnnotationsTests {
testSynthesisWithImplicitAliases(GroovyImplicitAliasesSimpleTestConfigurationClass.class, "groovyScript"); testSynthesisWithImplicitAliases(GroovyImplicitAliasesSimpleTestConfigurationClass.class, "groovyScript");
} }
private void testSynthesisWithImplicitAliases(Class<?> clazz, String expected) private void testSynthesisWithImplicitAliases(Class<?> clazz, String expected) throws Exception {
throws Exception { ImplicitAliasesTestConfiguration config = clazz.getAnnotation(ImplicitAliasesTestConfiguration.class);
ImplicitAliasesTestConfiguration config = clazz.getAnnotation(
ImplicitAliasesTestConfiguration.class);
assertThat(config).isNotNull(); assertThat(config).isNotNull();
ImplicitAliasesTestConfiguration synthesized = MergedAnnotation.from( ImplicitAliasesTestConfiguration synthesized = MergedAnnotation.from(config).synthesize();
config).synthesize(); assertSynthesized(synthesized);
assertThat(synthesized).isInstanceOf(SynthesizedAnnotation.class);
assertThat(synthesized.value()).isEqualTo(expected); assertThat(synthesized.value()).isEqualTo(expected);
assertThat(synthesized.location1()).isEqualTo(expected); assertThat(synthesized.location1()).isEqualTo(expected);
assertThat(synthesized.xmlFile()).isEqualTo(expected); assertThat(synthesized.xmlFile()).isEqualTo(expected);
@ -1630,7 +1628,7 @@ class MergedAnnotationsTests {
assertThat(config).isNotNull(); assertThat(config).isNotNull();
ImplicitAliasesWithImpliedAliasNamesOmittedTestConfiguration synthesized = ImplicitAliasesWithImpliedAliasNamesOmittedTestConfiguration synthesized =
MergedAnnotation.from(config).synthesize(); MergedAnnotation.from(config).synthesize();
assertThat(synthesized).isInstanceOf(SynthesizedAnnotation.class); assertSynthesized(synthesized);
assertThat(synthesized.value()).isEqualTo(expected); assertThat(synthesized.value()).isEqualTo(expected);
assertThat(synthesized.location()).isEqualTo(expected); assertThat(synthesized.location()).isEqualTo(expected);
assertThat(synthesized.xmlFile()).isEqualTo(expected); assertThat(synthesized.xmlFile()).isEqualTo(expected);
@ -1642,7 +1640,7 @@ class MergedAnnotationsTests {
ImplicitAliasesForAliasPairTestConfigurationClass.class.getAnnotation( ImplicitAliasesForAliasPairTestConfigurationClass.class.getAnnotation(
ImplicitAliasesForAliasPairTestConfiguration.class); ImplicitAliasesForAliasPairTestConfiguration.class);
ImplicitAliasesForAliasPairTestConfiguration synthesized = MergedAnnotation.from(config).synthesize(); ImplicitAliasesForAliasPairTestConfiguration synthesized = MergedAnnotation.from(config).synthesize();
assertThat(synthesized).isInstanceOf(SynthesizedAnnotation.class); assertSynthesized(synthesized);
assertThat(synthesized.xmlFile()).isEqualTo("test.xml"); assertThat(synthesized.xmlFile()).isEqualTo("test.xml");
assertThat(synthesized.groovyScript()).isEqualTo("test.xml"); assertThat(synthesized.groovyScript()).isEqualTo("test.xml");
} }
@ -1653,7 +1651,7 @@ class MergedAnnotationsTests {
TransitiveImplicitAliasesTestConfigurationClass.class.getAnnotation( TransitiveImplicitAliasesTestConfigurationClass.class.getAnnotation(
TransitiveImplicitAliasesTestConfiguration.class); TransitiveImplicitAliasesTestConfiguration.class);
TransitiveImplicitAliasesTestConfiguration synthesized = MergedAnnotation.from(config).synthesize(); TransitiveImplicitAliasesTestConfiguration synthesized = MergedAnnotation.from(config).synthesize();
assertThat(synthesized).isInstanceOf(SynthesizedAnnotation.class); assertSynthesized(synthesized);
assertThat(synthesized.xml()).isEqualTo("test.xml"); assertThat(synthesized.xml()).isEqualTo("test.xml");
assertThat(synthesized.groovy()).isEqualTo("test.xml"); assertThat(synthesized.groovy()).isEqualTo("test.xml");
} }
@ -1665,7 +1663,7 @@ class MergedAnnotationsTests {
TransitiveImplicitAliasesForAliasPairTestConfiguration.class); TransitiveImplicitAliasesForAliasPairTestConfiguration.class);
TransitiveImplicitAliasesForAliasPairTestConfiguration synthesized = MergedAnnotation.from( TransitiveImplicitAliasesForAliasPairTestConfiguration synthesized = MergedAnnotation.from(
config).synthesize(); config).synthesize();
assertThat(synthesized).isInstanceOf(SynthesizedAnnotation.class); assertSynthesized(synthesized);
assertThat(synthesized.xml()).isEqualTo("test.xml"); assertThat(synthesized.xml()).isEqualTo("test.xml");
assertThat(synthesized.groovy()).isEqualTo("test.xml"); assertThat(synthesized.groovy()).isEqualTo("test.xml");
} }
@ -1724,7 +1722,7 @@ class MergedAnnotationsTests {
Map<String, Object> map = Collections.singletonMap("value", "webController"); Map<String, Object> map = Collections.singletonMap("value", "webController");
MergedAnnotation<Component> annotation = MergedAnnotation.of(Component.class, map); MergedAnnotation<Component> annotation = MergedAnnotation.of(Component.class, map);
Component synthesizedComponent = annotation.synthesize(); Component synthesizedComponent = annotation.synthesize();
assertThat(synthesizedComponent).isInstanceOf(SynthesizedAnnotation.class); assertSynthesized(synthesizedComponent);
assertThat(synthesizedComponent.value()).isEqualTo("webController"); assertThat(synthesizedComponent.value()).isEqualTo("webController");
} }
@ -1745,7 +1743,7 @@ class MergedAnnotationsTests {
MergedAnnotation<ComponentScanSingleFilter> annotation = MergedAnnotation.of( MergedAnnotation<ComponentScanSingleFilter> annotation = MergedAnnotation.of(
ComponentScanSingleFilter.class, map); ComponentScanSingleFilter.class, map);
ComponentScanSingleFilter synthesizedComponentScan = annotation.synthesize(); ComponentScanSingleFilter synthesizedComponentScan = annotation.synthesize();
assertThat(synthesizedComponentScan).isInstanceOf(SynthesizedAnnotation.class); assertSynthesized(synthesizedComponentScan);
assertThat(synthesizedComponentScan.value().pattern()).isEqualTo("newFoo"); assertThat(synthesizedComponentScan.value().pattern()).isEqualTo("newFoo");
} }
@ -1769,7 +1767,7 @@ class MergedAnnotationsTests {
MergedAnnotation<ComponentScan> annotation = MergedAnnotation.of( MergedAnnotation<ComponentScan> annotation = MergedAnnotation.of(
ComponentScan.class, map); ComponentScan.class, map);
ComponentScan synthesizedComponentScan = annotation.synthesize(); ComponentScan synthesizedComponentScan = annotation.synthesize();
assertThat(synthesizedComponentScan).isInstanceOf(SynthesizedAnnotation.class); assertSynthesized(synthesizedComponentScan);
assertThat(Arrays.stream(synthesizedComponentScan.excludeFilters()).map( assertThat(Arrays.stream(synthesizedComponentScan.excludeFilters()).map(
Filter::pattern)).containsExactly("newFoo", "newBar"); Filter::pattern)).containsExactly("newFoo", "newBar");
} }
@ -1888,7 +1886,7 @@ class MergedAnnotationsTests {
assertThat(component).isNotNull(); assertThat(component).isNotNull();
Map<String, Object> attributes = MergedAnnotation.from(component).asMap(); Map<String, Object> attributes = MergedAnnotation.from(component).asMap();
Component synthesized = MergedAnnotation.of(Component.class, attributes).synthesize(); Component synthesized = MergedAnnotation.of(Component.class, attributes).synthesize();
assertThat(synthesized).isInstanceOf(SynthesizedAnnotation.class); assertSynthesized(synthesized);
assertThat(synthesized).isEqualTo(component); assertThat(synthesized).isEqualTo(component);
} }
@ -2047,7 +2045,7 @@ class MergedAnnotationsTests {
assertThat(annotation).isNotNull(); assertThat(annotation).isNotNull();
MergedAnnotation<Annotation> mergedAnnotation = MergedAnnotation.from(annotation); MergedAnnotation<Annotation> mergedAnnotation = MergedAnnotation.from(annotation);
Annotation synthesizedAnnotation = mergedAnnotation.synthesize(); Annotation synthesizedAnnotation = mergedAnnotation.synthesize();
assertThat(synthesizedAnnotation).isInstanceOf(SynthesizedAnnotation.class); assertSynthesized(synthesizedAnnotation);
assertThat(mergedAnnotation.getString("name")).isEqualTo("test"); assertThat(mergedAnnotation.getString("name")).isEqualTo("test");
assertThat(mergedAnnotation.getString("path")).isEqualTo("/test"); assertThat(mergedAnnotation.getString("path")).isEqualTo("/test");
assertThat(mergedAnnotation.getString("value")).isEqualTo("/test"); assertThat(mergedAnnotation.getString("value")).isEqualTo("/test");
@ -2058,10 +2056,10 @@ class MergedAnnotationsTests {
Hierarchy hierarchy = HierarchyClass.class.getAnnotation(Hierarchy.class); Hierarchy hierarchy = HierarchyClass.class.getAnnotation(Hierarchy.class);
assertThat(hierarchy).isNotNull(); assertThat(hierarchy).isNotNull();
Hierarchy synthesizedHierarchy = MergedAnnotation.from(hierarchy).synthesize(); Hierarchy synthesizedHierarchy = MergedAnnotation.from(hierarchy).synthesize();
assertThat(synthesizedHierarchy).isInstanceOf(SynthesizedAnnotation.class); assertSynthesized(synthesizedHierarchy);
TestConfiguration[] configs = synthesizedHierarchy.value(); TestConfiguration[] configs = synthesizedHierarchy.value();
assertThat(configs).isNotNull(); assertThat(configs).isNotNull();
assertThat(configs).allMatch(SynthesizedAnnotation.class::isInstance); assertThat(configs).allMatch(AnnotationUtils::isSynthesizedAnnotation);
assertThat(configs).extracting(TestConfiguration::value).containsExactly("A", "B"); assertThat(configs).extracting(TestConfiguration::value).containsExactly("A", "B");
assertThat(configs).extracting(TestConfiguration::location).containsExactly("A", "B"); assertThat(configs).extracting(TestConfiguration::location).containsExactly("A", "B");
@ -2082,7 +2080,7 @@ class MergedAnnotationsTests {
assertThat(charsContainer).isNotNull(); assertThat(charsContainer).isNotNull();
CharsContainer synthesizedCharsContainer = MergedAnnotation.from( CharsContainer synthesizedCharsContainer = MergedAnnotation.from(
charsContainer).synthesize(); charsContainer).synthesize();
assertThat(synthesizedCharsContainer).isInstanceOf(SynthesizedAnnotation.class); assertSynthesized(synthesizedCharsContainer);
char[] chars = synthesizedCharsContainer.chars(); char[] chars = synthesizedCharsContainer.chars();
assertThat(chars).containsExactly('x', 'y', 'z'); assertThat(chars).containsExactly('x', 'y', 'z');
// Alter array returned from synthesized annotation // Alter array returned from synthesized annotation
@ -3682,4 +3680,12 @@ class MergedAnnotationsTests {
} }
// @formatter:on // @formatter:on
static void assertSynthesized(Annotation annotation) {
assertThat(AnnotationUtils.isSynthesizedAnnotation(annotation)).as("synthesized annotation").isTrue();
}
static void assertNotSynthesized(Annotation annotation) {
assertThat(AnnotationUtils.isSynthesizedAnnotation(annotation)).as("synthesized annotation").isFalse();
}
} }

Loading…
Cancel
Save