1、duplicate declaration checking

/** Check that variable does not hide variable with same name in
     *  immediately enclosing local scope.
     *
     *  e.g
     *  public void m1(boolean a){
     *       int a = 3;
     *       boolean a = true; // 不调用checkTransparentVar()方法,因为调用checkUnique()方法时能测出冲突
     *  }
     *
     *  @param pos           Position for error reporting.
     *  @param v             The symbol.
     *  @param s             The scope.
     */
    void checkTransparentVar(DiagnosticPosition pos, VarSymbol v, Scope s) {
        if (s.next != null) {
            for (Scope.Entry e = s.next.lookup(v.name);
                 e.scope != null && e.sym.owner == v.owner;
                 e = e.next()) {
                if (e.sym.kind == VAR &&
                    (e.sym.owner.kind & (VAR | MTH)) != 0 &&
                    v.name != names.error) {
                    duplicateError(pos, e.sym);
                    return;
                }
            }
        }
    }

e.g

	public void m1(boolean a){
		int a = 3;

		for(int x = 0;x<2;x++){
			int x = 3;
		}
	}

其中int a = 3与int x= 3将调用checkTransparentVar()方法检测出重复声明。

/** Check that a class or interface does not hide a class or
     *  interface with same name in immediately enclosing local scope.
     *  @param pos           Position for error reporting.
     *  @param c             The symbol.
     *  @param s             The scope.
     */
    void checkTransparentClass(DiagnosticPosition pos, ClassSymbol c, Scope s) {
        if (s.next != null) {
            for (Scope.Entry e = s.next.lookup(c.name);
                 e.scope != null && e.sym.owner == c.owner;
                 e = e.next()) {
                if (e.sym.kind == TYP && e.sym.type.tag != TYPEVAR &&
                    (e.sym.owner.kind & (VAR | MTH)) != 0 &&
                    c.name != names.error) {
                    duplicateError(pos, e.sym);
                    return;
                }
            }
        }
    }

