Tue, 15 Sep 2009 12:20:55 -0700
6882235: invalid exponent causes silent javac crash
Reviewed-by: darcy
1 /*
2 * Copyright 2006 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 */
24 /*
25 * @test
26 * @bug 6413682
27 * @summary Compiler confused about implicit type args and arrays
28 * @author Peter von der Ah\u00e9
29 */
31 import com.sun.source.tree.CompilationUnitTree;
32 import com.sun.source.tree.ErroneousTree;
33 import com.sun.source.tree.Tree;
34 import com.sun.source.util.JavacTask;
35 import com.sun.source.util.TreeScanner;
36 import com.sun.source.util.Trees;
37 import java.io.IOException;
38 import java.net.URI;
39 import java.util.Collections;
40 import java.util.List;
41 import javax.tools.Diagnostic;
42 import javax.tools.DiagnosticListener;
43 import javax.tools.JavaCompiler;
44 import javax.tools.JavaFileObject;
45 import javax.tools.SimpleJavaFileObject;
46 import static javax.tools.JavaFileObject.Kind.SOURCE;
47 import javax.tools.ToolProvider;
49 public class TestPos {
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");
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 }
124 static void error(String format, Object... args) {
125 throw new AssertionError(String.format(format, args));
126 }
128 }