提交 ec781650 编写于 作者: J Juergen Hoeller

Component scanning ignores attributes and meta-annotations on non-public annotations

Issue: SPR-11091
(cherry picked from commit 161819f1)
上级 e4fb72f6
/*
* Copyright 2002-2013 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package example.scannable;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
public class CustomAnnotations {
@Retention(RetentionPolicy.RUNTIME)
private @interface PrivateAnnotation {
String value();
}
@Retention(RetentionPolicy.RUNTIME)
@PrivateAnnotation("special")
public @interface SpecialAnnotation {
}
}
/* /*
* Copyright 2002-2007 the original author or authors. * Copyright 2002-2013 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.
...@@ -28,6 +28,7 @@ import java.lang.annotation.Target; ...@@ -28,6 +28,7 @@ import java.lang.annotation.Target;
@Target(ElementType.TYPE) @Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
@Inherited @Inherited
@CustomAnnotations.SpecialAnnotation
public @interface CustomComponent { public @interface CustomComponent {
} }
/* /*
* Copyright 2002-2012 the original author or authors. * Copyright 2002-2013 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.
...@@ -20,6 +20,7 @@ package example.scannable; ...@@ -20,6 +20,7 @@ package example.scannable;
* @author Mark Fisher * @author Mark Fisher
*/ */
@CustomComponent @CustomComponent
@CustomAnnotations.SpecialAnnotation
public class MessageBean { public class MessageBean {
private String message; private String message;
...@@ -32,6 +33,7 @@ public class MessageBean { ...@@ -32,6 +33,7 @@ public class MessageBean {
this.message = message; this.message = message;
} }
@CustomAnnotations.SpecialAnnotation
public String getMessage() { public String getMessage() {
return this.message; return this.message;
} }
......
...@@ -20,6 +20,7 @@ import java.lang.annotation.Annotation; ...@@ -20,6 +20,7 @@ import java.lang.annotation.Annotation;
import java.lang.reflect.Array; import java.lang.reflect.Array;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
import java.util.List; import java.util.List;
...@@ -28,6 +29,7 @@ import java.util.Set; ...@@ -28,6 +29,7 @@ import java.util.Set;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.springframework.asm.AnnotationVisitor; import org.springframework.asm.AnnotationVisitor;
import org.springframework.asm.SpringAsmInfo; import org.springframework.asm.SpringAsmInfo;
import org.springframework.asm.Type; import org.springframework.asm.Type;
...@@ -36,7 +38,6 @@ import org.springframework.core.annotation.AnnotationUtils; ...@@ -36,7 +38,6 @@ import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.util.ObjectUtils; import org.springframework.util.ObjectUtils;
import org.springframework.util.ReflectionUtils; import org.springframework.util.ReflectionUtils;
/** /**
* @author Chris Beams * @author Chris Beams
* @author Juergen Hoeller * @author Juergen Hoeller
...@@ -169,12 +170,11 @@ class RecursiveAnnotationAttributesVisitor extends AbstractRecursiveAnnotationVi ...@@ -169,12 +170,11 @@ class RecursiveAnnotationAttributesVisitor extends AbstractRecursiveAnnotationVi
public final void visitEnd() { public final void visitEnd() {
try { try {
Class<?> annotationClass = this.classLoader.loadClass(this.annotationType); Class<?> annotationClass = this.classLoader.loadClass(this.annotationType);
this.doVisitEnd(annotationClass); doVisitEnd(annotationClass);
} }
catch (ClassNotFoundException ex) { catch (ClassNotFoundException ex) {
this.logger.debug("Failed to classload type while reading annotation " + this.logger.debug("Failed to class-load type while reading annotation metadata. " +
"metadata. This is a non-fatal error, but certain annotation " + "This is a non-fatal error, but certain annotation metadata may be unavailable.", ex);
"metadata may be unavailable.", ex);
} }
} }
...@@ -183,26 +183,30 @@ class RecursiveAnnotationAttributesVisitor extends AbstractRecursiveAnnotationVi ...@@ -183,26 +183,30 @@ class RecursiveAnnotationAttributesVisitor extends AbstractRecursiveAnnotationVi
} }
private void registerDefaultValues(Class<?> annotationClass) { private void registerDefaultValues(Class<?> annotationClass) {
// Check declared default values of attributes in the annotation type. // Only do further scanning for public annotations; we'd run into IllegalAccessExceptions
Method[] annotationAttributes = annotationClass.getMethods(); // otherwise, and don't want to mess with accessibility in a SecurityManager environment.
for (Method annotationAttribute : annotationAttributes) { if (Modifier.isPublic(annotationClass.getModifiers())) {
String attributeName = annotationAttribute.getName(); // Check declared default values of attributes in the annotation type.
Object defaultValue = annotationAttribute.getDefaultValue(); Method[] annotationAttributes = annotationClass.getMethods();
if (defaultValue != null && !this.attributes.containsKey(attributeName)) { for (Method annotationAttribute : annotationAttributes) {
if (defaultValue instanceof Annotation) { String attributeName = annotationAttribute.getName();
defaultValue = AnnotationAttributes.fromMap( Object defaultValue = annotationAttribute.getDefaultValue();
AnnotationUtils.getAnnotationAttributes((Annotation)defaultValue, false, true)); if (defaultValue != null && !this.attributes.containsKey(attributeName)) {
} if (defaultValue instanceof Annotation) {
else if (defaultValue instanceof Annotation[]) { defaultValue = AnnotationAttributes.fromMap(
Annotation[] realAnnotations = (Annotation[]) defaultValue; AnnotationUtils.getAnnotationAttributes((Annotation) defaultValue, false, true));
AnnotationAttributes[] mappedAnnotations = new AnnotationAttributes[realAnnotations.length]; }
for (int i = 0; i < realAnnotations.length; i++) { else if (defaultValue instanceof Annotation[]) {
mappedAnnotations[i] = AnnotationAttributes.fromMap( Annotation[] realAnnotations = (Annotation[]) defaultValue;
AnnotationUtils.getAnnotationAttributes(realAnnotations[i], false, true)); AnnotationAttributes[] mappedAnnotations = new AnnotationAttributes[realAnnotations.length];
for (int i = 0; i < realAnnotations.length; i++) {
mappedAnnotations[i] = AnnotationAttributes.fromMap(
AnnotationUtils.getAnnotationAttributes(realAnnotations[i], false, true));
}
defaultValue = mappedAnnotations;
} }
defaultValue = mappedAnnotations; this.attributes.put(attributeName, defaultValue);
} }
this.attributes.put(attributeName, defaultValue);
} }
} }
} }
...@@ -251,12 +255,16 @@ final class AnnotationAttributesReadingVisitor extends RecursiveAnnotationAttrib ...@@ -251,12 +255,16 @@ final class AnnotationAttributesReadingVisitor extends RecursiveAnnotationAttrib
Set<String> metaAnnotationTypeNames = new LinkedHashSet<String>(); Set<String> metaAnnotationTypeNames = new LinkedHashSet<String>();
for (Annotation metaAnnotation : annotationClass.getAnnotations()) { for (Annotation metaAnnotation : annotationClass.getAnnotations()) {
metaAnnotationTypeNames.add(metaAnnotation.annotationType().getName()); metaAnnotationTypeNames.add(metaAnnotation.annotationType().getName());
if (!this.attributesMap.containsKey(metaAnnotation.annotationType().getName())) { // Only do further scanning for public annotations; we'd run into IllegalAccessExceptions
this.attributesMap.put(metaAnnotation.annotationType().getName(), // otherwise, and don't want to mess with accessibility in a SecurityManager environment.
AnnotationUtils.getAnnotationAttributes(metaAnnotation, true, true)); if (Modifier.isPublic(metaAnnotation.annotationType().getModifiers())) {
} if (!this.attributesMap.containsKey(metaAnnotation.annotationType().getName())) {
for (Annotation metaMetaAnnotation : metaAnnotation.annotationType().getAnnotations()) { this.attributesMap.put(metaAnnotation.annotationType().getName(),
metaAnnotationTypeNames.add(metaMetaAnnotation.annotationType().getName()); AnnotationUtils.getAnnotationAttributes(metaAnnotation, true, true));
}
for (Annotation metaMetaAnnotation : metaAnnotation.annotationType().getAnnotations()) {
metaAnnotationTypeNames.add(metaMetaAnnotation.annotationType().getName());
}
} }
} }
if (this.metaAnnotationMap != null) { if (this.metaAnnotationMap != null) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册