1. 程式人生 > >Check類的validate方法解讀

Check類的validate方法解讀

png class a it is 有一個 www one inf any subclass

此方法的實現如下:

public void validate(JCTree tree, Env<AttrContext> env, boolean checkRaw) {
        Validator vr = new Validator(env,Check.this);
        vr.validateTree(tree, checkRaw, true);
}

這個方法在Attr中有兩處調用,如下:

技術分享圖片

checkRaw為false表示不用檢查是否為Raw類型。這樣就不會出現這樣的警告 ”找到原始類型: {0} 缺少泛型類{1}的類型參數“

關於Raw類型參見:https://www.cnblogs.com/extjs4/p/9209276.html

另外還有一個方法調用如上方法進行實現,代碼如下:

public  void validate(JCTree tree, Env<AttrContext> env) {
        validate(tree, env, true);
} 

Validate a type expression. That is,check that all type arguments of a parametric type are within

their bounds. This must be done in a second phase after type attribution


since a class might have a subclass as type parameter bound. E.g:

class B<A extends C> { ... }
class C extends B<C> { ... }

and we can‘t treeMaker sure that the bound is already attributed because of possible cycles.
AttrVisitor method: Validate a type expression, if it is not null, catching and reporting any completion failures.

調用這個方法的地方如下截圖。

技術分享圖片

還有另外的地方調用這個方法,代碼如下:

 /** AttrVisitor method: Validate a list of type expressions.
*/
public void validate(List<? extends JCTree> trees, Env<AttrContext> env) {
        for (List<? extends JCTree> l = trees; l.nonEmpty(); l = l.tail) {
            validate(l.head, env);
        }
}

這個方法調用地方的截圖如下:

技術分享圖片

  

  

  

Check類的validate方法解讀