author | avstepan |
Tue, 19 May 2015 16:04:14 +0400 | |
changeset 30655 | d83f50188ca9 |
parent 27579 | d1a63c99cdd5 |
child 30730 | d3ce7619db2c |
permissions | -rw-r--r-- |
10 | 1 |
/* |
22690
86dd707d616a
8027175: Existing regression tests in tl/langtools and tl/jdk need to be updated for @bug keyword
sogoel
parents:
17578
diff
changeset
|
2 |
* Copyright (c) 2005, 2014, 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 |
17556
eced45696767
8014363: javac test class ToolTester handles classpath incorrectly
jjg
parents:
17280
diff
changeset
|
30 |
* @build ToolTester |
10 | 31 |
* @run main TestJavacTaskScanner TestJavacTaskScanner.java |
32 |
*/ |
|
33 |
||
34 |
import com.sun.tools.javac.api.JavacTaskImpl; |
|
6716
71df48777dd1
6877202: Elements.getDocComment() is not getting JavaDocComments
jjg
parents:
5520
diff
changeset
|
35 |
import com.sun.tools.javac.parser.*; |
10815 | 36 |
import com.sun.tools.javac.parser.Tokens.Token; |
6716
71df48777dd1
6877202: Elements.getDocComment() is not getting JavaDocComments
jjg
parents:
5520
diff
changeset
|
37 |
import com.sun.tools.javac.util.*; |
10 | 38 |
import java.io.*; |
5016
ff5e6791d0bb
6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
2986
diff
changeset
|
39 |
import java.net.*; |
10 | 40 |
import java.nio.*; |
5016
ff5e6791d0bb
6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
2986
diff
changeset
|
41 |
import java.nio.charset.Charset; |
ff5e6791d0bb
6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
2986
diff
changeset
|
42 |
import java.util.Arrays; |
10 | 43 |
import javax.lang.model.element.Element; |
44 |
import javax.lang.model.element.TypeElement; |
|
45 |
import javax.lang.model.type.DeclaredType; |
|
46 |
import javax.lang.model.type.TypeMirror; |
|
47 |
import javax.lang.model.util.Elements; |
|
48 |
import javax.lang.model.util.Types; |
|
49 |
import javax.tools.*; |
|
50 |
||
5016
ff5e6791d0bb
6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
2986
diff
changeset
|
51 |
import static javax.tools.StandardLocation.CLASS_PATH; |
ff5e6791d0bb
6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
2986
diff
changeset
|
52 |
import static javax.tools.StandardLocation.SOURCE_PATH; |
ff5e6791d0bb
6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
2986
diff
changeset
|
53 |
import static javax.tools.StandardLocation.CLASS_OUTPUT; |
ff5e6791d0bb
6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
2986
diff
changeset
|
54 |
|
2986 | 55 |
public class TestJavacTaskScanner extends ToolTester { |
10 | 56 |
|
57 |
final JavacTaskImpl task; |
|
58 |
final Elements elements; |
|
59 |
final Types types; |
|
60 |
||
2986 | 61 |
int numTokens; |
62 |
int numParseTypeElements; |
|
63 |
int numAllMembers; |
|
64 |
||
65 |
TestJavacTaskScanner(File file) { |
|
66 |
final Iterable<? extends JavaFileObject> compilationUnits = |
|
67 |
fm.getJavaFileObjects(new File[] {file}); |
|
5016
ff5e6791d0bb
6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
2986
diff
changeset
|
68 |
StandardJavaFileManager fm = getLocalFileManager(tool, null, null); |
2986 | 69 |
task = (JavacTaskImpl)tool.getTask(null, fm, null, null, null, compilationUnits); |
6716
71df48777dd1
6877202: Elements.getDocComment() is not getting JavaDocComments
jjg
parents:
5520
diff
changeset
|
70 |
task.getContext().put(ScannerFactory.scannerFactoryKey, |
2986 | 71 |
new MyScanner.Factory(task.getContext(), this)); |
10 | 72 |
elements = task.getElements(); |
73 |
types = task.getTypes(); |
|
74 |
} |
|
75 |
||
76 |
public void run() { |
|
77 |
Iterable<? extends TypeElement> toplevels; |
|
26264
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
22690
diff
changeset
|
78 |
toplevels = task.enter(task.parse()); |
10 | 79 |
for (TypeElement clazz : toplevels) { |
80 |
System.out.format("Testing %s:%n%n", clazz.getSimpleName()); |
|
81 |
testParseType(clazz); |
|
82 |
testGetAllMembers(clazz); |
|
83 |
System.out.println(); |
|
84 |
System.out.println(); |
|
85 |
System.out.println(); |
|
86 |
} |
|
2986 | 87 |
|
88 |
System.out.println("#tokens: " + numTokens); |
|
89 |
System.out.println("#parseTypeElements: " + numParseTypeElements); |
|
90 |
System.out.println("#allMembers: " + numAllMembers); |
|
91 |
||
27579 | 92 |
check(numTokens, "#Tokens", 1054); |
17280
8cc37edd05a7
8013256: javac test failing after Lambda changes to java.util.List
jjg
parents:
16569
diff
changeset
|
93 |
check(numParseTypeElements, "#parseTypeElements", 158); |
10815 | 94 |
check(numAllMembers, "#allMembers", 52); |
2986 | 95 |
} |
96 |
||
97 |
void check(int value, String name, int expected) { |
|
98 |
// allow some slop in the comparison to allow for minor edits in the |
|
99 |
// test and in the platform |
|
100 |
if (value < expected * 9 / 10) |
|
101 |
throw new Error(name + " lower than expected; expected " + expected + "; found: " + value); |
|
102 |
if (value > expected * 11 / 10) |
|
103 |
throw new Error(name + " higher than expected; expected " + expected + "; found: " + value); |
|
10 | 104 |
} |
105 |
||
106 |
void testParseType(TypeElement clazz) { |
|
107 |
DeclaredType type = (DeclaredType)task.parseType("List<String>", clazz); |
|
108 |
for (Element member : elements.getAllMembers((TypeElement)type.asElement())) { |
|
109 |
TypeMirror mt = types.asMemberOf(type, member); |
|
17578 | 110 |
System.out.format("type#%d: %s : %s -> %s%n", |
111 |
numParseTypeElements, member.getSimpleName(), member.asType(), mt); |
|
2986 | 112 |
numParseTypeElements++; |
10 | 113 |
} |
114 |
} |
|
115 |
||
116 |
public static void main(String... args) throws IOException { |
|
117 |
String srcdir = System.getProperty("test.src"); |
|
27321
c8eceba990bf
8062514: Update ToolTester tests to close file manager
jjg
parents:
26264
diff
changeset
|
118 |
try (TestJavacTaskScanner t = new TestJavacTaskScanner(new File(srcdir, args[0]))) { |
c8eceba990bf
8062514: Update ToolTester tests to close file manager
jjg
parents:
26264
diff
changeset
|
119 |
t.run(); |
c8eceba990bf
8062514: Update ToolTester tests to close file manager
jjg
parents:
26264
diff
changeset
|
120 |
} |
10 | 121 |
} |
122 |
||
123 |
private void testGetAllMembers(TypeElement clazz) { |
|
124 |
for (Element member : elements.getAllMembers(clazz)) { |
|
17578 | 125 |
System.out.format("elem#%d: %s : %s%n", |
126 |
numAllMembers, member.getSimpleName(), member.asType()); |
|
2986 | 127 |
numAllMembers++; |
10 | 128 |
} |
129 |
} |
|
5016
ff5e6791d0bb
6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
2986
diff
changeset
|
130 |
|
ff5e6791d0bb
6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
2986
diff
changeset
|
131 |
/* 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
|
132 |
* 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
|
133 |
* 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
|
134 |
* 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
|
135 |
* 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
|
136 |
* 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
|
137 |
*/ |
ff5e6791d0bb
6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
2986
diff
changeset
|
138 |
public StandardJavaFileManager getLocalFileManager(JavaCompiler tool, |
ff5e6791d0bb
6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
2986
diff
changeset
|
139 |
DiagnosticListener<JavaFileObject> dl, |
ff5e6791d0bb
6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
2986
diff
changeset
|
140 |
Charset encoding) { |
ff5e6791d0bb
6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
2986
diff
changeset
|
141 |
StandardJavaFileManager fm = tool.getStandardFileManager(dl, null, encoding); |
ff5e6791d0bb
6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
2986
diff
changeset
|
142 |
try { |
ff5e6791d0bb
6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
2986
diff
changeset
|
143 |
fm.setLocation(SOURCE_PATH, Arrays.asList(test_src)); |
27579 | 144 |
fm.setLocation(CLASS_PATH, test_class_path); |
5016
ff5e6791d0bb
6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
2986
diff
changeset
|
145 |
fm.setLocation(CLASS_OUTPUT, Arrays.asList(test_classes)); |
ff5e6791d0bb
6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
2986
diff
changeset
|
146 |
} catch (IOException e) { |
ff5e6791d0bb
6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
2986
diff
changeset
|
147 |
throw new AssertionError(e); |
ff5e6791d0bb
6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
2986
diff
changeset
|
148 |
} |
ff5e6791d0bb
6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
2986
diff
changeset
|
149 |
return fm; |
ff5e6791d0bb
6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
2986
diff
changeset
|
150 |
} |
10 | 151 |
} |
152 |
||
153 |
class MyScanner extends Scanner { |
|
154 |
||
6716
71df48777dd1
6877202: Elements.getDocComment() is not getting JavaDocComments
jjg
parents:
5520
diff
changeset
|
155 |
public static class Factory extends ScannerFactory { |
2986 | 156 |
public Factory(Context context, TestJavacTaskScanner test) { |
10 | 157 |
super(context); |
2986 | 158 |
this.test = test; |
10 | 159 |
} |
160 |
||
161 |
@Override |
|
6716
71df48777dd1
6877202: Elements.getDocComment() is not getting JavaDocComments
jjg
parents:
5520
diff
changeset
|
162 |
public Scanner newScanner(CharSequence input, boolean keepDocComments) { |
10 | 163 |
if (input instanceof CharBuffer) { |
2986 | 164 |
return new MyScanner(this, (CharBuffer)input, test); |
10 | 165 |
} else { |
166 |
char[] array = input.toString().toCharArray(); |
|
6716
71df48777dd1
6877202: Elements.getDocComment() is not getting JavaDocComments
jjg
parents:
5520
diff
changeset
|
167 |
return newScanner(array, array.length, keepDocComments); |
10 | 168 |
} |
169 |
} |
|
170 |
||
171 |
@Override |
|
6716
71df48777dd1
6877202: Elements.getDocComment() is not getting JavaDocComments
jjg
parents:
5520
diff
changeset
|
172 |
public Scanner newScanner(char[] input, int inputLength, boolean keepDocComments) { |
2986 | 173 |
return new MyScanner(this, input, inputLength, test); |
10 | 174 |
} |
2986 | 175 |
|
176 |
private TestJavacTaskScanner test; |
|
10 | 177 |
} |
6716
71df48777dd1
6877202: Elements.getDocComment() is not getting JavaDocComments
jjg
parents:
5520
diff
changeset
|
178 |
protected MyScanner(ScannerFactory fac, CharBuffer buffer, TestJavacTaskScanner test) { |
10 | 179 |
super(fac, buffer); |
2986 | 180 |
this.test = test; |
10 | 181 |
} |
6716
71df48777dd1
6877202: Elements.getDocComment() is not getting JavaDocComments
jjg
parents:
5520
diff
changeset
|
182 |
protected MyScanner(ScannerFactory fac, char[] input, int inputLength, TestJavacTaskScanner test) { |
10 | 183 |
super(fac, input, inputLength); |
2986 | 184 |
this.test = test; |
10 | 185 |
} |
186 |
||
187 |
public void nextToken() { |
|
188 |
super.nextToken(); |
|
10815 | 189 |
Token tk = token(); |
190 |
System.err.format("Saw token %s %n", tk.kind); |
|
2986 | 191 |
test.numTokens++; |
10 | 192 |
} |
2986 | 193 |
|
194 |
private TestJavacTaskScanner test; |
|
195 |
||
10 | 196 |
} |