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;
        }
    }

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值