Introduce ClassMetadata#getMemberClassNames
ClassMetadata implementations can now introspect their member (nested) classes. This will allow for automatic detection of nested @Configuration types in SPR-8186. Issue: SPR-8358,SPR-8186
This commit is contained in:
@@ -89,9 +89,18 @@ public interface ClassMetadata {
|
||||
String getSuperClassName();
|
||||
|
||||
/**
|
||||
* Return the name of all interfaces that the underlying class
|
||||
* Return the names of all interfaces that the underlying class
|
||||
* implements, or an empty array if there are none.
|
||||
*/
|
||||
String[] getInterfaceNames();
|
||||
|
||||
/**
|
||||
* Return the names of all classes declared as members of the class represented by
|
||||
* this ClassMetadata object. This includes public, protected, default (package)
|
||||
* access, and private classes and interfaces declared by the class, but excludes
|
||||
* inherited classes and interfaces. An empty array is returned if no member classes
|
||||
* or interfaces exist.
|
||||
*/
|
||||
String[] getMemberClassNames();
|
||||
|
||||
}
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
package org.springframework.core.type;
|
||||
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.util.LinkedHashSet;
|
||||
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
@@ -102,4 +103,12 @@ public class StandardClassMetadata implements ClassMetadata {
|
||||
return ifcNames;
|
||||
}
|
||||
|
||||
public String[] getMemberClassNames() {
|
||||
LinkedHashSet<String> memberClassNames = new LinkedHashSet<String>();
|
||||
for (Class<?> nestedClass : this.introspectedClass.getDeclaredClasses()) {
|
||||
memberClassNames.add(nestedClass.getName());
|
||||
}
|
||||
return memberClassNames.toArray(new String[memberClassNames.size()]);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -16,6 +16,9 @@
|
||||
|
||||
package org.springframework.core.type.classreading;
|
||||
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import org.springframework.asm.AnnotationVisitor;
|
||||
import org.springframework.asm.Attribute;
|
||||
import org.springframework.asm.ClassVisitor;
|
||||
@@ -54,7 +57,9 @@ class ClassMetadataReadingVisitor implements ClassVisitor, ClassMetadata {
|
||||
private String superClassName;
|
||||
|
||||
private String[] interfaces;
|
||||
|
||||
|
||||
private Set<String> memberClassNames = new LinkedHashSet<String>();
|
||||
|
||||
|
||||
public void visit(int version, int access, String name, String signature, String supername, String[] interfaces) {
|
||||
this.className = ClassUtils.convertResourcePathToClassName(name);
|
||||
@@ -75,9 +80,16 @@ class ClassMetadataReadingVisitor implements ClassVisitor, ClassMetadata {
|
||||
}
|
||||
|
||||
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);
|
||||
this.independentInnerClass = ((access & Opcodes.ACC_STATIC) != 0);
|
||||
String fqName = ClassUtils.convertResourcePathToClassName(name);
|
||||
String fqOuterName = ClassUtils.convertResourcePathToClassName(outerName);
|
||||
if (outerName != null) {
|
||||
if (this.className.equals(fqName)) {
|
||||
this.enclosingClassName = fqOuterName;
|
||||
this.independentInnerClass = ((access & Opcodes.ACC_STATIC) != 0);
|
||||
}
|
||||
else if (this.className.equals(fqOuterName)) {
|
||||
this.memberClassNames.add(fqName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -153,4 +165,8 @@ class ClassMetadataReadingVisitor implements ClassVisitor, ClassMetadata {
|
||||
return this.interfaces;
|
||||
}
|
||||
|
||||
public String[] getMemberClassNames() {
|
||||
return this.memberClassNames.toArray(new String[this.memberClassNames.size()]);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user