org.springframework.core.type.classreading.MergedAnnotationReadingVisitor Maven / Gradle / Ivy
/*
* Copyright 2002-2023 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
*
* https://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.classreading;
import java.lang.annotation.Annotation;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import org.springframework.asm.AnnotationVisitor;
import org.springframework.asm.SpringAsmInfo;
import org.springframework.asm.Type;
import org.springframework.core.annotation.AnnotationFilter;
import org.springframework.core.annotation.MergedAnnotation;
import org.springframework.lang.Nullable;
import org.springframework.util.ClassUtils;
/**
* {@link AnnotationVisitor} that can be used to construct a
* {@link MergedAnnotation}.
*
* @author Phillip Webb
* @since 5.2
* @param the annotation type
*/
class MergedAnnotationReadingVisitor extends AnnotationVisitor {
@Nullable
private final ClassLoader classLoader;
@Nullable
private final Object source;
private final Class annotationType;
private final Consumer> consumer;
private final Map attributes = new LinkedHashMap<>(4);
public MergedAnnotationReadingVisitor(@Nullable ClassLoader classLoader, @Nullable Object source,
Class annotationType, Consumer> consumer) {
super(SpringAsmInfo.ASM_VERSION);
this.classLoader = classLoader;
this.source = source;
this.annotationType = annotationType;
this.consumer = consumer;
}
@Override
public void visit(String name, Object value) {
if (value instanceof Type type) {
value = type.getClassName();
}
this.attributes.put(name, value);
}
@Override
public void visitEnum(String name, String descriptor, String value) {
visitEnum(descriptor, value, enumValue -> this.attributes.put(name, enumValue));
}
@Override
@Nullable
public AnnotationVisitor visitAnnotation(String name, String descriptor) {
return visitAnnotation(descriptor, annotation -> this.attributes.put(name, annotation));
}
@Override
public AnnotationVisitor visitArray(String name) {
return new ArrayVisitor(value -> this.attributes.put(name, value));
}
@Override
public void visitEnd() {
Map compactedAttributes =
(this.attributes.size() == 0 ? Collections.emptyMap() : this.attributes);
MergedAnnotation annotation = MergedAnnotation.of(
this.classLoader, this.source, this.annotationType, compactedAttributes);
this.consumer.accept(annotation);
}
@SuppressWarnings("unchecked")
public > void visitEnum(String descriptor, String value, Consumer consumer) {
String className = Type.getType(descriptor).getClassName();
Class type = (Class) ClassUtils.resolveClassName(className, this.classLoader);
consumer.accept(Enum.valueOf(type, value));
}
@SuppressWarnings("unchecked")
@Nullable
private AnnotationVisitor visitAnnotation(
String descriptor, Consumer> consumer) {
String className = Type.getType(descriptor).getClassName();
if (AnnotationFilter.PLAIN.matches(className)) {
return null;
}
Class type = (Class) ClassUtils.resolveClassName(className, this.classLoader);
return new MergedAnnotationReadingVisitor<>(this.classLoader, this.source, type, consumer);
}
@SuppressWarnings("unchecked")
@Nullable
static AnnotationVisitor get(@Nullable ClassLoader classLoader,
@Nullable Object source, String descriptor, boolean visible,
Consumer> consumer) {
if (!visible) {
return null;
}
String typeName = Type.getType(descriptor).getClassName();
if (AnnotationFilter.PLAIN.matches(typeName)) {
return null;
}
try {
Class annotationType = (Class) ClassUtils.forName(typeName, classLoader);
return new MergedAnnotationReadingVisitor<>(classLoader, source, annotationType, consumer);
}
catch (ClassNotFoundException | LinkageError ex) {
return null;
}
}
/**
* {@link AnnotationVisitor} to deal with array attributes.
*/
private class ArrayVisitor extends AnnotationVisitor {
private final List