Skip to content

Commit 7031964

Browse files
philwebbjhoeller
authored andcommitted
Deprecate StandardMetadata constructors
Deprecate the public `StandardMetadata` constructors to make it clearer that these classes should not be instantiated directly. A new `AnnotationMetadata.introspect` factory method has been added which can now be used to obtain instances. This change will allow use to make the constructors package private and drop the `nestedAnnotationsAsMap` parameter in a future release. Closes gh-22906
1 parent 7fbf3f9 commit 7031964

File tree

12 files changed

+53
-19
lines changed

12 files changed

+53
-19
lines changed

spring-beans/src/main/java/org/springframework/beans/factory/annotation/AnnotatedGenericBeanDefinition.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -55,7 +55,7 @@ public class AnnotatedGenericBeanDefinition extends GenericBeanDefinition implem
5555
*/
5656
public AnnotatedGenericBeanDefinition(Class<?> beanClass) {
5757
setBeanClass(beanClass);
58-
this.metadata = new StandardAnnotationMetadata(beanClass, true);
58+
this.metadata = AnnotationMetadata.introspect(beanClass);
5959
}
6060

6161
/**

spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClass.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -29,7 +29,6 @@
2929
import org.springframework.core.io.DescriptiveResource;
3030
import org.springframework.core.io.Resource;
3131
import org.springframework.core.type.AnnotationMetadata;
32-
import org.springframework.core.type.StandardAnnotationMetadata;
3332
import org.springframework.core.type.classreading.MetadataReader;
3433
import org.springframework.lang.Nullable;
3534
import org.springframework.util.Assert;
@@ -104,7 +103,7 @@ public ConfigurationClass(MetadataReader metadataReader, @Nullable Configuration
104103
*/
105104
public ConfigurationClass(Class<?> clazz, String beanName) {
106105
Assert.notNull(beanName, "Bean name must not be null");
107-
this.metadata = new StandardAnnotationMetadata(clazz, true);
106+
this.metadata = AnnotationMetadata.introspect(clazz);
108107
this.resource = new DescriptiveResource(clazz.getName());
109108
this.beanName = beanName;
110109
}
@@ -118,7 +117,7 @@ public ConfigurationClass(Class<?> clazz, String beanName) {
118117
* @since 3.1.1
119118
*/
120119
public ConfigurationClass(Class<?> clazz, @Nullable ConfigurationClass importedBy) {
121-
this.metadata = new StandardAnnotationMetadata(clazz, true);
120+
this.metadata = AnnotationMetadata.introspect(clazz);
122121
this.resource = new DescriptiveResource(clazz.getName());
123122
this.importedBy.add(importedBy);
124123
}

spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassParser.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -913,7 +913,7 @@ private class SourceClass implements Ordered {
913913
public SourceClass(Object source) {
914914
this.source = source;
915915
if (source instanceof Class) {
916-
this.metadata = new StandardAnnotationMetadata((Class<?>) source, true);
916+
this.metadata = AnnotationMetadata.introspect((Class<?>) source);
917917
}
918918
else {
919919
this.metadata = ((MetadataReader) source).getAnnotationMetadata();

spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassUtils.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
import org.springframework.core.annotation.AnnotationUtils;
3737
import org.springframework.core.annotation.Order;
3838
import org.springframework.core.type.AnnotationMetadata;
39-
import org.springframework.core.type.StandardAnnotationMetadata;
4039
import org.springframework.core.type.classreading.MetadataReader;
4140
import org.springframework.core.type.classreading.MetadataReaderFactory;
4241
import org.springframework.lang.Nullable;
@@ -106,7 +105,7 @@ else if (beanDef instanceof AbstractBeanDefinition && ((AbstractBeanDefinition)
106105
EventListenerFactory.class.isAssignableFrom(beanClass)) {
107106
return false;
108107
}
109-
metadata = new StandardAnnotationMetadata(beanClass, true);
108+
metadata = AnnotationMetadata.introspect(beanClass);
110109
}
111110
else {
112111
try {

spring-core/src/main/java/org/springframework/core/type/AnnotationMetadata.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,4 +115,16 @@ default boolean hasAnnotatedMethods(String annotationName) {
115115
*/
116116
Set<MethodMetadata> getAnnotatedMethods(String annotationName);
117117

118+
119+
/**
120+
* Factory method to create a new {@link AnnotationMetadata} instance
121+
* for the given class using standard reflection.
122+
* @param type the class to introspect
123+
* @return a new {@link AnnotationMetadata} instance
124+
* @since 5.2
125+
*/
126+
static AnnotationMetadata introspect(Class<?> type) {
127+
return StandardAnnotationMetadata.from(type);
128+
}
129+
118130
}

spring-core/src/main/java/org/springframework/core/type/StandardAnnotationMetadata.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.springframework.core.annotation.AnnotatedElementUtils;
2727
import org.springframework.core.annotation.AnnotationFilter;
2828
import org.springframework.core.annotation.AnnotationUtils;
29+
import org.springframework.core.annotation.MergedAnnotation;
2930
import org.springframework.core.annotation.MergedAnnotations;
3031
import org.springframework.core.annotation.MergedAnnotations.SearchStrategy;
3132
import org.springframework.core.annotation.RepeatableContainers;
@@ -56,7 +57,9 @@ public class StandardAnnotationMetadata extends StandardClassMetadata implements
5657
* Create a new {@code StandardAnnotationMetadata} wrapper for the given Class.
5758
* @param introspectedClass the Class to introspect
5859
* @see #StandardAnnotationMetadata(Class, boolean)
60+
* @deprecated since 5.2 in favor of the factory method {@link AnnotationMetadata#introspect(Class)}
5961
*/
62+
@Deprecated
6063
public StandardAnnotationMetadata(Class<?> introspectedClass) {
6164
this(introspectedClass, false);
6265
}
@@ -71,7 +74,12 @@ public StandardAnnotationMetadata(Class<?> introspectedClass) {
7174
* {@link org.springframework.core.annotation.AnnotationAttributes} for compatibility
7275
* with ASM-based {@link AnnotationMetadata} implementations
7376
* @since 3.1.1
77+
* @deprecated since 5.2 in favor of the factory method {@link AnnotationMetadata#introspect(Class)}.
78+
* Use {@link MergedAnnotation#asMap(org.springframework.core.annotation.MergedAnnotation.Adapt...) MergedAnnotation.asMap}
79+
* from {@link #getAnnotations()} rather than {@link #getAnnotationAttributes(String)}
80+
* if {@code nestedAnnotationsAsMap} is {@code false}
7481
*/
82+
@Deprecated
7583
public StandardAnnotationMetadata(Class<?> introspectedClass, boolean nestedAnnotationsAsMap) {
7684
super(introspectedClass);
7785
this.mergedAnnotations = MergedAnnotations.from(introspectedClass,
@@ -138,6 +146,7 @@ public boolean hasAnnotatedMethods(String annotationName) {
138146
}
139147

140148
@Override
149+
@SuppressWarnings("deprecation")
141150
public Set<MethodMetadata> getAnnotatedMethods(String annotationName) {
142151
Set<MethodMetadata> annotatedMethods = null;
143152
if (AnnotationUtils.isCandidateClass(getIntrospectedClass(), annotationName)) {
@@ -164,4 +173,9 @@ private boolean isAnnotatedMethod(Method method, String annotationName) {
164173
AnnotatedElementUtils.isAnnotated(method, annotationName);
165174
}
166175

176+
177+
static AnnotationMetadata from(Class<?> introspectedClass) {
178+
return new StandardAnnotationMetadata(introspectedClass, true);
179+
}
180+
167181
}

spring-core/src/main/java/org/springframework/core/type/StandardClassMetadata.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -38,7 +38,9 @@ public class StandardClassMetadata implements ClassMetadata {
3838
/**
3939
* Create a new StandardClassMetadata wrapper for the given Class.
4040
* @param introspectedClass the Class to introspect
41+
* @deprecated since 5.2 in favor of {@link StandardAnnotationMetadata}
4142
*/
43+
@Deprecated
4244
public StandardClassMetadata(Class<?> introspectedClass) {
4345
Assert.notNull(introspectedClass, "Class must not be null");
4446
this.introspectedClass = introspectedClass;

spring-core/src/main/java/org/springframework/core/type/StandardMethodMetadata.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,9 @@ public class StandardMethodMetadata implements MethodMetadata {
5151
/**
5252
* Create a new StandardMethodMetadata wrapper for the given Method.
5353
* @param introspectedMethod the Method to introspect
54+
* @deprecated since 5.2 in favor of obtaining instances via {@link AnnotationMetadata}
5455
*/
56+
@Deprecated
5557
public StandardMethodMetadata(Method introspectedMethod) {
5658
this(introspectedMethod, false);
5759
}
@@ -66,7 +68,9 @@ public StandardMethodMetadata(Method introspectedMethod) {
6668
* {@link org.springframework.core.annotation.AnnotationAttributes} for compatibility
6769
* with ASM-based {@link AnnotationMetadata} implementations
6870
* @since 3.1.1
71+
* @deprecated since 5.2 in favor of obtaining instances via {@link AnnotationMetadata}
6972
*/
73+
@Deprecated
7074
public StandardMethodMetadata(Method introspectedMethod, boolean nestedAnnotationsAsMap) {
7175
Assert.notNull(introspectedMethod, "Method must not be null");
7276
this.introspectedMethod = introspectedMethod;

spring-core/src/test/java/org/springframework/core/type/AnnotationMetadataTests.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public class AnnotationMetadataTests {
5555

5656
@Test
5757
public void standardAnnotationMetadata() {
58-
AnnotationMetadata metadata = new StandardAnnotationMetadata(AnnotatedComponent.class, true);
58+
AnnotationMetadata metadata = AnnotationMetadata.introspect(AnnotatedComponent.class);
5959
doTestAnnotationInfo(metadata);
6060
doTestMethodAnnotationInfo(metadata);
6161
}
@@ -71,7 +71,7 @@ public void asmAnnotationMetadata() throws Exception {
7171

7272
@Test
7373
public void standardAnnotationMetadataForSubclass() {
74-
AnnotationMetadata metadata = new StandardAnnotationMetadata(AnnotatedComponentSubClass.class, true);
74+
AnnotationMetadata metadata = AnnotationMetadata.introspect(AnnotatedComponentSubClass.class);
7575
doTestSubClassAnnotationInfo(metadata);
7676
}
7777

@@ -107,7 +107,7 @@ private void doTestSubClassAnnotationInfo(AnnotationMetadata metadata) {
107107

108108
@Test
109109
public void standardAnnotationMetadataForInterface() {
110-
AnnotationMetadata metadata = new StandardAnnotationMetadata(AnnotationMetadata.class, true);
110+
AnnotationMetadata metadata = AnnotationMetadata.introspect(AnnotationMetadata.class);
111111
doTestMetadataForInterfaceClass(metadata);
112112
}
113113

@@ -135,7 +135,7 @@ private void doTestMetadataForInterfaceClass(AnnotationMetadata metadata) {
135135

136136
@Test
137137
public void standardAnnotationMetadataForAnnotation() {
138-
AnnotationMetadata metadata = new StandardAnnotationMetadata(Component.class, true);
138+
AnnotationMetadata metadata = AnnotationMetadata.introspect(Component.class);
139139
doTestMetadataForAnnotationClass(metadata);
140140
}
141141

@@ -174,6 +174,7 @@ private void doTestMetadataForAnnotationClass(AnnotationMetadata metadata) {
174174
* 'true' as is done in the main test above.
175175
*/
176176
@Test
177+
@Deprecated
177178
public void standardAnnotationMetadata_nestedAnnotationsAsMap_false() {
178179
AnnotationMetadata metadata = new StandardAnnotationMetadata(AnnotatedComponent.class);
179180
AnnotationAttributes specialAttrs = (AnnotationAttributes) metadata.getAnnotationAttributes(SpecialAttr.class.getName());
@@ -182,6 +183,7 @@ public void standardAnnotationMetadata_nestedAnnotationsAsMap_false() {
182183
}
183184

184185
@Test
186+
@Deprecated
185187
public void metaAnnotationOverridesUsingStandardAnnotationMetadata() {
186188
AnnotationMetadata metadata = new StandardAnnotationMetadata(ComposedConfigurationWithAttributeOverridesClass.class);
187189
assertMetaAnnotationOverrides(metadata);
@@ -209,7 +211,7 @@ private void assertMetaAnnotationOverrides(AnnotationMetadata metadata) {
209211

210212
@Test // SPR-11649
211213
public void multipleAnnotationsWithIdenticalAttributeNamesUsingStandardAnnotationMetadata() {
212-
AnnotationMetadata metadata = new StandardAnnotationMetadata(NamedAnnotationsClass.class);
214+
AnnotationMetadata metadata = AnnotationMetadata.introspect(NamedAnnotationsClass.class);
213215
assertMultipleAnnotationsWithIdenticalAttributeNames(metadata);
214216
}
215217

@@ -223,7 +225,7 @@ public void multipleAnnotationsWithIdenticalAttributeNamesUsingAnnotationMetadat
223225

224226
@Test // SPR-11649
225227
public void composedAnnotationWithMetaAnnotationsWithIdenticalAttributeNamesUsingStandardAnnotationMetadata() {
226-
AnnotationMetadata metadata = new StandardAnnotationMetadata(NamedComposedAnnotationClass.class);
228+
AnnotationMetadata metadata = AnnotationMetadata.introspect(NamedComposedAnnotationClass.class);
227229
assertMultipleAnnotationsWithIdenticalAttributeNames(metadata);
228230
}
229231

@@ -237,7 +239,7 @@ public void composedAnnotationWithMetaAnnotationsWithIdenticalAttributeNamesUsin
237239

238240
@Test
239241
public void inheritedAnnotationWithMetaAnnotationsWithIdenticalAttributeNamesUsingStandardAnnotationMetadata() {
240-
AnnotationMetadata metadata = new StandardAnnotationMetadata(NamedComposedAnnotationExtended.class);
242+
AnnotationMetadata metadata = AnnotationMetadata.introspect(NamedComposedAnnotationExtended.class);
241243
assertFalse(metadata.hasAnnotation(NamedComposedAnnotation.class.getName()));
242244
}
243245

spring-core/src/test/java/org/springframework/core/type/StandardAnnotationMetadataTests.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
public class StandardAnnotationMetadataTests extends AbstractAnnotationMetadataTests {
2525

2626
@Override
27+
@SuppressWarnings("deprecation")
2728
protected AnnotationMetadata get(Class<?> source) {
2829
return new StandardAnnotationMetadata(source);
2930
}

spring-core/src/test/java/org/springframework/core/type/StandardClassMetadataMemberClassTests.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2011 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -25,6 +25,7 @@ public class StandardClassMetadataMemberClassTests
2525
extends AbstractClassMetadataMemberClassTests {
2626

2727
@Override
28+
@SuppressWarnings("deprecation")
2829
public ClassMetadata getClassMetadataFor(Class<?> clazz) {
2930
return new StandardClassMetadata(clazz);
3031
}

spring-core/src/test/java/org/springframework/core/type/StandardMethodMetadataTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public class StandardMethodMetadataTests extends AbstractMethodMetadataTests {
2525

2626
@Override
2727
protected AnnotationMetadata get(Class<?> source) {
28-
return new StandardAnnotationMetadata(source);
28+
return AnnotationMetadata.introspect(source);
2929
}
3030

3131
}

0 commit comments

Comments
 (0)