javac的Resolve类解读

 

方法1:isInitializer()

/** An environment is an "initializer" if it is a constructor or
     *  an instance initializer.
     */
    static boolean isInitializer(Env<AttrContext> env) {
        Symbol owner = env.info.scope.owner;
        return owner.isConstructor() ||
                owner.owner.kind == TYP &&
                (owner.kind == VAR || owner.kind == MTH && (owner.flags() & BLOCK) != 0) &&
                (owner.flags() & STATIC) == 0;
    }

 

根据如上方法可以看出检查了如下三种情况的initializer:

 

public class TestInitializer {
	
	TestInitializer() { // constructor
		
	}

	int a = 2;   // instance variable initializer
	
	{  // instance initializer block
		a = 3;
	}
}

 

方法2:isAccessable() 

/** Is class accessible in given environment?
     *  @param env    The current environment.
     *  @param c      The class whose accessibility is checked.
     */
    public boolean isAccessible(Env<AttrContext> env, TypeSymbol c) {
        return isAccessible(env, c, false);
    }

    public boolean isAccessible(Env<AttrContext> env, TypeSymbol c, boolean checkInner) {
        boolean isAccessible = false;
        switch ((short)(c.flags() & AccessFlags)) {
            case PRIVATE:
                isAccessible =   env.enclClass.sym.outermostClass() == c.owner.outermostClass();
                break;
            case 0:
                isAccessible =
                    env.toplevel.packge == c.owner // fast special case
                    ||
                    env.toplevel.packge == c.packge()
                    ||
                    // Hack: this case is added since synthesized default constructors
                    // of anonymous classes should be allowed to access
                    // classes which would be inaccessible otherwise.
                    env.enclMethod != null && (env.enclMethod.mods.flags & ANONCONSTR) != 0; // anonconstr 匿名构造函数
                break;
            default: // error recovery
            case PUBLIC:
                isAccessible = true;
                break;
            case PROTECTED:
                isAccessible =
                    env.toplevel.packge == c.owner // fast special case
                    ||
                    env.toplevel.packge == c.packge()
                    ||
                    isInnerSubClass(env.enclClass.sym, c.owner);
                break;
        }
        return (checkInner == false || c.type.getEnclosingType() == Type.noType) ?
            isAccessible :
            isAccessible && isAccessible(env, c.type.getEnclosingType(), checkInner);
    }  

检查的是TypeSymbol是否在某个Env中被访问到,能否访问到最终还要看Java的权限控制符public、protected、default和private。

另外需要知道的是只有Inner Class才可以为protected和private进行修饰。重点看一下protected修饰的类的逻辑。

public class Test {

	protected class D {
		public D() {
			// TODO Auto-generated constructor stub
		}
	}
}

 

class A { // only public, abstract & final are permitted
	// protected class D{}
}

public class TestAccessible {

	protected class B { // public,protected,private are permitted

	}

	public void method() {
		new A();  // 走env.toplevel.packge == c.owner
		new B();  // 走env.toplevel.packge == c.packge
	}
	
	class C extends Test{
		public void method3(){
			new D(); // 走isInnerSubClass(env.enclClass.sym,c.owner)
		}
	}

}

 继续看isInnerSubClass,如下:

 /** Is given class a subclass of given base class, or an inner class of a subclass?
     *  Return null if no such class exists.
     *  @param c     The class which is the subclass or is contained in it.
     *  @param base  The base class
     */
    private boolean isInnerSubClass(ClassSymbol c, Symbol base) {
        while (c != null && !c.isSubClass(base, types)) {
            c = c.owner.enclClass();
        }
        return c != null;
    }

  

 

方法3:isProtectedAccessible()

/** Is given protected symbol accessible if it is selected from given site
         *  and the selection takes place in given class?
         *  @param sym     The symbol with protected access
         *  @param c       The class where the access takes place
         *  @site          The type of the qualifier
         */
        private  boolean isProtectedAccessible(Symbol sym, ClassSymbol c, Type site) {
            while(  c != null &&
                     !(
                         c.isSubClass(sym.owner, types) &&
                         (c.flags() & INTERFACE) == 0 &&
                         // In JLS 2e 6.6.2.1, the subclass restriction applies only to instance fields and methods
                         // -- types are excluded regardless of whether they are declared 'static' or not.
                         ((sym.flags() & STATIC) != 0 || sym.kind == TYP || site.tsym.isSubClass(c, types))
                     )
             ) {
                c = c.owner.enclClass();
            }
            return c != null;
        }  

 

这个方法中包含了太多的逻辑,有一占需要说明,参数sym的修饰符已经判断为protected,下面分几个步骤来说:

(1)c.isSubClass(sym.owner,types)

类c(获取protected修饰符修饰的sym的类)是否与sym符号所属的符号形成了父子关系,如果是父子关系,当然有访问权限了。

(2)c.flags() & INTERFACE ==0 判断c是否为接口,如果c为接口,那么这个等式不成立。也就说明当c与sym.owner形成父子关系时,如果c为接口,那么有访问的权限。因为接口中所有成员(变量,方法,静态类与非静态类)的访问修饰符为public 

(3) ((sym.flags() & STATIC) != 0 || sym.kind == TYP || site.tsym.isSubClass(c, types))

当protected修饰符修饰的sym为静态成员或者是个类型时,由于c为非接口且c与sym.owner已经形成了父子关系,所以有访问的权限。另外还有最后一种情况,site.tsym.isSubClass(c,types),举个例子:

package com.test05;

import com.test06.SubClass;

public class Temp extends SubClass{
	protected int b = 2;
}

 

package com.test05;

public class ParentClass {	
	protected static int a = 1;
}

  

 

package com.test06;

import com.test05.ParentClass;
import com.test05.Temp;

public class SubClass extends ParentClass{
	class Inner{
		public void test() {
			int y = new Temp().a;
		}
	}
}  

由于Temp与所属的Inner类没有形成父子关系,所以c = c.owner.enclClass(),此时c已经成为了SubClass,而SubClass与符号a的owner(ParentClass)形成了父子关系并且SubClass不为接口,这时候就要判断如上的表达式了。

Temp与SubClass形成了父子关系,当然也就可以访问ParentClass中的protected修饰的成员了。  

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  

 

转载于:https://www.cnblogs.com/extjs4/p/7440643.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值