langtools/test/tools/javac/api/6471599/Main.java
changeset 10 06bc494ca11e
child 5520 86e4b9a9da40
equal deleted inserted replaced
0:fd16c54261b3 10:06bc494ca11e
       
     1 /*
       
     2  * Copyright 2006 Sun Microsystems, Inc.  All Rights Reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.
       
     8  *
       
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    12  * version 2 for more details (a copy is included in the LICENSE file that
       
    13  * accompanied this code).
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License version
       
    16  * 2 along with this work; if not, write to the Free Software Foundation,
       
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    18  *
       
    19  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    20  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    21  * have any questions.
       
    22  */
       
    23 
       
    24 /*
       
    25  * @test
       
    26  * @bug     6471599
       
    27  * @summary Type of rhs cannot be obtained when assigning to erroneous symbol
       
    28  * @author  Peter von der Ah\u00e9
       
    29  * @compile Main.java
       
    30  * @run main Main
       
    31  */
       
    32 
       
    33 import com.sun.source.tree.AssignmentTree;
       
    34 import com.sun.source.tree.CompilationUnitTree;
       
    35 import com.sun.source.util.JavacTask;
       
    36 import com.sun.source.util.TreePath;
       
    37 import com.sun.source.util.TreePathScanner;
       
    38 import com.sun.source.util.Trees;
       
    39 import com.sun.tools.javac.util.List;
       
    40 import java.io.IOException;
       
    41 import java.net.URI;
       
    42 import javax.lang.model.type.TypeKind;
       
    43 import javax.tools.JavaCompiler;
       
    44 import javax.tools.JavaFileObject;
       
    45 import javax.tools.SimpleJavaFileObject;
       
    46 import javax.tools.ToolProvider;
       
    47 
       
    48 public class Main {
       
    49     static class MyFileObject extends SimpleJavaFileObject {
       
    50         public MyFileObject() {
       
    51             super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
       
    52         }
       
    53         public CharSequence getCharContent(boolean ignoreEncodingErrors) {
       
    54             return "public class Test { { x = java.util.Collections.emptySet(); } }";
       
    55         }
       
    56     }
       
    57     static Trees trees;
       
    58     public static void main(String[] args) throws IOException {
       
    59         JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
       
    60         JavacTask task = (JavacTask) compiler.getTask(null, null, null, null, null, List.of(new MyFileObject()));
       
    61         trees = Trees.instance(task);
       
    62         Iterable<? extends CompilationUnitTree> asts = task.parse();
       
    63         task.analyze();
       
    64         for (CompilationUnitTree ast : asts) {
       
    65             new MyVisitor().scan(ast, null);
       
    66         }
       
    67     }
       
    68 
       
    69     static class MyVisitor extends TreePathScanner<Void,Void> {
       
    70         @Override
       
    71         public Void visitAssignment(AssignmentTree node, Void ignored) {
       
    72             TreePath path = TreePath.getPath(getCurrentPath(), node.getExpression());
       
    73             if (trees.getTypeMirror(path).getKind() == TypeKind.ERROR)
       
    74                 throw new AssertionError(path.getLeaf());
       
    75             return null;
       
    76         }
       
    77 
       
    78     }
       
    79 }