MutableAnnotationVisitor.java 3.9 KB
Newer Older
1
/*
C
Chris Beams 已提交
2
 * Copyright 2002-2009 the original author or authors.
3 4 5 6 7 8 9 10 11 12 13 14 15
 *
 * 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.
 */
C
Chris Beams 已提交
16

17
package org.springframework.context.annotation;
18

19
import static org.springframework.context.annotation.AsmUtils.*;
20 21 22 23

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;

24 25
import org.springframework.asm.AnnotationVisitor;
import org.springframework.asm.Type;
26 27 28 29
import org.springframework.util.Assert;


/**
30 31 32 33 34 35
 * ASM {@link AnnotationVisitor} that populates a given {@link MutableAnnotation} instance
 * with its attributes.
 *
 * @author Chris Beams
 * @see MutableAnnotation
 * @see MutableAnnotationInvocationHandler
36
 * @see AsmUtils#createMutableAnnotation
37 38
 */
class MutableAnnotationVisitor implements AnnotationVisitor {
39 40 41

	protected final MutableAnnotation mutableAnno;

42 43
	private final ClassLoader classLoader;

44 45 46 47 48 49 50 51 52 53
	/**
	 * Creates a new {@link MutableAnnotationVisitor} instance that will populate the the
	 * attributes of the given <var>mutableAnno</var>. Accepts {@link Annotation} instead of
	 * {@link MutableAnnotation} to avoid the need for callers to typecast.
	 * 
	 * @param mutableAnno {@link MutableAnnotation} instance to visit and populate
	 * 
	 * @throws IllegalArgumentException if <var>mutableAnno</var> is not of type
	 *         {@link MutableAnnotation}
	 * 
54
	 * @see AsmUtils#createMutableAnnotation
55
	 */
56
	public MutableAnnotationVisitor(Annotation mutableAnno, ClassLoader classLoader) {
57 58
		Assert.isInstanceOf(MutableAnnotation.class, mutableAnno, "annotation must be mutable");
		this.mutableAnno = (MutableAnnotation) mutableAnno;
59
		this.classLoader = classLoader;
60 61 62
	}

	public AnnotationVisitor visitArray(final String attribName) {
63
		return new MutableAnnotationArrayVisitor(mutableAnno, attribName, classLoader);
64 65 66 67 68 69 70 71 72
	}

	public void visit(String attribName, Object attribValue) {
		Class<?> attribReturnType = mutableAnno.getAttributeType(attribName);

		if (attribReturnType.equals(Class.class)) {
			// the attribute type is Class -> load it and set it.
			String fqClassName = ((Type) attribValue).getClassName();

73
			Class<?> classVal = loadToolingSafeClass(fqClassName, classLoader);
74 75 76 77 78 79 80 81 82 83 84 85 86 87

			if (classVal == null)
				return;

			mutableAnno.setAttributeValue(attribName, classVal);
			return;
		}

		// otherwise, assume the value can be set literally
		mutableAnno.setAttributeValue(attribName, attribValue);
	}

	@SuppressWarnings("unchecked")
	public void visitEnum(String attribName, String enumTypeDescriptor, String strEnumValue) {
88
		String enumClassName = convertAsmTypeDescriptorToClassName(enumTypeDescriptor);
89

90
		Class<? extends Enum> enumClass = loadToolingSafeClass(enumClassName, classLoader);
91 92 93 94 95 96 97 98 99

		if (enumClass == null)
			return;

		Enum enumValue = Enum.valueOf(enumClass, strEnumValue);
		mutableAnno.setAttributeValue(attribName, enumValue);
	}

	public AnnotationVisitor visitAnnotation(String attribName, String attribAnnoTypeDesc) {
100
		String annoTypeName = convertAsmTypeDescriptorToClassName(attribAnnoTypeDesc);
101
		Class<? extends Annotation> annoType = loadToolingSafeClass(annoTypeName, classLoader);
102 103

		if (annoType == null)
104
			return ASM_EMPTY_VISITOR.visitAnnotation(attribName, attribAnnoTypeDesc);
105

106
		Annotation anno = createMutableAnnotation(annoType, classLoader);
107 108 109 110 111 112 113 114

		try {
			Field attribute = mutableAnno.getClass().getField(attribName);
			attribute.set(mutableAnno, anno);
		} catch (Exception ex) {
			throw new RuntimeException(ex);
		}

115
		return new MutableAnnotationVisitor(anno, classLoader);
116 117 118 119
	}

	public void visitEnd() {
	}
120 121

}