Main.java revision 553:9d9f26857129
1163953Srrs/*
2185694Srrs * Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved.
3235828Stuexen * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4235828Stuexen *
5163953Srrs * This code is free software; you can redistribute it and/or modify it
6163953Srrs * under the terms of the GNU General Public License version 2 only, as
7163953Srrs * published by the Free Software Foundation.
8163953Srrs *
9163953Srrs * This code is distributed in the hope that it will be useful, but WITHOUT
10228653Stuexen * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11163953Srrs * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12163953Srrs * version 2 for more details (a copy is included in the LICENSE file that
13163953Srrs * accompanied this code).
14228653Stuexen *
15163953Srrs * You should have received a copy of the GNU General Public License version
16163953Srrs * 2 along with this work; if not, write to the Free Software Foundation,
17163953Srrs * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18163953Srrs *
19163953Srrs * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20163953Srrs * or visit www.oracle.com if you need additional information or have any
21163953Srrs * questions.
22163953Srrs */
23163953Srrs
24163953Srrs/*
25163953Srrs * @test
26163953Srrs * @bug     6471599
27163953Srrs * @summary Type of rhs cannot be obtained when assigning to erroneous symbol
28163953Srrs * @author  Peter von der Ah\u00e9
29163953Srrs * @compile Main.java
30163953Srrs * @run main Main
31163953Srrs */
32163953Srrs
33163953Srrsimport com.sun.source.tree.AssignmentTree;
34163953Srrsimport com.sun.source.tree.CompilationUnitTree;
35163953Srrsimport com.sun.source.util.JavacTask;
36166086Srrsimport com.sun.source.util.TreePath;
37163953Srrsimport com.sun.source.util.TreePathScanner;
38163953Srrsimport com.sun.source.util.Trees;
39167598Srrsimport com.sun.tools.javac.util.List;
40163953Srrsimport java.io.IOException;
41163953Srrsimport java.net.URI;
42163953Srrsimport javax.lang.model.type.TypeKind;
43163953Srrsimport javax.tools.JavaCompiler;
44163953Srrsimport javax.tools.JavaFileObject;
45163953Srrsimport javax.tools.SimpleJavaFileObject;
46163953Srrsimport javax.tools.ToolProvider;
47163953Srrs
48163953Srrspublic class Main {
49163953Srrs    static class MyFileObject extends SimpleJavaFileObject {
50163953Srrs        public MyFileObject() {
51171158Srrs            super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
52188067Srrs        }
53270350Stuexen        public CharSequence getCharContent(boolean ignoreEncodingErrors) {
54179157Srrs            return "public class Test { { x = java.util.Collections.emptySet(); } }";
55270350Stuexen        }
56233597Stuexen    }
57179783Srrs    static Trees trees;
58163953Srrs    public static void main(String[] args) throws IOException {
59163953Srrs        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
60163953Srrs        JavacTask task = (JavacTask) compiler.getTask(null, null, null, null, null, List.of(new MyFileObject()));
61163953Srrs        trees = Trees.instance(task);
62163953Srrs        Iterable<? extends CompilationUnitTree> asts = task.parse();
63163953Srrs        task.analyze();
64163953Srrs        for (CompilationUnitTree ast : asts) {
65163953Srrs            new MyVisitor().scan(ast, null);
66163953Srrs        }
67163953Srrs    }
68163953Srrs
69163953Srrs    static class MyVisitor extends TreePathScanner<Void,Void> {
70163953Srrs        @Override
71163953Srrs        public Void visitAssignment(AssignmentTree node, Void ignored) {
72163953Srrs            TreePath path = TreePath.getPath(getCurrentPath(), node.getExpression());
73163953Srrs            if (trees.getTypeMirror(path).getKind() == TypeKind.ERROR)
74163953Srrs                throw new AssertionError(path.getLeaf());
75163953Srrs            return null;
76163953Srrs        }
77163953Srrs
78163953Srrs    }
79163953Srrs}
80163953Srrs