1/* 2 * Copyright (c) 2006, 2015, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * 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 * @modules jdk.compiler/com.sun.tools.javac.util 30 * @compile Main.java 31 * @run main Main 32 */ 33 34import com.sun.source.tree.AssignmentTree; 35import com.sun.source.tree.CompilationUnitTree; 36import com.sun.source.util.JavacTask; 37import com.sun.source.util.TreePath; 38import com.sun.source.util.TreePathScanner; 39import com.sun.source.util.Trees; 40import com.sun.tools.javac.util.List; 41import java.io.IOException; 42import java.net.URI; 43import javax.lang.model.type.TypeKind; 44import javax.tools.JavaCompiler; 45import javax.tools.JavaFileObject; 46import javax.tools.SimpleJavaFileObject; 47import javax.tools.ToolProvider; 48 49public class Main { 50 static class MyFileObject extends SimpleJavaFileObject { 51 public MyFileObject() { 52 super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE); 53 } 54 public CharSequence getCharContent(boolean ignoreEncodingErrors) { 55 return "public class Test { { x = java.util.Collections.emptySet(); } }"; 56 } 57 } 58 static Trees trees; 59 public static void main(String[] args) throws IOException { 60 JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); 61 JavacTask task = (JavacTask) compiler.getTask(null, null, null, null, null, List.of(new MyFileObject())); 62 trees = Trees.instance(task); 63 Iterable<? extends CompilationUnitTree> asts = task.parse(); 64 task.analyze(); 65 for (CompilationUnitTree ast : asts) { 66 new MyVisitor().scan(ast, null); 67 } 68 } 69 70 static class MyVisitor extends TreePathScanner<Void,Void> { 71 @Override 72 public Void visitAssignment(AssignmentTree node, Void ignored) { 73 TreePath path = TreePath.getPath(getCurrentPath(), node.getExpression()); 74 if (trees.getTypeMirror(path).getKind() == TypeKind.ERROR) 75 throw new AssertionError(path.getLeaf()); 76 return null; 77 } 78 79 } 80} 81