概述
反射获取内部类
通过Class的getDeclaredClasses来获取
/* Returns an array of {@code Class} objects reflecting all the
* classes and interfaces declared as members of the class represented by
* this {@code Class} object. This includes public, protected, default
* (package) access, and private classes and interfaces declared by the
* class, but excludes inherited classes and interfaces. This method
* returns an array of length 0 if the class declares no classes or
* interfaces as members, or if this {@code Class} object represents a
* primitive type, an array class, or void.
*/
@CallerSensitive
public Class<?>[] getDeclaredClasses() throws SecurityException {
checkMemberAccess(Member.DECLARED, Reflection.getCallerClass(), false);
return getDeclaredClasses0();
}
Spring抽象封装
ClassMetadata#getMemberClassNames
/**
* 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.
* @since 3.1
*/
String[] getMemberClassNames();
底层实现:StandardClassMetadata#getMemberClassNames
本质上还是反射调用Class的getDeclaredClasses方法。
public String[] getMemberClassNames() {
LinkedHashSet<String> memberClassNames = new LinkedHashSet<>(4);
for (Class<?> nestedClass : this.introspectedClass.getDeclaredClasses()) {
memberClassNames.add(nestedClass.getName());
}
return StringUtils.toStringArray(memberClassNames);
}