如果class对象是local class,是在类B的方法里定义的,同样返回null值,而不会返回类B。 ```java /** * If the class or interface represented by this {@code Class} object * is a member of another class, returns the {@code Class} object * representing the class in which it was declared. This method returns * null if this class or interface is not a member of any other class. If * this {@code Class} object represents an array class, a primitive * type, or void,then this method returns null. * @return the declaring class for this class * @throws SecurityException * If a security manager, s, is present and the caller's * class loader is not the same as or an ancestor of the class * loader for the declaring class and invocation of {@link * SecurityManager#checkPackageAccess s.checkPackageAccess()} * denies access to the package of the declaring class * @since JDK1.1 */ @CallerSensitive public Class getDeclaringClass() throws SecurityException { final Class candidate = getDeclaringClass0();
if (candidate != null) candidate.checkPackageAccess( ClassLoader.getClassLoader(Reflection.getCallerClass()), true); return candidate; }
@CallerSensitive public Class getEnclosingClass() throws SecurityException { EnclosingMethodInfo enclosingInfo = getEnclosingMethodInfo(); Class enclosingCandidate;
if (enclosingInfo == null) { // This is a top level or a nested class or an inner class (a, b, or c) enclosingCandidate = getDeclaringClass(); } else { Class enclosingClass = enclosingInfo.getEnclosingClass(); // This is a local class or an anonymous class (d or e) if (enclosingClass == this || enclosingClass == null) thrownew InternalError("Malformed enclosing method information"); else enclosingCandidate = enclosingClass; }
if (enclosingCandidate != null) enclosingCandidate.checkPackageAccess( ClassLoader.getClassLoader(Reflection.getCallerClass()), true); return enclosingCandidate; }
Class d = D.class; System.out.println(d.getClass()); System.out.println(d.getEnclosingClass()); System.out.println(d.getDeclaringClass()); System.out.println(Arrays.toString(d.getDeclaredClasses())); }
class java.lang.Class null null [interface com.calebzhao.openfeign.ClassTest$C, class com.calebzhao.openfeign.ClassTest$B, class com.calebzhao.openfeign.ClassTest$A]
*************A*******************
class java.lang.Class class com.calebzhao.openfeign.ClassTest class com.calebzhao.openfeign.ClassTest []
*************B*******************
class java.lang.Class class com.calebzhao.openfeign.ClassTest class com.calebzhao.openfeign.ClassTest []
*************C*******************
class java.lang.Class class com.calebzhao.openfeign.ClassTest null []
*************D***********
class java.lang.Class class com.calebzhao.openfeign.ClassTest null []
public String getSimpleName(){ if (isArray()) return getComponentType().getSimpleName()+"[]";
String simpleName = getSimpleBinaryName(); if (simpleName == null) { // top level class simpleName = getName(); return simpleName.substring(simpleName.lastIndexOf(".")+1); // strip the package name } int length = simpleName.length(); if (length < 1 || simpleName.charAt(0) != '$') thrownew InternalError("Malformed class name"); int index = 1; while (index < length && isAsciiDigit(simpleName.charAt(index))) index++; // Eventually, this is the empty string iff this is an anonymous class return simpleName.substring(index); }
java
1 2 3 4 5 6 7 8 9 10 11
private String getSimpleBinaryName(){ Class enclosingClass = getEnclosingClass(); if (enclosingClass == null) // top level class returnnull; // Otherwise, strip the enclosing class' name try { return getName().substring(enclosingClass.getName().length()); } catch (IndexOutOfBoundsException ex) { thrownew InternalError("Malformed class name", ex); } }