T6557752.java revision 3294:9adfb22ff08f
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 6557752 27 * @summary Test for wrapping the original type in ErrorType. 28 * @library ../lib 29 * @modules jdk.compiler/com.sun.tools.javac.util 30 * @compile T6557752.java 31 * @run main T6557752 32 */ 33 34import com.sun.source.tree.AssignmentTree; 35import com.sun.source.tree.CompilationUnitTree; 36import com.sun.source.tree.MethodInvocationTree; 37import com.sun.source.util.JavacTask; 38import com.sun.source.util.TreePath; 39import com.sun.source.util.TreePathScanner; 40import com.sun.source.util.Trees; 41import com.sun.tools.javac.util.List; 42import java.io.IOException; 43import java.net.URI; 44import javax.lang.model.type.ErrorType; 45import javax.lang.model.type.TypeKind; 46import javax.lang.model.type.TypeMirror; 47import javax.tools.JavaCompiler; 48import javax.tools.JavaFileObject; 49import javax.tools.SimpleJavaFileObject; 50import javax.tools.ToolProvider; 51import javax.lang.model.util.Types; 52 53public class T6557752 { 54 static class MyFileObject extends SimpleJavaFileObject { 55 public MyFileObject() { 56 super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE); 57 } 58 public CharSequence getCharContent(boolean ignoreEncodingErrors) { 59 return "import java.util.*;\n" 60 + "public class Test {\n" 61 + " void foobar() {\n" 62 + " Iterator<Number> itr = null;\n" 63 + " String str = itr.next();\n" 64 + " FooBar fooBar = FooBar.foobar();\n" 65 + " }\n" 66 + "}"; 67 } 68 } 69 static Trees trees; 70 static JavacTask task = null; 71 public static void main(String[] args) throws IOException { 72 JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); 73 task = (JavacTask) compiler.getTask(null, null, null, null, null, List.of(new MyFileObject())); 74 Iterable<? extends CompilationUnitTree> asts = task.parse(); 75 task.analyze(); 76 trees = Trees.instance(task); 77 MyVisitor myVisitor = new MyVisitor(); 78 for (CompilationUnitTree ast : asts) { 79 myVisitor.compilationUnit = ast; 80 myVisitor.scan(ast, null); 81 } 82 83 if (!myVisitor.foundError) { 84 throw new AssertionError("Expected error not found!"); 85 } 86 } 87 88 static class MyVisitor extends TreePathScanner<Void,Void> { 89 public boolean foundError = false; 90 CompilationUnitTree compilationUnit = null; 91 int i = 0; 92 @Override 93 public Void visitMethodInvocation(MethodInvocationTree node, Void ignored) { 94 TreePath path = TreePath.getPath(compilationUnit, node); 95 TypeMirror typeMirror = trees.getTypeMirror(path); 96 if (typeMirror.getKind() == TypeKind.ERROR) { 97 if (i == 0) { 98 String str1 = trees.getOriginalType((ErrorType)typeMirror).toString(); 99 if (!str1.equals("java.lang.Number")) { 100 throw new AssertionError("Trees.getOriginalType() error!"); 101 } 102 103 Types types = task.getTypes(); 104 105 str1 = types.asElement(trees.getOriginalType((ErrorType)typeMirror)).toString(); 106 if (!str1.equals("java.lang.Number")) { 107 throw new AssertionError("Types.asElement() error!"); 108 } 109 110 i++; 111 } 112 else if (i == 1) { 113 String str1 = trees.getOriginalType((ErrorType)typeMirror).toString(); 114 if (!str1.equals("FooBar")) { 115 throw new AssertionError("Trees.getOriginalType() error!"); 116 } 117 118 Types types = task.getTypes(); 119 120 str1 = types.asElement(trees.getOriginalType((ErrorType)typeMirror)).toString(); 121 if (!str1.equals("FooBar")) { 122 throw new AssertionError("Types.asElement() error!"); 123 } 124 foundError = true; 125 } 126 } 127 128 129 return null; 130 } 131 132 } 133} 134