leaning for Class---2
public native boolean isPrimitive();
?该方法只有八种基本类型的wrapper 类以及void 的wrapper类会返回true.
?
/** * Gets the signers of this class. * * @return the signers of this class, or null if there are no signers. In * particular, this method returns null if this object represents * a primitive type or void. * @since JDK1.1 */ public native Object[] getSigners(); /** * Set the signers of this class. */ native void setSigners(Object[] signers);
?
?
?这2个方法的意思很简单,就是get/set签名,但我 不清楚这2个方法有啥子用途>>>
?
public Method getEnclosingMethod() { EnclosingMethodInfo enclosingInfo = getEnclosingMethodInfo();//得到enclosing类的信息 if (enclosingInfo == null) return null; else { if (!enclosingInfo.isMethod())//排除构造方法 return null; MethodRepository typeInfo = MethodRepository.make(enclosingInfo.getDescriptor(), getFactory()); Class<?> returnType = toClass(typeInfo.getReturnType()); Type [] parameterTypes = typeInfo.getParameterTypes(); Class<?>[] parameterClasses = new Class<?>[parameterTypes.length]; //得到enclosing方法的返回值类型,参数类型 // Convert Types to Classes; returned types *should* // be class objects since the methodDescriptor's used // don't have generics information for(int i = 0; i < parameterClasses.length; i++) parameterClasses[i] = toClass(parameterTypes[i]); /* * Loop over all declared methods; match method name, * number of and type of parameters, *and* return * type. Matching return type is also necessary * because of covariant returns, etc. */ //比对类的返回值,名字,参数等信息,得到相同的method并返回 for(Method m: enclosingInfo.getEnclosingClass().getDeclaredMethods()) { if (m.getName().equals(enclosingInfo.getName()) ) { Class<?>[] candidateParamClasses = m.getParameterTypes(); if (candidateParamClasses.length == parameterClasses.length) { boolean matches = true; for(int i = 0; i < candidateParamClasses.length; i++) { if (!candidateParamClasses[i].equals(parameterClasses[i])) { matches = false; break; } } if (matches) { // finally, check return type if (m.getReturnType().equals(returnType) ) return m; } } } } throw new InternalError("Enclosing method not found"); } }
?
?类似的方法还有下面这个getEnclosingConstructor(),就不多做解释了
?
public Constructor<?> getEnclosingConstructor()
?这些方法的重点在于:?getEnclosingMethodInfo() 方法
?
?
private EnclosingMethodInfo getEnclosingMethodInfo() {Object[] enclosingInfo = getEnclosingMethod0();// 如果方法内没有local class 或者Anonymous class,则enclosingInfo == null,如果有,就新建一个EnclosingMethodInfo 类if (enclosingInfo == null)return null;else {return new EnclosingMethodInfo(enclosingInfo);}}
?
??在看看2个方法
public native Class<?> getDeclaringClass(); //可以发现 getEnclosingClass()方法进一步wrapper了getDeclaringClass //如果得到的enclosingInfo 为null 就调用getDeclaringClass 方法 // 也就是说 getEnclosingClass不仅可以定位非方法内部类的声明位置,也可以定位方法内部类的声明位置 public Class<?> getEnclosingClass() { EnclosingMethodInfo enclosingInfo = getEnclosingMethodInfo(); //如果当前的Class不是方法内部类,则enclosingInfo为null, 否则有值非null。 if (enclosingInfo == null) { // This is a top level or a nested class or an inner class (a, b, or c) //不是方法内部类,直接调用getDeclaringClass() 方法 return getDeclaringClass(); } else { Class<?> enclosingClass = enclosingInfo.getEnclosingClass(); // This is a local class or an anonymous class (d or e) if (enclosingClass == this || enclosingClass == null) throw new InternalError("Malformed enclosing method information"); else //方法内部类,返回 enclosingInfo的属性 enclosingClass,也就是外层包围类 return enclosingClass; } }
?
??
?