e.g

	public void m1() {
		class A{}
		{
			class A{} // err Duplicate nested type A
		}

而对于e.sym.owner.kind为VAR的情况还没有举出例子。  

 /** Check that class does not have the same name as one of
     *  its enclosing classes, or as a class defined in its enclosing scope.
     *  return true if class is unique in its enclosing scope.
     *  @param pos           Position for error reporting.
     *  @param name          The class name.
     *  @param s             The enclosing scope.
     */
    boolean checkUniqueClassName(DiagnosticPosition pos, Name name, Scope s) {
        for (Scope.Entry e = s.lookup(name); e.scope == s; e = e.next()) {
            if (e.sym.kind == TYP && e.sym.name != names.error) {
                duplicateError(pos, e.sym);
                return false;
            }
        }
        for (Symbol sym = s.owner; sym != null; sym = sym.owner) {
            if (sym.kind == TYP && sym.name == name && sym.name != names.error) {
                duplicateError(pos, sym);
                return true;
            }
        }
        return true;
    }

e.g

	{
		class A {}
		interface A{} // A类型的冲突将由checkUniqueClassName()方法来检测
	}
	{
		class B {
			class B {
			}
		}
	}

  

2、Class name generation

 /** Return name of local class.
     *  This is of the form    <enclClass> $ n <classname>
     *  where
     *    enclClass is the flat name of the enclosing class,
     *    classname is the simple name of the local class
     */
    Name localClassName(ClassSymbol c) {
        for (int i=1; ; i++) {
            Name flatname = names.
                fromString("" + c.owner.enclClass().flatname +
                           syntheticNameChar + i +
                           c.name);
            if (compiled.get(flatname) == null)
                return flatname;
        }
    }

e.g

package com.test07;
public class Test3{

	interface I {
	}

	// com.test07.Test3$1
	final I i1 = new I() {

	};

	// com.test07.Test3$2
	final I i2 = new I() {

	};

	{
		// com.test07.Test3$1B
		class B {
		}
	}

	public void m1() {
		// com.test07.Test3$2B
		class B {
		}
		// com.test07.Test3$1C
		class C {
		}
		// com.test07.Test3$3
		final I i3 = new I() {

		};
	}

	public void m2() {
		// com.test07.Test3$3B
		class B {
		}
		// com.test07.Test3$1D
		class D {
		}
	}

}

  

  

3、Type Checking

   /** Check that a given type is assignable to a given proto-type.
     *  If it is, return the type, otherwise return errType.
     *  @param pos        Position to be used for error reporting.
     *  @param found      The type that was found.
     *  @param req        The type that was required.
     */
    Type checkType(DiagnosticPosition pos, Type found, Type req) {
        // 不兼容的类型
        return checkType(pos, found, req, "incompatible.types");
    }

    Type checkType(DiagnosticPosition pos, Type found, Type req, String errKey) {
        if (req.tag == ERROR)
            return req;
        if (found.tag == FORALL)
            return instantiatePoly(pos, (ForAll)found, req, convertWarner(pos, found, req));
        if (req.tag == NONE)
            return found;
        if (types.isAssignable(found, req, convertWarner(pos, found, req)))
            return found;
        if (found.tag <= DOUBLE && req.tag <= DOUBLE)
            // 可能损失精度
            return typeError(pos, diags.fragment("possible.loss.of.precision"), found, req);
        if (found.isSuperBound()) {
            // 从 super-bound 类型{0}进行分配
            log.error(pos, "assignment.from.super-bound", found);
            return types.createErrorType(found);
        }
        if (req.isExtendsBound()) {
            log.error(pos, "assignment.to.extends-bound", req);
            return types.createErrorType(found);
        }
        return typeError(pos, diags.fragment(errKey), found, req);
    }

checkType方法主要是在Attr类中的check()方法调用的,这个方法的代码如下:

 /** Check kind and type of given tree against protokind and prototype.
     *  If check succeeds, store type in tree and return it.
     *  If check fails, store errType in tree and return it.
     *  No checks are performed if the prototype is a method type.
     *  It is not necessary in this case since we know that kind and type
     *  are correct.
     *
     *  @param tree     The tree whose kind and type is checked
     *  @param owntype  The computed type of the tree
     *  @param ownkind  The computed kind of the tree
     *  @param protokind    The expected kind (or: protokind) of the tree
     *  @param prototype       The expected type (or: prototype) of the tree
     */
    Type check(JCTree tree, Type owntype, int ownkind, int protokind, Type prototype) {
    	// prototype不允许为method type
        if (owntype.tag != ERROR && prototype.tag != METHOD && prototype.tag != FORALL) {
            if ((ownkind & ~protokind) == 0) {
                owntype = chk.checkType(tree.pos(), owntype, prototype, errKey);
            } else {
                log.error(tree.pos(), "unexpected.type",kindNames(protokind),kindName(ownkind));
                owntype = types.createErrorType(owntype);
            }
        }
        tree.type = owntype;
        return owntype;
    }

  

owntype与prototype都是Type类型,其tag取值是TypeTags类中的预先定义好的值。

ownkind与protokind都是int类型,其取值是Kinds预先定义好的值。

通过比较如上的两类型值,其实也就是比较了语法节点的Type类型与Symbol类型。

 /** Check that a type is within some bounds.
     *
     *  Used in TypeApply to verify that, e.g., X in V<X> is a valid type argument.
     *  @param pos           Position to be used for error reporting.
     *  @param a             The type that should be bounded by bs.
     *  @param bs            The bound.
     */
    private boolean checkExtends(Type a, Type bound) {
         if (a.isUnbound()) {
             return true;
         } else if (a.tag != WILDCARD) {
             a = types.upperBound(a);
             return types.isSubtype(a, bound);
         } else if (a.isExtendsBound()) {
             Type ut = types.upperBound(a);
             boolean result = types.isCastable(bound, ut , Warner.noWarnings);
             return result;
         } else if (a.isSuperBound()) {
             Type lt = types.lowerBound(a);
             boolean result = !types.notSoftSubtype(lt, bound);
             return result;
         }
         return true;
     }

e.g

class BC<T extends Closeable> {
	public void test() {
		BC<InputStream> a = null;
		BC<? extends InputStream> b = null;
		BC<? super InputStream> c = null;
	}
}

当a.isExtendsBound()时调用了isCastable()方法。

当a.isSuperBound()时调用了notSoftSubtype()方法。

  

  

 

  

Check类之duplicate declaration checking/Class name generation/Type Checking的更多相关文章

  1. 类型检查和鸭子类型 Duck typing in computer programming is an application of the duck test 鸭子测试 鸭子类型 指示编译器将类的类型检查安排在运行时而不是编译时 type checking can be specified to occur at run time rather than compile time.

    Go所提供的面向对象功能十分简洁,但却兼具了类型检查和鸭子类型两者的有点,这是何等优秀的设计啊! Duck typing in computer programming is an applicati ...

  2. qt ISO C++ forbids declaration of 'XXXX' with no type

    error: ISO C++ forbids declaration of 'XXXX' with no type   出现这个错误,一般是由于两个CPP相互都相互包含了对方的头文件造成的,比如: 当 ...

  3. Refused to execute script from '....js' because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled.md

    目录 问题描述 解决过程 总结 问题描述 在整合 Spring Boot.Spring Security.Thymeleaf 的练习中,对页面进行调试时,发现如下错误提示: Refused to ex ...

  4. Dynamic type checking and runtime type information

    动态类型的关键是将动态对象与实际类型信息绑定. See also: Dynamic programming language and Interpreted language Dynamic type ...

  5. 类型检查 Type Checking 一些编程语言并不安全 类型化语言的优点 定型环境 (符号表) 断言的种类

    Compiler http://staff.ustc.edu.cn/~bjhua/courses/compiler/2014/ http://staff.ustc.edu.cn/~bjhua/cour ...

  6. Check类中的incl、union,excl,diff,intersect

    定义一些类,这些类之间有父子关系,如下: class Father{} class Son1 extends Father{} class Son2 extends Father{} class To ...

  7. Check类的validate方法解读

    此方法的实现如下: public void validate(JCTree tree, Env<AttrContext> env, boolean checkRaw) { Validato ...

  8. Check类之TypeValidation

    (1)Validator类的visitTypeApply()方法 实例1: class TestTypeVal<T extends InputStream>{ TestTypeVal< ...

  9. 怎么在eclipse中查到这个类用的是哪个jar的类和Eclipse 编译错误 Access restriction:The type *** is not accessible due to restriction on... 解决方案

    找到了一个办法,你先按F3,然后点击Change Attached Source..按钮,在弹出的框里有个路径,我的路径是D:/SNFWorkSpace/JAR/~importu9.jar,然后你去引 ...

随机推荐

  1. OKHttp概览

    1,整体思路 从使用方法出发,首先是怎么使用,其次是我们使用的功能在内部是如何实现的,实现方案上有什么技巧,有什么范式.全文基本上是对 OkHttp 源码的一个分析与导读,非常建议大家下载 OkHtt ...

  2. STL中list中push_back(对象)保存对象的内部实现

    STL中list中push_back(对象)保存对象的内部实现 1. 在容器中,存放的是对象拷贝 #include<iostream> #include<list> using ...

  3. hdu 4983 欧拉函数

    http://acm.hdu.edu.cn/showproblem.php?pid=4983 求有多少对元组满足题目中的公式. 对于K=1的情况,等价于gcd(A, N) * gcd(B, N) = ...

  4. error: libXpm.(a|so)

    centos 6.5 安装php时老是报错,找了很久答案都是千篇一律且不起作用,最后找到一个答案,特记录在此 脚本: tar zxvf php-5.3.28.tar.gz && cd ...

  5. Oracle EBS标准错误信息如何追踪 (Debug)

    http://www.cnblogs.com/songdavid/articles/2067534.html 调用EBS标准API的时候,可能会返回一些让人看不懂的错误,比如最近我在开发rcv_tra ...

  6. Python学习-38.Python中的正则表达式(二)

    在Python中,正则表达式还有较其他编程语言有特色的地方.那就是支持松散正则表达式了. 在某些情况,正则表达式会写得十分的长,这时候,维护就成问题了.而松散正则表达式就是解决这一问题的办法. 用上一 ...

  7. Windows Phone 放开政策 - 应用内支付(IAP)可加入三方支付

    Windows Phone 应用商店在 今年(2013)11月04号 修改了商店政策 允许公司账户的应用使用三方支付SDK. 通过 App certification requirements cha ...

  8. TempDB--临时表的缓存

    --========================================================================== 在博客园看到一篇文章<SQLServer ...

  9. WebAPI Token 验证

    WebAPI Token 验证 登录端 //HttpContext.Current.Session.Timeout = 10; ////生成Ticket //FormsAuthenticationTi ...

  10. Oracle数据库多行记录转换一行并排序函数

    Oracle数据库多行记录转换一行并排序方法 在ORACLE数据库查询中,我们通常会要求用到将多行记录转换成一行并排序,这时候我们自然会想到Oracle的一个“wx_concat”函数,可以将多行记录 ...