1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/test/tools/javac/api/6557752/T6557752.java Wed Apr 27 01:34:52 2016 +0800 1.3 @@ -0,0 +1,134 @@ 1.4 +/* 1.5 + * Copyright (c) 2006, 2011, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.23 + * or visit www.oracle.com if you need additional information or have any 1.24 + * questions. 1.25 + */ 1.26 + 1.27 + 1.28 +/* 1.29 + * @test 1.30 + * @bug 6557752 1.31 + * @summary Test for wrapping the original type in ErrorType. 1.32 + * @library ../lib 1.33 + * @compile T6557752.java 1.34 + * @run main T6557752 1.35 + */ 1.36 + 1.37 +import com.sun.source.tree.AssignmentTree; 1.38 +import com.sun.source.tree.CompilationUnitTree; 1.39 +import com.sun.source.tree.MethodInvocationTree; 1.40 +import com.sun.source.util.JavacTask; 1.41 +import com.sun.source.util.TreePath; 1.42 +import com.sun.source.util.TreePathScanner; 1.43 +import com.sun.source.util.Trees; 1.44 +import com.sun.tools.javac.api.JavacTaskImpl; 1.45 +import com.sun.tools.javac.util.List; 1.46 +import java.io.IOException; 1.47 +import java.net.URI; 1.48 +import javax.lang.model.type.ErrorType; 1.49 +import javax.lang.model.type.TypeKind; 1.50 +import javax.lang.model.type.TypeMirror; 1.51 +import javax.tools.JavaCompiler; 1.52 +import javax.tools.JavaFileObject; 1.53 +import javax.tools.SimpleJavaFileObject; 1.54 +import javax.tools.ToolProvider; 1.55 +import javax.lang.model.util.Types; 1.56 + 1.57 +public class T6557752 { 1.58 + static class MyFileObject extends SimpleJavaFileObject { 1.59 + public MyFileObject() { 1.60 + super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE); 1.61 + } 1.62 + public CharSequence getCharContent(boolean ignoreEncodingErrors) { 1.63 + return "import java.util.*;\n" 1.64 + + "public class Test {\n" 1.65 + + " void foobar() {\n" 1.66 + + " Iterator<Number> itr = null;\n" 1.67 + + " String str = itr.next();\n" 1.68 + + " FooBar fooBar = FooBar.foobar();\n" 1.69 + + " }\n" 1.70 + + "}"; 1.71 + } 1.72 + } 1.73 + static Trees trees; 1.74 + static JavacTask task = null; 1.75 + public static void main(String[] args) throws IOException { 1.76 + JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); 1.77 + task = (JavacTask) compiler.getTask(null, null, null, null, null, List.of(new MyFileObject())); 1.78 + Iterable<? extends CompilationUnitTree> asts = task.parse(); 1.79 + task.analyze(); 1.80 + trees = Trees.instance(task); 1.81 + MyVisitor myVisitor = new MyVisitor(); 1.82 + for (CompilationUnitTree ast : asts) { 1.83 + myVisitor.compilationUnit = ast; 1.84 + myVisitor.scan(ast, null); 1.85 + } 1.86 + 1.87 + if (!myVisitor.foundError) { 1.88 + throw new AssertionError("Expected error not found!"); 1.89 + } 1.90 + } 1.91 + 1.92 + static class MyVisitor extends TreePathScanner<Void,Void> { 1.93 + public boolean foundError = false; 1.94 + CompilationUnitTree compilationUnit = null; 1.95 + int i = 0; 1.96 + @Override 1.97 + public Void visitMethodInvocation(MethodInvocationTree node, Void ignored) { 1.98 + TreePath path = TreePath.getPath(compilationUnit, node); 1.99 + TypeMirror typeMirror = trees.getTypeMirror(path); 1.100 + if (typeMirror.getKind() == TypeKind.ERROR) { 1.101 + if (i == 0) { 1.102 + String str1 = trees.getOriginalType((ErrorType)typeMirror).toString(); 1.103 + if (!str1.equals("java.lang.Number")) { 1.104 + throw new AssertionError("Trees.getOriginalType() error!"); 1.105 + } 1.106 + 1.107 + Types types = task.getTypes(); 1.108 + 1.109 + str1 = types.asElement(trees.getOriginalType((ErrorType)typeMirror)).toString(); 1.110 + if (!str1.equals("java.lang.Number")) { 1.111 + throw new AssertionError("Types.asElement() error!"); 1.112 + } 1.113 + 1.114 + i++; 1.115 + } 1.116 + else if (i == 1) { 1.117 + String str1 = trees.getOriginalType((ErrorType)typeMirror).toString(); 1.118 + if (!str1.equals("FooBar")) { 1.119 + throw new AssertionError("Trees.getOriginalType() error!"); 1.120 + } 1.121 + 1.122 + Types types = task.getTypes(); 1.123 + 1.124 + str1 = types.asElement(trees.getOriginalType((ErrorType)typeMirror)).toString(); 1.125 + if (!str1.equals("FooBar")) { 1.126 + throw new AssertionError("Types.asElement() error!"); 1.127 + } 1.128 + foundError = true; 1.129 + } 1.130 + } 1.131 + 1.132 + 1.133 + return null; 1.134 + } 1.135 + 1.136 + } 1.137 +}