TestPos.java revision 553:9d9f26857129
1/* 2 * Copyright (c) 2006, 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 6413682 27 * @summary Compiler confused about implicit type args and arrays 28 * @author Peter von der Ah\u00e9 29 */ 30 31import com.sun.source.tree.CompilationUnitTree; 32import com.sun.source.tree.ErroneousTree; 33import com.sun.source.tree.Tree; 34import com.sun.source.util.JavacTask; 35import com.sun.source.util.TreeScanner; 36import com.sun.source.util.Trees; 37import java.io.IOException; 38import java.net.URI; 39import java.util.Collections; 40import java.util.List; 41import javax.tools.Diagnostic; 42import javax.tools.DiagnosticListener; 43import javax.tools.JavaCompiler; 44import javax.tools.JavaFileObject; 45import javax.tools.SimpleJavaFileObject; 46import static javax.tools.JavaFileObject.Kind.SOURCE; 47import javax.tools.ToolProvider; 48 49public class TestPos { 50 51 static final String errCode = "compiler.err.cannot.create.array.with.type.arguments"; 52 static final String expected = 53 String.format("%s%n%s%n", 54 "compiler.err.cannot.create.array.with.type.arguments @ 33", 55 "begin=28, end=50 : new Object[0],T,e,s,t"); 56 57 public static void main(String... args) throws IOException { 58 final boolean[] sawError = { false }; 59 final StringBuilder log = new StringBuilder(); 60 class MyFileObject extends SimpleJavaFileObject { 61 MyFileObject() { 62 super(URI.create("myfo:///Test.java"), SOURCE); 63 } 64 @Override 65 public String getCharContent(boolean ignoreEncodingErrors) { 66 // 0 1 2 3 4 5 67 // 0123456789012345678901234567890123456789012345678901234 68 return "class Test { { Object[] o = new <T,e,s,t>Object[0]; } }"; 69 } 70 } 71 class Scanner extends TreeScanner<Void,Trees> { 72 CompilationUnitTree toplevel = null; 73 @Override 74 public Void visitCompilationUnit(CompilationUnitTree node, Trees trees) { 75 toplevel = node; 76 return super.visitCompilationUnit(node, trees); 77 } 78 @Override 79 public Void visitErroneous(ErroneousTree node, Trees trees) { 80 sawError[0] = true; 81 long startPos = trees.getSourcePositions().getStartPosition(toplevel, node); 82 long endPos = trees.getSourcePositions().getEndPosition(toplevel, node); 83 log.append(String.format("begin=%s, end=%s : %s%n", 84 startPos, 85 endPos, 86 node.getErrorTrees())); 87 if (startPos != 28) 88 error("Start pos for %s is incorrect (%s)!", node, startPos); 89 if (endPos != 50) 90 error("End pos for %s is incorrect (%s)!", node, endPos); 91 return null; 92 } 93 } 94 JavaCompiler javac = ToolProvider.getSystemJavaCompiler(); 95 List<JavaFileObject> compilationUnits = 96 Collections.<JavaFileObject>singletonList(new MyFileObject()); 97 DiagnosticListener<JavaFileObject> dl = new DiagnosticListener<JavaFileObject>() { 98 public void report(Diagnostic<? extends JavaFileObject> diag) { 99 log.append(String.format("%s @ %s%n", diag.getCode(), diag.getPosition())); 100 if (!diag.getCode().equals(errCode)) 101 error("unexpected error"); 102 if (diag.getPosition() != 33) 103 error("Error pos for %s is incorrect (%s)!", 104 diag.getCode(), diag.getPosition()); 105 sawError[0] = true; 106 } 107 }; 108 JavacTask task = (JavacTask)javac.getTask(null, null, dl, null, null, 109 compilationUnits); 110 Trees trees = Trees.instance(task); 111 Iterable<? extends Tree> toplevels = task.parse(); 112 if (!sawError[0]) 113 error("No parse error detected"); 114 sawError[0] = false; 115 new Scanner().scan(toplevels, trees); 116 if (!sawError[0]) 117 error("No error tree detected"); 118 if (!log.toString().equals(expected)) 119 error("Unexpected log message: %n%s%n", log); 120 System.out.print(log); 121 System.out.flush(); 122 } 123 124 static void error(String format, Object... args) { 125 throw new AssertionError(String.format(format, args)); 126 } 127 128} 129