author | jjg |
Wed, 10 Aug 2016 15:47:46 -0700 | |
changeset 40308 | 274367a99f98 |
parent 37848 | 3c8ff4204d2d |
permissions | -rw-r--r-- |
10 | 1 |
/* |
37848
3c8ff4204d2d
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36526
diff
changeset
|
2 |
* Copyright (c) 2005, 2016, 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 |
|
22690
86dd707d616a
8027175: Existing regression tests in tl/langtools and tl/jdk need to be updated for @bug keyword
sogoel
parents:
17578
diff
changeset
|
26 |
* @bug 4813736 8013256 |
10 | 27 |
* @summary Additional functionality test of task and JSR 269 |
28 |
* @author Peter von der Ah\u00e9 |
|
2986 | 29 |
* @library ./lib |
30730
d3ce7619db2c
8076543: Add @modules as needed to the langtools tests
akulyakh
parents:
27579
diff
changeset
|
30 |
* @modules jdk.compiler/com.sun.tools.javac.api |
d3ce7619db2c
8076543: Add @modules as needed to the langtools tests
akulyakh
parents:
27579
diff
changeset
|
31 |
* jdk.compiler/com.sun.tools.javac.code |
d3ce7619db2c
8076543: Add @modules as needed to the langtools tests
akulyakh
parents:
27579
diff
changeset
|
32 |
* jdk.compiler/com.sun.tools.javac.parser |
d3ce7619db2c
8076543: Add @modules as needed to the langtools tests
akulyakh
parents:
27579
diff
changeset
|
33 |
* jdk.compiler/com.sun.tools.javac.util |
17556
eced45696767
8014363: javac test class ToolTester handles classpath incorrectly
jjg
parents:
17280
diff
changeset
|
34 |
* @build ToolTester |
10 | 35 |
* @run main TestJavacTaskScanner TestJavacTaskScanner.java |
36 |
*/ |
|
37 |
||
38 |
import com.sun.tools.javac.api.JavacTaskImpl; |
|
6716
71df48777dd1
6877202: Elements.getDocComment() is not getting JavaDocComments
jjg
parents:
5520
diff
changeset
|
39 |
import com.sun.tools.javac.parser.*; |
10815 | 40 |
import com.sun.tools.javac.parser.Tokens.Token; |
6716
71df48777dd1
6877202: Elements.getDocComment() is not getting JavaDocComments
jjg
parents:
5520
diff
changeset
|
41 |
import com.sun.tools.javac.util.*; |
36526 | 42 |
|
10 | 43 |
import java.io.*; |
5016
ff5e6791d0bb
6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
2986
diff
changeset
|
44 |
import java.net.*; |
10 | 45 |
import java.nio.*; |
5016
ff5e6791d0bb
6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
2986
diff
changeset
|
46 |
import java.nio.charset.Charset; |
ff5e6791d0bb
6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
2986
diff
changeset
|
47 |
import java.util.Arrays; |
36526 | 48 |
|
10 | 49 |
import javax.lang.model.element.Element; |
50 |
import javax.lang.model.element.TypeElement; |
|
51 |
import javax.lang.model.type.DeclaredType; |
|
52 |
import javax.lang.model.type.TypeMirror; |
|
36526 | 53 |
import javax.lang.model.util.ElementFilter; |
10 | 54 |
import javax.lang.model.util.Elements; |
55 |
import javax.lang.model.util.Types; |
|
56 |
import javax.tools.*; |
|
57 |
||
5016
ff5e6791d0bb
6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
2986
diff
changeset
|
58 |
import static javax.tools.StandardLocation.CLASS_PATH; |
ff5e6791d0bb
6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
2986
diff
changeset
|
59 |
import static javax.tools.StandardLocation.SOURCE_PATH; |
ff5e6791d0bb
6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
2986
diff
changeset
|
60 |
import static javax.tools.StandardLocation.CLASS_OUTPUT; |
ff5e6791d0bb
6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
2986
diff
changeset
|
61 |
|
2986 | 62 |
public class TestJavacTaskScanner extends ToolTester { |
10 | 63 |
|
64 |
final JavacTaskImpl task; |
|
65 |
final Elements elements; |
|
66 |
final Types types; |
|
67 |
||
2986 | 68 |
int numTokens; |
69 |
int numParseTypeElements; |
|
70 |
int numAllMembers; |
|
71 |
||
72 |
TestJavacTaskScanner(File file) { |
|
73 |
final Iterable<? extends JavaFileObject> compilationUnits = |
|
74 |
fm.getJavaFileObjects(new File[] {file}); |
|
5016
ff5e6791d0bb
6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
2986
diff
changeset
|
75 |
StandardJavaFileManager fm = getLocalFileManager(tool, null, null); |
37848
3c8ff4204d2d
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36526
diff
changeset
|
76 |
java.util.List<String> options = Arrays.asList( |
40308
274367a99f98
8136930: Simplify use of module-system options by custom launchers
jjg
parents:
37848
diff
changeset
|
77 |
"--add-exports", "jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED", |
274367a99f98
8136930: Simplify use of module-system options by custom launchers
jjg
parents:
37848
diff
changeset
|
78 |
"--add-exports", "jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED", |
274367a99f98
8136930: Simplify use of module-system options by custom launchers
jjg
parents:
37848
diff
changeset
|
79 |
"--add-exports", "jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED", |
274367a99f98
8136930: Simplify use of module-system options by custom launchers
jjg
parents:
37848
diff
changeset
|
80 |
"--add-exports", "jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED"); |
36526 | 81 |
task = (JavacTaskImpl)tool.getTask(null, fm, null, options, null, compilationUnits); |
6716
71df48777dd1
6877202: Elements.getDocComment() is not getting JavaDocComments
jjg
parents:
5520
diff
changeset
|
82 |
task.getContext().put(ScannerFactory.scannerFactoryKey, |
2986 | 83 |
new MyScanner.Factory(task.getContext(), this)); |
10 | 84 |
elements = task.getElements(); |
85 |
types = task.getTypes(); |
|
86 |
} |
|
87 |
||
88 |
public void run() { |
|
89 |
Iterable<? extends TypeElement> toplevels; |
|
36526 | 90 |
toplevels = ElementFilter.typesIn(task.enter(task.parse())); |
10 | 91 |
for (TypeElement clazz : toplevels) { |
92 |
System.out.format("Testing %s:%n%n", clazz.getSimpleName()); |
|
93 |
testParseType(clazz); |
|
94 |
testGetAllMembers(clazz); |
|
95 |
System.out.println(); |
|
96 |
System.out.println(); |
|
97 |
System.out.println(); |
|
98 |
} |
|
2986 | 99 |
|
100 |
System.out.println("#tokens: " + numTokens); |
|
101 |
System.out.println("#parseTypeElements: " + numParseTypeElements); |
|
102 |
System.out.println("#allMembers: " + numAllMembers); |
|
103 |
||
27579 | 104 |
check(numTokens, "#Tokens", 1054); |
17280
8cc37edd05a7
8013256: javac test failing after Lambda changes to java.util.List
jjg
parents:
16569
diff
changeset
|
105 |
check(numParseTypeElements, "#parseTypeElements", 158); |
10815 | 106 |
check(numAllMembers, "#allMembers", 52); |
2986 | 107 |
} |
108 |
||
109 |
void check(int value, String name, int expected) { |
|
110 |
// allow some slop in the comparison to allow for minor edits in the |
|
111 |
// test and in the platform |
|
112 |
if (value < expected * 9 / 10) |
|
113 |
throw new Error(name + " lower than expected; expected " + expected + "; found: " + value); |
|
114 |
if (value > expected * 11 / 10) |
|
115 |
throw new Error(name + " higher than expected; expected " + expected + "; found: " + value); |
|
10 | 116 |
} |
117 |
||
118 |
void testParseType(TypeElement clazz) { |
|
119 |
DeclaredType type = (DeclaredType)task.parseType("List<String>", clazz); |
|
120 |
for (Element member : elements.getAllMembers((TypeElement)type.asElement())) { |
|
121 |
TypeMirror mt = types.asMemberOf(type, member); |
|
17578 | 122 |
System.out.format("type#%d: %s : %s -> %s%n", |
123 |
numParseTypeElements, member.getSimpleName(), member.asType(), mt); |
|
2986 | 124 |
numParseTypeElements++; |
10 | 125 |
} |
126 |
} |
|
127 |
||
128 |
public static void main(String... args) throws IOException { |
|
129 |
String srcdir = System.getProperty("test.src"); |
|
27321
c8eceba990bf
8062514: Update ToolTester tests to close file manager
jjg
parents:
26264
diff
changeset
|
130 |
try (TestJavacTaskScanner t = new TestJavacTaskScanner(new File(srcdir, args[0]))) { |
c8eceba990bf
8062514: Update ToolTester tests to close file manager
jjg
parents:
26264
diff
changeset
|
131 |
t.run(); |
c8eceba990bf
8062514: Update ToolTester tests to close file manager
jjg
parents:
26264
diff
changeset
|
132 |
} |
10 | 133 |
} |
134 |
||
135 |
private void testGetAllMembers(TypeElement clazz) { |
|
136 |
for (Element member : elements.getAllMembers(clazz)) { |
|
17578 | 137 |
System.out.format("elem#%d: %s : %s%n", |
138 |
numAllMembers, member.getSimpleName(), member.asType()); |
|
2986 | 139 |
numAllMembers++; |
10 | 140 |
} |
141 |
} |
|
5016
ff5e6791d0bb
6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
2986
diff
changeset
|
142 |
|
ff5e6791d0bb
6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
2986
diff
changeset
|
143 |
/* Similar to ToolTester.getFileManager, except that this version also ensures |
ff5e6791d0bb
6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
2986
diff
changeset
|
144 |
* javac classes will be available on the classpath. The javac classes are assumed |
ff5e6791d0bb
6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
2986
diff
changeset
|
145 |
* to be on the classpath used to run this test (this is true when using jtreg). |
ff5e6791d0bb
6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
2986
diff
changeset
|
146 |
* The classes are found by obtaining the URL for a sample javac class, using |
ff5e6791d0bb
6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
2986
diff
changeset
|
147 |
* getClassLoader().getResource(), and then deconstructing the URL to find the |
ff5e6791d0bb
6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
2986
diff
changeset
|
148 |
* underlying directory or jar file to place on the classpath. |
ff5e6791d0bb
6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
2986
diff
changeset
|
149 |
*/ |
ff5e6791d0bb
6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
2986
diff
changeset
|
150 |
public StandardJavaFileManager getLocalFileManager(JavaCompiler tool, |
ff5e6791d0bb
6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
2986
diff
changeset
|
151 |
DiagnosticListener<JavaFileObject> dl, |
ff5e6791d0bb
6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
2986
diff
changeset
|
152 |
Charset encoding) { |
ff5e6791d0bb
6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
2986
diff
changeset
|
153 |
StandardJavaFileManager fm = tool.getStandardFileManager(dl, null, encoding); |
ff5e6791d0bb
6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
2986
diff
changeset
|
154 |
try { |
ff5e6791d0bb
6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
2986
diff
changeset
|
155 |
fm.setLocation(SOURCE_PATH, Arrays.asList(test_src)); |
27579 | 156 |
fm.setLocation(CLASS_PATH, test_class_path); |
5016
ff5e6791d0bb
6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
2986
diff
changeset
|
157 |
fm.setLocation(CLASS_OUTPUT, Arrays.asList(test_classes)); |
ff5e6791d0bb
6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
2986
diff
changeset
|
158 |
} catch (IOException e) { |
ff5e6791d0bb
6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
2986
diff
changeset
|
159 |
throw new AssertionError(e); |
ff5e6791d0bb
6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
2986
diff
changeset
|
160 |
} |
ff5e6791d0bb
6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
2986
diff
changeset
|
161 |
return fm; |
ff5e6791d0bb
6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
2986
diff
changeset
|
162 |
} |
10 | 163 |
} |
164 |
||
165 |
class MyScanner extends Scanner { |
|
166 |
||
6716
71df48777dd1
6877202: Elements.getDocComment() is not getting JavaDocComments
jjg
parents:
5520
diff
changeset
|
167 |
public static class Factory extends ScannerFactory { |
2986 | 168 |
public Factory(Context context, TestJavacTaskScanner test) { |
10 | 169 |
super(context); |
2986 | 170 |
this.test = test; |
10 | 171 |
} |
172 |
||
173 |
@Override |
|
6716
71df48777dd1
6877202: Elements.getDocComment() is not getting JavaDocComments
jjg
parents:
5520
diff
changeset
|
174 |
public Scanner newScanner(CharSequence input, boolean keepDocComments) { |
10 | 175 |
if (input instanceof CharBuffer) { |
2986 | 176 |
return new MyScanner(this, (CharBuffer)input, test); |
10 | 177 |
} else { |
178 |
char[] array = input.toString().toCharArray(); |
|
6716
71df48777dd1
6877202: Elements.getDocComment() is not getting JavaDocComments
jjg
parents:
5520
diff
changeset
|
179 |
return newScanner(array, array.length, keepDocComments); |
10 | 180 |
} |
181 |
} |
|
182 |
||
183 |
@Override |
|
6716
71df48777dd1
6877202: Elements.getDocComment() is not getting JavaDocComments
jjg
parents:
5520
diff
changeset
|
184 |
public Scanner newScanner(char[] input, int inputLength, boolean keepDocComments) { |
2986 | 185 |
return new MyScanner(this, input, inputLength, test); |
10 | 186 |
} |
2986 | 187 |
|
188 |
private TestJavacTaskScanner test; |
|
10 | 189 |
} |
6716
71df48777dd1
6877202: Elements.getDocComment() is not getting JavaDocComments
jjg
parents:
5520
diff
changeset
|
190 |
protected MyScanner(ScannerFactory fac, CharBuffer buffer, TestJavacTaskScanner test) { |
10 | 191 |
super(fac, buffer); |
2986 | 192 |
this.test = test; |
10 | 193 |
} |
6716
71df48777dd1
6877202: Elements.getDocComment() is not getting JavaDocComments
jjg
parents:
5520
diff
changeset
|
194 |
protected MyScanner(ScannerFactory fac, char[] input, int inputLength, TestJavacTaskScanner test) { |
10 | 195 |
super(fac, input, inputLength); |
2986 | 196 |
this.test = test; |
10 | 197 |
} |
198 |
||
199 |
public void nextToken() { |
|
200 |
super.nextToken(); |
|
10815 | 201 |
Token tk = token(); |
202 |
System.err.format("Saw token %s %n", tk.kind); |
|
2986 | 203 |
test.numTokens++; |
10 | 204 |
} |
2986 | 205 |
|
206 |
private TestJavacTaskScanner test; |
|
207 |
||
10 | 208 |
} |