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
This commit is contained in:
Phillip Webb
2019-05-06 09:46:11 -07:00
committed by Juergen Hoeller
parent 7fbf3f97cd
commit 7031964e49
12 changed files with 53 additions and 19 deletions

View File

@@ -115,4 +115,16 @@ public interface AnnotationMetadata extends ClassMetadata, AnnotatedTypeMetadata
*/
Set<MethodMetadata> getAnnotatedMethods(String annotationName);
/**
* Factory method to create a new {@link AnnotationMetadata} instance
* for the given class using standard reflection.
* @param type the class to introspect
* @return a new {@link AnnotationMetadata} instance
* @since 5.2
*/
static AnnotationMetadata introspect(Class<?> type) {
return StandardAnnotationMetadata.from(type);
}
}

View File

@@ -26,6 +26,7 @@ import java.util.Set;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.core.annotation.AnnotationFilter;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.core.annotation.MergedAnnotation;
import org.springframework.core.annotation.MergedAnnotations;
import org.springframework.core.annotation.MergedAnnotations.SearchStrategy;
import org.springframework.core.annotation.RepeatableContainers;
@@ -56,7 +57,9 @@ public class StandardAnnotationMetadata extends StandardClassMetadata implements
* Create a new {@code StandardAnnotationMetadata} wrapper for the given Class.
* @param introspectedClass the Class to introspect
* @see #StandardAnnotationMetadata(Class, boolean)
* @deprecated since 5.2 in favor of the factory method {@link AnnotationMetadata#introspect(Class)}
*/
@Deprecated
public StandardAnnotationMetadata(Class<?> introspectedClass) {
this(introspectedClass, false);
}
@@ -71,7 +74,12 @@ public class StandardAnnotationMetadata extends StandardClassMetadata implements
* {@link org.springframework.core.annotation.AnnotationAttributes} for compatibility
* with ASM-based {@link AnnotationMetadata} implementations
* @since 3.1.1
* @deprecated since 5.2 in favor of the factory method {@link AnnotationMetadata#introspect(Class)}.
* Use {@link MergedAnnotation#asMap(org.springframework.core.annotation.MergedAnnotation.Adapt...) MergedAnnotation.asMap}
* from {@link #getAnnotations()} rather than {@link #getAnnotationAttributes(String)}
* if {@code nestedAnnotationsAsMap} is {@code false}
*/
@Deprecated
public StandardAnnotationMetadata(Class<?> introspectedClass, boolean nestedAnnotationsAsMap) {
super(introspectedClass);
this.mergedAnnotations = MergedAnnotations.from(introspectedClass,
@@ -138,6 +146,7 @@ public class StandardAnnotationMetadata extends StandardClassMetadata implements
}
@Override
@SuppressWarnings("deprecation")
public Set<MethodMetadata> getAnnotatedMethods(String annotationName) {
Set<MethodMetadata> annotatedMethods = null;
if (AnnotationUtils.isCandidateClass(getIntrospectedClass(), annotationName)) {
@@ -164,4 +173,9 @@ public class StandardAnnotationMetadata extends StandardClassMetadata implements
AnnotatedElementUtils.isAnnotated(method, annotationName);
}
static AnnotationMetadata from(Class<?> introspectedClass) {
return new StandardAnnotationMetadata(introspectedClass, true);
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -38,7 +38,9 @@ public class StandardClassMetadata implements ClassMetadata {
/**
* Create a new StandardClassMetadata wrapper for the given Class.
* @param introspectedClass the Class to introspect
* @deprecated since 5.2 in favor of {@link StandardAnnotationMetadata}
*/
@Deprecated
public StandardClassMetadata(Class<?> introspectedClass) {
Assert.notNull(introspectedClass, "Class must not be null");
this.introspectedClass = introspectedClass;

View File

@@ -51,7 +51,9 @@ public class StandardMethodMetadata implements MethodMetadata {
/**
* Create a new StandardMethodMetadata wrapper for the given Method.
* @param introspectedMethod the Method to introspect
* @deprecated since 5.2 in favor of obtaining instances via {@link AnnotationMetadata}
*/
@Deprecated
public StandardMethodMetadata(Method introspectedMethod) {
this(introspectedMethod, false);
}
@@ -66,7 +68,9 @@ public class StandardMethodMetadata implements MethodMetadata {
* {@link org.springframework.core.annotation.AnnotationAttributes} for compatibility
* with ASM-based {@link AnnotationMetadata} implementations
* @since 3.1.1
* @deprecated since 5.2 in favor of obtaining instances via {@link AnnotationMetadata}
*/
@Deprecated
public StandardMethodMetadata(Method introspectedMethod, boolean nestedAnnotationsAsMap) {
Assert.notNull(introspectedMethod, "Method must not be null");
this.introspectedMethod = introspectedMethod;