revised support for annotated factory methods (merged @FactoryMethod functionality into JavaConfig facility)

This commit is contained in:
Juergen Hoeller
2009-04-19 23:45:31 +00:00
parent 736169aa2a
commit 14bd475519
84 changed files with 2411 additions and 3458 deletions

View File

@@ -138,7 +138,7 @@ public abstract class AnnotationUtils {
}
}
Class<?> superClass = clazz.getSuperclass();
if (superClass == null || superClass == Object.class) {
if (superClass == null || superClass.equals(Object.class)) {
return null;
}
return findAnnotation(superClass, annotationType);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2007 the original author or authors.
* Copyright 2002-2009 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.
@@ -70,8 +70,8 @@ public interface AnnotationMetadata extends ClassMetadata {
* annotation is defined.
*/
Map<String, Object> getAnnotationAttributes(String annotationType);
// TODO return null would be more consistent with other methods if no match is found
/**

View File

@@ -1,8 +1,28 @@
/*
* Copyright 2002-2009 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 org.springframework.core.type;
import java.util.Map;
import java.util.Set;
/**
* @author Mark Pollack
* @since 3.0
*/
public interface MethodMetadata {
int getModifiers();

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2007 the original author or authors.
* Copyright 2002-2009 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.
@@ -43,16 +43,16 @@ public class StandardAnnotationMetadata extends StandardClassMetadata implements
public Set<String> getAnnotationTypes() {
Set<String> types = new HashSet<String>();
Annotation[] anns = getIntrospectedClass().getAnnotations();
for (int i = 0; i < anns.length; i++) {
types.add(anns[i].annotationType().getName());
for (Annotation ann : anns) {
types.add(ann.annotationType().getName());
}
return types;
}
public boolean hasAnnotation(String annotationType) {
Annotation[] anns = getIntrospectedClass().getAnnotations();
for (int i = 0; i < anns.length; i++) {
if (anns[i].annotationType().getName().equals(annotationType)) {
for (Annotation ann : anns) {
if (ann.annotationType().getName().equals(annotationType)) {
return true;
}
}
@@ -61,10 +61,10 @@ public class StandardAnnotationMetadata extends StandardClassMetadata implements
public Set<String> getMetaAnnotationTypes(String annotationType) {
Annotation[] anns = getIntrospectedClass().getAnnotations();
for (int i = 0; i < anns.length; i++) {
if (anns[i].annotationType().getName().equals(annotationType)) {
for (Annotation ann : anns) {
if (ann.annotationType().getName().equals(annotationType)) {
Set<String> types = new HashSet<String>();
Annotation[] metaAnns = anns[i].annotationType().getAnnotations();
Annotation[] metaAnns = ann.annotationType().getAnnotations();
for (Annotation meta : metaAnns) {
types.add(meta.annotationType().getName());
}
@@ -76,8 +76,8 @@ public class StandardAnnotationMetadata extends StandardClassMetadata implements
public boolean hasMetaAnnotation(String annotationType) {
Annotation[] anns = getIntrospectedClass().getAnnotations();
for (int i = 0; i < anns.length; i++) {
Annotation[] metaAnns = anns[i].annotationType().getAnnotations();
for (Annotation ann : anns) {
Annotation[] metaAnns = ann.annotationType().getAnnotations();
for (Annotation meta : metaAnns) {
if (meta.annotationType().getName().equals(annotationType)) {
return true;
@@ -89,8 +89,7 @@ public class StandardAnnotationMetadata extends StandardClassMetadata implements
public Map<String, Object> getAnnotationAttributes(String annotationType) {
Annotation[] anns = getIntrospectedClass().getAnnotations();
for (int i = 0; i < anns.length; i++) {
Annotation ann = anns[i];
for (Annotation ann : anns) {
if (ann.annotationType().getName().equals(annotationType)) {
return AnnotationUtils.getAnnotationAttributes(ann);
}
@@ -101,11 +100,9 @@ public class StandardAnnotationMetadata extends StandardClassMetadata implements
public Set<MethodMetadata> getAnnotatedMethods(String annotationType) {
Method[] methods = getIntrospectedClass().getMethods();
Set<MethodMetadata> annotatedMethods = new LinkedHashSet<MethodMetadata>();
for (int i = 0; i < methods.length; i++) {
Method method = methods[i];
for (Method method : methods) {
Annotation[] methodAnnotations = method.getAnnotations();
for (int j = 0; j < methodAnnotations.length; j++) {
Annotation ann = methodAnnotations[j];
for (Annotation ann : methodAnnotations) {
if (ann.annotationType().getName().equals(annotationType)) {
MethodMetadata mm = new StandardMethodMetadata(method);
annotatedMethods.add(mm);

View File

@@ -1,3 +1,19 @@
/*
* Copyright 2002-2009 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 org.springframework.core.type;
import java.lang.annotation.Annotation;
@@ -9,12 +25,18 @@ import java.util.Map;
import java.util.Set;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.util.Assert;
/**
* @author Mark Pollack
* @since 3.0
*/
public class StandardMethodMetadata implements MethodMetadata {
private final Method introspectedMethod;
public StandardMethodMetadata(Method method) {
Assert.notNull(method, "Method must not be null");
introspectedMethod = method;
}
@@ -25,8 +47,7 @@ public class StandardMethodMetadata implements MethodMetadata {
public Map<String, Object> getAnnotationAttributes(String annotationType) {
Annotation[] anns = getIntrospectedMethod().getAnnotations();
for (int i = 0; i < anns.length; i++) {
Annotation ann = anns[i];
for (Annotation ann : anns) {
if (ann.annotationType().getName().equals(annotationType)) {
return AnnotationUtils.getAnnotationAttributes(ann);
}
@@ -37,14 +58,11 @@ public class StandardMethodMetadata implements MethodMetadata {
public Set<String> getAnnotationTypes() {
Set<String> types = new HashSet<String>();
Annotation[] anns = getIntrospectedMethod().getAnnotations();
for (int i = 0; i < anns.length; i++) {
types.add(anns[i].annotationType().getName());
for (Annotation ann : anns) {
types.add(ann.annotationType().getName());
}
return types;
}
public String getMethodName() {
return introspectedMethod.getName();
@@ -56,8 +74,8 @@ public class StandardMethodMetadata implements MethodMetadata {
public boolean hasAnnotation(String annotationType) {
Annotation[] anns = getIntrospectedMethod().getAnnotations();
for (int i = 0; i < anns.length; i++) {
if (anns[i].annotationType().getName().equals(annotationType)) {
for (Annotation ann : anns) {
if (ann.annotationType().getName().equals(annotationType)) {
return true;
}
}
@@ -70,10 +88,10 @@ public class StandardMethodMetadata implements MethodMetadata {
public Set<String> getMetaAnnotationTypes(String annotationType) {
Annotation[] anns = getIntrospectedMethod().getAnnotations();
for (int i = 0; i < anns.length; i++) {
if (anns[i].annotationType().getName().equals(annotationType)) {
for (Annotation ann : anns) {
if (ann.annotationType().getName().equals(annotationType)) {
Set<String> types = new HashSet<String>();
Annotation[] metaAnns = anns[i].annotationType().getAnnotations();
Annotation[] metaAnns = ann.annotationType().getAnnotations();
for (Annotation meta : metaAnns) {
types.add(meta.annotationType().getName());
}
@@ -83,13 +101,11 @@ public class StandardMethodMetadata implements MethodMetadata {
return null;
}
public boolean hasMetaAnnotation(String metaAnnotationType) {
//TODO can refactor into shared (utility) method with StandardAnnotationMetadata
Annotation[] anns = getIntrospectedMethod().getAnnotations();
for (int i = 0; i < anns.length; i++) {
Annotation[] metaAnns = anns[i].annotationType().getAnnotations();
for (Annotation ann : anns) {
Annotation[] metaAnns = ann.annotationType().getAnnotations();
for (Annotation meta : metaAnns) {
if (meta.annotationType().getName().equals(metaAnnotationType)) {
return true;
@@ -99,8 +115,7 @@ public class StandardMethodMetadata implements MethodMetadata {
return false;
}
public Set<String> getAnnotationTypesWithMetaAnnotation(
String qualifierClassName) {
public Set<String> getAnnotationTypesWithMetaAnnotation(String qualifierClassName) {
// TODO Auto-generated method stub
return null;
}

View File

@@ -17,6 +17,7 @@
package org.springframework.core.type.classreading;
import java.lang.annotation.Annotation;
import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Collection;
@@ -32,6 +33,7 @@ import org.springframework.asm.Type;
import org.springframework.asm.commons.EmptyVisitor;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.core.type.MethodMetadata;
import org.springframework.util.ObjectUtils;
import org.springframework.util.ReflectionUtils;
/**
@@ -59,23 +61,18 @@ class AnnotationMetadataReadingVisitor extends ClassMetadataReadingVisitor imple
}
@Override
public MethodVisitor visitMethod(int access, String name, String desc,
String signature, String[] exceptions) {
public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
MethodMetadataReadingVisitor md = new MethodMetadataReadingVisitor(classLoader, name, access);
methodMetadataSet.add(md);
return md;
}
@Override
public AnnotationVisitor visitAnnotation(final String desc, boolean visible) {
final String className = Type.getType(desc).getClassName();
final Map<String, Object> attributes = new LinkedHashMap<String, Object>();
return new EmptyVisitor() {
@Override
return new AnnotationVisitor() {
public void visit(String name, Object value) {
// Explicitly defined annotation attribute value.
Object valueToUse = value;
@@ -89,7 +86,6 @@ class AnnotationMetadataReadingVisitor extends ClassMetadataReadingVisitor imple
}
attributes.put(name, valueToUse);
}
@Override
public void visitEnum(String name, String desc, String value) {
Object valueToUse = value;
try {
@@ -104,7 +100,36 @@ class AnnotationMetadataReadingVisitor extends ClassMetadataReadingVisitor imple
}
attributes.put(name, valueToUse);
}
@Override
public AnnotationVisitor visitAnnotation(String name, String desc) {
return new EmptyVisitor();
}
public AnnotationVisitor visitArray(final String attrName) {
return new AnnotationVisitor() {
public void visit(String name, Object value) {
Object newValue = value;
Object existingValue = attributes.get(attrName);
if (existingValue != null) {
newValue = ObjectUtils.addObjectToArray((Object[]) existingValue, newValue);
}
else {
Object[] newArray = (Object[]) Array.newInstance(newValue.getClass(), 1);
newArray[0] = newValue;
newValue = newArray;
}
attributes.put(attrName, newValue);
}
public void visitEnum(String name, String desc, String value) {
}
public AnnotationVisitor visitAnnotation(String name, String desc) {
return new EmptyVisitor();
}
public AnnotationVisitor visitArray(String name) {
return new EmptyVisitor();
}
public void visitEnd() {
}
};
}
public void visitEnd() {
try {
Class<?> annotationClass = classLoader.loadClass(className);

View File

@@ -16,7 +16,11 @@
package org.springframework.core.type.classreading;
import org.springframework.asm.ClassAdapter;
import org.springframework.asm.AnnotationVisitor;
import org.springframework.asm.Attribute;
import org.springframework.asm.ClassVisitor;
import org.springframework.asm.FieldVisitor;
import org.springframework.asm.MethodVisitor;
import org.springframework.asm.Opcodes;
import org.springframework.asm.commons.EmptyVisitor;
import org.springframework.core.type.ClassMetadata;
@@ -33,7 +37,7 @@ import org.springframework.util.ClassUtils;
* @author Ramnivas Laddad
* @since 2.5
*/
class ClassMetadataReadingVisitor extends ClassAdapter implements ClassMetadata {
class ClassMetadataReadingVisitor implements ClassVisitor, ClassMetadata {
private String className;
@@ -49,12 +53,7 @@ class ClassMetadataReadingVisitor extends ClassAdapter implements ClassMetadata
private String[] interfaces;
public ClassMetadataReadingVisitor()
{
super(new EmptyVisitor());
}
@Override
public void visit(int version, int access, String name, String signature, String supername, String[] interfaces) {
this.className = ClassUtils.convertResourcePathToClassName(name);
this.isInterface = ((access & Opcodes.ACC_INTERFACE) != 0);
@@ -68,12 +67,10 @@ class ClassMetadataReadingVisitor extends ClassAdapter implements ClassMetadata
}
}
@Override
public void visitOuterClass(String owner, String name, String desc) {
this.enclosingClassName = ClassUtils.convertResourcePathToClassName(owner);
}
@Override
public void visitInnerClass(String name, String outerName, String innerName, int access) {
if (outerName != null && this.className.equals(ClassUtils.convertResourcePathToClassName(name))) {
this.enclosingClassName = ClassUtils.convertResourcePathToClassName(outerName);
@@ -81,6 +78,33 @@ class ClassMetadataReadingVisitor extends ClassAdapter implements ClassMetadata
}
}
public void visitSource(String source, String debug) {
// no-op
}
public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
// no-op
return new EmptyVisitor();
}
public void visitAttribute(Attribute attr) {
// no-op
}
public FieldVisitor visitField(int access, String name, String desc, String signature, Object value) {
// no-op
return new EmptyVisitor();
}
public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
// no-op
return new EmptyVisitor();
}
public void visitEnd() {
// no-op
}
public String getClassName() {
return this.className;

View File

@@ -17,10 +17,10 @@
package org.springframework.core.type.classreading;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Map;
@@ -32,17 +32,26 @@ import org.springframework.asm.Opcodes;
import org.springframework.asm.Type;
import org.springframework.asm.commons.EmptyVisitor;
import org.springframework.core.type.MethodMetadata;
import org.springframework.util.ReflectionUtils;
public class MethodMetadataReadingVisitor extends MethodAdapter implements MethodMetadata {
/**
* @author Mark Pollack
* @since 3.0
*/
class MethodMetadataReadingVisitor extends MethodAdapter implements MethodMetadata {
private ClassLoader classLoader;
private String name;
private int access;
private boolean isStatic;
private final Map<String, Map<String, Object>> attributesMap = new LinkedHashMap<String, Map<String, Object>>();
private final Map<String, Set<String>> metaAnnotationMap = new LinkedHashMap<String, Set<String>>();
private ClassLoader classLoader;
private String name;
private int access;
private boolean isStatic;
public MethodMetadataReadingVisitor(ClassLoader classLoader, String name, int access) {
super(new EmptyVisitor());
@@ -52,6 +61,7 @@ public class MethodMetadataReadingVisitor extends MethodAdapter implements Metho
this.isStatic = ((access & Opcodes.ACC_STATIC) != 0);
}
public Map<String, Object> getAnnotationAttributes(String annotationType) {
return this.attributesMap.get(annotationType);
}
@@ -87,29 +97,22 @@ public class MethodMetadataReadingVisitor extends MethodAdapter implements Metho
}
public boolean isStatic() {
return isStatic;
return this.isStatic;
}
public Set<String> getAnnotationTypesWithMetaAnnotation(String metaAnnotationType) {
///metaAnnotationMap.put(className, metaAnnotationTypeNames);
Set<String> annotationTypes = new LinkedHashSet<String>();
Set< Map.Entry<String, Set<String>> > metaValues = metaAnnotationMap.entrySet();
Iterator<Map.Entry<String, Set<String>> > metaIterator = metaValues.iterator();
while (metaIterator.hasNext())
{
Map.Entry<String, Set<String>> entry = metaIterator.next();
for (Map.Entry<String, Set<String>> entry : metaAnnotationMap.entrySet()) {
String attributeType = entry.getKey();
Set<String> metaAttributes = entry.getValue();
if (metaAttributes.contains(metaAnnotationType))
{
if (metaAttributes.contains(metaAnnotationType)) {
annotationTypes.add(attributeType);
}
}
return annotationTypes;
}
@Override
public AnnotationVisitor visitAnnotation(final String desc, boolean visible) {
final String className = Type.getType(desc).getClassName();
@@ -121,13 +124,27 @@ public class MethodMetadataReadingVisitor extends MethodAdapter implements Metho
attributes.put(name, value);
}
@Override
public void visitEnum(String name, String desc, String value) {
Object valueToUse = value;
try {
Class<?> enumType = classLoader.loadClass(Type.getType(desc).getClassName());
Field enumConstant = ReflectionUtils.findField(enumType, value);
if (enumConstant != null) {
valueToUse = enumConstant.get(null);
}
}
catch (Exception ex) {
// Class not found - can't resolve class reference in annotation attribute.
}
attributes.put(name, valueToUse);
}
@Override
public void visitEnd() {
try {
Class<?> annotationClass = classLoader.loadClass(className);
// Check declared default values of attributes in the annotation type.
Method[] annotationAttributes = annotationClass.getMethods();
for (int i = 0; i < annotationAttributes.length; i++) {
Method annotationAttribute = annotationAttributes[i];
for (Method annotationAttribute : annotationAttributes) {
String attributeName = annotationAttribute.getName();
Object defaultValue = annotationAttribute.getDefaultValue();
if (defaultValue != null && !attributes.containsKey(attributeName)) {
@@ -150,5 +167,4 @@ public class MethodMetadataReadingVisitor extends MethodAdapter implements Metho
};
}
}