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