src/share/classes/com/sun/tools/javac/comp/Attr.java

changeset 676
bfdfc13fe641
parent 674
584365f256a7
child 689
77cc34d5e548
     1.1 --- a/src/share/classes/com/sun/tools/javac/comp/Attr.java	Tue Sep 07 17:32:52 2010 +0100
     1.2 +++ b/src/share/classes/com/sun/tools/javac/comp/Attr.java	Tue Sep 07 17:33:43 2010 +0100
     1.3 @@ -3201,4 +3201,104 @@
     1.4              super.visitMethodDef(tree);
     1.5          }
     1.6      };
     1.7 +
     1.8 +    // <editor-fold desc="post-attribution visitor">
     1.9 +
    1.10 +    /**
    1.11 +     * Handle missing types/symbols in an AST. This routine is useful when
    1.12 +     * the compiler has encountered some errors (which might have ended up
    1.13 +     * terminating attribution abruptly); if the compiler is used in fail-over
    1.14 +     * mode (e.g. by an IDE) and the AST contains semantic errors, this routine
    1.15 +     * prevents NPE to be progagated during subsequent compilation steps.
    1.16 +     */
    1.17 +    public void postAttr(Env<AttrContext> env) {
    1.18 +        new PostAttrAnalyzer().scan(env.tree);
    1.19 +    }
    1.20 +
    1.21 +    class PostAttrAnalyzer extends TreeScanner {
    1.22 +
    1.23 +        private void initTypeIfNeeded(JCTree that) {
    1.24 +            if (that.type == null) {
    1.25 +                that.type = syms.unknownType;
    1.26 +            }
    1.27 +        }
    1.28 +
    1.29 +        @Override
    1.30 +        public void scan(JCTree tree) {
    1.31 +            if (tree == null) return;
    1.32 +            if (tree instanceof JCExpression) {
    1.33 +                initTypeIfNeeded(tree);
    1.34 +            }
    1.35 +            super.scan(tree);
    1.36 +        }
    1.37 +
    1.38 +        @Override
    1.39 +        public void visitIdent(JCIdent that) {
    1.40 +            if (that.sym == null) {
    1.41 +                that.sym = syms.unknownSymbol;
    1.42 +            }
    1.43 +        }
    1.44 +
    1.45 +        @Override
    1.46 +        public void visitSelect(JCFieldAccess that) {
    1.47 +            if (that.sym == null) {
    1.48 +                that.sym = syms.unknownSymbol;
    1.49 +            }
    1.50 +            super.visitSelect(that);
    1.51 +        }
    1.52 +
    1.53 +        @Override
    1.54 +        public void visitClassDef(JCClassDecl that) {
    1.55 +            initTypeIfNeeded(that);
    1.56 +            if (that.sym == null) {
    1.57 +                that.sym = new ClassSymbol(0, that.name, that.type, syms.noSymbol);
    1.58 +            }
    1.59 +            super.visitClassDef(that);
    1.60 +        }
    1.61 +
    1.62 +        @Override
    1.63 +        public void visitMethodDef(JCMethodDecl that) {
    1.64 +            initTypeIfNeeded(that);
    1.65 +            if (that.sym == null) {
    1.66 +                that.sym = new MethodSymbol(0, that.name, that.type, syms.noSymbol);
    1.67 +            }
    1.68 +            super.visitMethodDef(that);
    1.69 +        }
    1.70 +
    1.71 +        @Override
    1.72 +        public void visitVarDef(JCVariableDecl that) {
    1.73 +            initTypeIfNeeded(that);
    1.74 +            if (that.sym == null) {
    1.75 +                that.sym = new VarSymbol(0, that.name, that.type, syms.noSymbol);
    1.76 +                that.sym.adr = 0;
    1.77 +            }
    1.78 +            super.visitVarDef(that);
    1.79 +        }
    1.80 +
    1.81 +        @Override
    1.82 +        public void visitNewClass(JCNewClass that) {
    1.83 +            if (that.constructor == null) {
    1.84 +                that.constructor = new MethodSymbol(0, names.init, syms.unknownType, syms.noSymbol);
    1.85 +            }
    1.86 +            if (that.constructorType == null) {
    1.87 +                that.constructorType = syms.unknownType;
    1.88 +            }
    1.89 +            super.visitNewClass(that);
    1.90 +        }
    1.91 +
    1.92 +        @Override
    1.93 +        public void visitBinary(JCBinary that) {
    1.94 +            if (that.operator == null)
    1.95 +                that.operator = new OperatorSymbol(names.empty, syms.unknownType, -1, syms.noSymbol);
    1.96 +            super.visitBinary(that);
    1.97 +        }
    1.98 +
    1.99 +        @Override
   1.100 +        public void visitUnary(JCUnary that) {
   1.101 +            if (that.operator == null)
   1.102 +                that.operator = new OperatorSymbol(names.empty, syms.unknownType, -1, syms.noSymbol);
   1.103 +            super.visitUnary(that);
   1.104 +        }
   1.105 +    }
   1.106 +    // </editor-fold>
   1.107  }

mercurial