org.springframework.core.type.classreading.SimpleAnnotationMetadata 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.util.Collections;
import java.util.LinkedHashSet;
import java.util.Set;
import org.springframework.asm.Opcodes;
import org.springframework.core.annotation.MergedAnnotations;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.core.type.MethodMetadata;
import org.springframework.lang.Nullable;
import org.springframework.util.StringUtils;
/**
* {@link AnnotationMetadata} created from a
* {@link SimpleAnnotationMetadataReadingVisitor}.
*
* @author Phillip Webb
* @author Sam Brannen
* @author Juergen Hoeller
* @since 5.2
*/
final class SimpleAnnotationMetadata implements AnnotationMetadata {
private final String className;
private final int access;
@Nullable
private final String enclosingClassName;
@Nullable
private final String superClassName;
private final boolean independentInnerClass;
private final Set interfaceNames;
private final Set memberClassNames;
private final Set declaredMethods;
private final MergedAnnotations annotations;
@Nullable
private Set annotationTypes;
SimpleAnnotationMetadata(String className, int access, @Nullable String enclosingClassName,
@Nullable String superClassName, boolean independentInnerClass, Set interfaceNames,
Set memberClassNames, Set declaredMethods, MergedAnnotations annotations) {
this.className = className;
this.access = access;
this.enclosingClassName = enclosingClassName;
this.superClassName = superClassName;
this.independentInnerClass = independentInnerClass;
this.interfaceNames = interfaceNames;
this.memberClassNames = memberClassNames;
this.declaredMethods = declaredMethods;
this.annotations = annotations;
}
@Override
public String getClassName() {
return this.className;
}
@Override
public boolean isInterface() {
return (this.access & Opcodes.ACC_INTERFACE) != 0;
}
@Override
public boolean isAnnotation() {
return (this.access & Opcodes.ACC_ANNOTATION) != 0;
}
@Override
public boolean isAbstract() {
return (this.access & Opcodes.ACC_ABSTRACT) != 0;
}
@Override
public boolean isFinal() {
return (this.access & Opcodes.ACC_FINAL) != 0;
}
@Override
public boolean isIndependent() {
return (this.enclosingClassName == null || this.independentInnerClass);
}
@Override
@Nullable
public String getEnclosingClassName() {
return this.enclosingClassName;
}
@Override
@Nullable
public String getSuperClassName() {
return this.superClassName;
}
@Override
public String[] getInterfaceNames() {
return StringUtils.toStringArray(this.interfaceNames);
}
@Override
public String[] getMemberClassNames() {
return StringUtils.toStringArray(this.memberClassNames);
}
@Override
public MergedAnnotations getAnnotations() {
return this.annotations;
}
@Override
public Set getAnnotationTypes() {
Set annotationTypes = this.annotationTypes;
if (annotationTypes == null) {
annotationTypes = Collections.unmodifiableSet(
AnnotationMetadata.super.getAnnotationTypes());
this.annotationTypes = annotationTypes;
}
return annotationTypes;
}
@Override
public Set getAnnotatedMethods(String annotationName) {
Set result = new LinkedHashSet<>(4);
for (MethodMetadata annotatedMethod : this.declaredMethods) {
if (annotatedMethod.isAnnotated(annotationName)) {
result.add(annotatedMethod);
}
}
return Collections.unmodifiableSet(result);
}
@Override
public Set getDeclaredMethods() {
return Collections.unmodifiableSet(this.declaredMethods);
}
@Override
public boolean equals(@Nullable Object obj) {
return (this == obj || (obj instanceof SimpleAnnotationMetadata that && this.className.equals(that.className)));
}
@Override
public int hashCode() {
return this.className.hashCode();
}
@Override
public String toString() {
return this.className;
}
}