AttrContext

 

info属性类型为AttrContext或AttrContextEnv。主要看AtrContext即可。定义了如下关键参数: 

/** Contains information specific to the attribute and enter
 *  passes, to be used in place of the generic field in environments.
 *
 */
public class AttrContext {

    /** The scope of local symbols.
     */
    public Scope scope = null;

    /** The number of enclosing `static' modifiers.
     */
    public int staticLevel = 0;

    /** Is this an environment for a this(...) or super(...) call?
     */
    public boolean isSelfCall = false;

    /** Are we evaluating the selector of a `super' or type name?
     */
    public boolean selectSuper = false;

    /** Are arguments to current function applications boxed into an array for varargs?
     */
    public boolean varArgs = false;

    /** A list of type variables that are all-quantifed in current context.
     */
    public List typeVars = List.nil();

    /** A record of the lint/SuppressWarnings currently in effect
     */
    public Lint lint;

    /** The variable whose initializer is being attributed
     * useful for detecting self-references in variable initializers
     */
    public Symbol enclVar = null;
   
    // ...
}

获取这个AtrContext的对象可调用dup()方法,如下: 

/** Duplicate this context, replacing scope field and copying all others.
 */
public AttrContext dup(Scope scope) {
    AttrContext info = new AttrContext();
    info.scope = scope;
    info.staticLevel = staticLevel;
    info.isSelfCall = isSelfCall;
    info.selectSuper = selectSuper;
    info.varArgs = varArgs;
    info.typeVars = typeVars;
    info.lint = lint;
    info.enclVar = enclVar;
    return info;
}

调用的地方如下截图。

AttrContext_第1张图片

另外一个dup()方法代码如下:

 /** Duplicate this context, copying all fields.
*/
public AttrContext dup() {
        return dup(scope);
}

调用的地方如下截图。

AttrContext_第2张图片 

 

1、staticLevel属性

AttrContext_第3张图片

只有变量、方法或者静态块时才会对staticLevel进行加1操作,对于静态类时不对此值进行操作。

不能在静态或者非静态方法或者静态/非静态块中出现static修饰的变量与类,如: 

Object o = new Object(){
	// The field b cannot be declared static in a non-static inner type, 
	// unless initialized with a constant expression
	static int b = 2;
};

class InnerD{
	// The member type InnerE cannot be declared static; 
	// static types can only be declared in static or top level types
	static class InnerE{}
}

static{
	static int a = 1;
	static class InnerA{}
}

public void methodA(){
	static int a = 1;
	static class InnerA{}
}

 

2、selectSuper属性

英文注释:Are we evaluating the selector of a `super' or type name?

举例如下:

public class TestScope {	
	class A{
		public void t() throws CloneNotSupportedException{
			TestScope.super.clone();
		}
	}	
}

当在Attr类的visitSelect中对TestScope.super进行标记时,则这个属性会设置为true  

 

3、typeVars属性

这个属性写入的地方如下:

AttrContext_第4张图片

读取值的地方如下:

AttrContext_第5张图片

 

 

 

 

 

 

 

 

  

 

 
 

  

 

 

 

 

 

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(AttrContext)