author | darcy |
Wed, 01 Jun 2011 23:56:31 -0700 | |
changeset 10180 | b293c1f36ac4 |
parent 6716 | 71df48777dd1 |
child 10815 | a719aa5f1631 |
permissions | -rw-r--r-- |
10 | 1 |
/* |
6716
71df48777dd1
6877202: Elements.getDocComment() is not getting JavaDocComments
jjg
parents:
5520
diff
changeset
|
2 |
* Copyright (c) 2005, 2010, 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 4813736 |
|
27 |
* @summary Additional functionality test of task and JSR 269 |
|
28 |
* @author Peter von der Ah\u00e9 |
|
2986 | 29 |
* @library ./lib |
10 | 30 |
* @run main TestJavacTaskScanner TestJavacTaskScanner.java |
31 |
*/ |
|
32 |
||
33 |
import com.sun.tools.javac.api.JavacTaskImpl; |
|
6716
71df48777dd1
6877202: Elements.getDocComment() is not getting JavaDocComments
jjg
parents:
5520
diff
changeset
|
34 |
import com.sun.tools.javac.parser.*; |
71df48777dd1
6877202: Elements.getDocComment() is not getting JavaDocComments
jjg
parents:
5520
diff
changeset
|
35 |
import com.sun.tools.javac.util.*; |
10 | 36 |
import java.io.*; |
5016
ff5e6791d0bb
6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
2986
diff
changeset
|
37 |
import java.net.*; |
10 | 38 |
import java.nio.*; |
5016
ff5e6791d0bb
6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
2986
diff
changeset
|
39 |
import java.nio.charset.Charset; |
ff5e6791d0bb
6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
2986
diff
changeset
|
40 |
import java.util.Arrays; |
10 | 41 |
import javax.lang.model.element.Element; |
42 |
import javax.lang.model.element.TypeElement; |
|
43 |
import javax.lang.model.type.DeclaredType; |
|
44 |
import javax.lang.model.type.TypeMirror; |
|
45 |
import javax.lang.model.util.Elements; |
|
46 |
import javax.lang.model.util.Types; |
|
47 |
import javax.tools.*; |
|
48 |
||
5016
ff5e6791d0bb
6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
2986
diff
changeset
|
49 |
import static javax.tools.StandardLocation.CLASS_PATH; |
ff5e6791d0bb
6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
2986
diff
changeset
|
50 |
import static javax.tools.StandardLocation.SOURCE_PATH; |
ff5e6791d0bb
6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
2986
diff
changeset
|
51 |
import static javax.tools.StandardLocation.CLASS_OUTPUT; |
ff5e6791d0bb
6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
2986
diff
changeset
|
52 |
|
2986 | 53 |
public class TestJavacTaskScanner extends ToolTester { |
10 | 54 |
|
55 |
final JavacTaskImpl task; |
|
56 |
final Elements elements; |
|
57 |
final Types types; |
|
58 |
||
2986 | 59 |
int numTokens; |
60 |
int numParseTypeElements; |
|
61 |
int numAllMembers; |
|
62 |
||
63 |
TestJavacTaskScanner(File file) { |
|
64 |
final Iterable<? extends JavaFileObject> compilationUnits = |
|
65 |
fm.getJavaFileObjects(new File[] {file}); |
|
5016
ff5e6791d0bb
6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
2986
diff
changeset
|
66 |
StandardJavaFileManager fm = getLocalFileManager(tool, null, null); |
2986 | 67 |
task = (JavacTaskImpl)tool.getTask(null, fm, null, null, null, compilationUnits); |
6716
71df48777dd1
6877202: Elements.getDocComment() is not getting JavaDocComments
jjg
parents:
5520
diff
changeset
|
68 |
task.getContext().put(ScannerFactory.scannerFactoryKey, |
2986 | 69 |
new MyScanner.Factory(task.getContext(), this)); |
10 | 70 |
elements = task.getElements(); |
71 |
types = task.getTypes(); |
|
72 |
} |
|
73 |
||
74 |
public void run() { |
|
75 |
Iterable<? extends TypeElement> toplevels; |
|
76 |
try { |
|
77 |
toplevels = task.enter(task.parse()); |
|
78 |
} catch (IOException ex) { |
|
79 |
throw new AssertionError(ex); |
|
80 |
} |
|
81 |
for (TypeElement clazz : toplevels) { |
|
82 |
System.out.format("Testing %s:%n%n", clazz.getSimpleName()); |
|
83 |
testParseType(clazz); |
|
84 |
testGetAllMembers(clazz); |
|
85 |
System.out.println(); |
|
86 |
System.out.println(); |
|
87 |
System.out.println(); |
|
88 |
} |
|
2986 | 89 |
|
90 |
System.out.println("#tokens: " + numTokens); |
|
91 |
System.out.println("#parseTypeElements: " + numParseTypeElements); |
|
92 |
System.out.println("#allMembers: " + numAllMembers); |
|
93 |
||
5016
ff5e6791d0bb
6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
2986
diff
changeset
|
94 |
check(numTokens, "#Tokens", 1222); |
2986 | 95 |
check(numParseTypeElements, "#parseTypeElements", 136); |
96 |
check(numAllMembers, "#allMembers", 67); |
|
97 |
} |
|
98 |
||
99 |
void check(int value, String name, int expected) { |
|
100 |
// allow some slop in the comparison to allow for minor edits in the |
|
101 |
// test and in the platform |
|
102 |
if (value < expected * 9 / 10) |
|
103 |
throw new Error(name + " lower than expected; expected " + expected + "; found: " + value); |
|
104 |
if (value > expected * 11 / 10) |
|
105 |
throw new Error(name + " higher than expected; expected " + expected + "; found: " + value); |
|
10 | 106 |
} |
107 |
||
108 |
void testParseType(TypeElement clazz) { |
|
109 |
DeclaredType type = (DeclaredType)task.parseType("List<String>", clazz); |
|
110 |
for (Element member : elements.getAllMembers((TypeElement)type.asElement())) { |
|
111 |
TypeMirror mt = types.asMemberOf(type, member); |
|
112 |
System.out.format("%s : %s -> %s%n", member.getSimpleName(), member.asType(), mt); |
|
2986 | 113 |
numParseTypeElements++; |
10 | 114 |
} |
115 |
} |
|
116 |
||
117 |
public static void main(String... args) throws IOException { |
|
118 |
String srcdir = System.getProperty("test.src"); |
|
2986 | 119 |
new TestJavacTaskScanner(new File(srcdir, args[0])).run(); |
10 | 120 |
} |
121 |
||
122 |
private void testGetAllMembers(TypeElement clazz) { |
|
123 |
for (Element member : elements.getAllMembers(clazz)) { |
|
2986 | 124 |
System.out.format("%s : %s%n", member.getSimpleName(), member.asType()); |
125 |
numAllMembers++; |
|
10 | 126 |
} |
127 |
} |
|
5016
ff5e6791d0bb
6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
2986
diff
changeset
|
128 |
|
ff5e6791d0bb
6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
2986
diff
changeset
|
129 |
/* 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
|
130 |
* 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
|
131 |
* 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
|
132 |
* 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
|
133 |
* 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
|
134 |
* 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
|
135 |
*/ |
ff5e6791d0bb
6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
2986
diff
changeset
|
136 |
public StandardJavaFileManager getLocalFileManager(JavaCompiler tool, |
ff5e6791d0bb
6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
2986
diff
changeset
|
137 |
DiagnosticListener<JavaFileObject> dl, |
ff5e6791d0bb
6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
2986
diff
changeset
|
138 |
Charset encoding) { |
ff5e6791d0bb
6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
2986
diff
changeset
|
139 |
File javac_classes; |
ff5e6791d0bb
6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
2986
diff
changeset
|
140 |
try { |
ff5e6791d0bb
6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
2986
diff
changeset
|
141 |
final String javacMainClass = "com/sun/tools/javac/Main.class"; |
ff5e6791d0bb
6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
2986
diff
changeset
|
142 |
URL url = getClass().getClassLoader().getResource(javacMainClass); |
ff5e6791d0bb
6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
2986
diff
changeset
|
143 |
if (url == null) |
ff5e6791d0bb
6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
2986
diff
changeset
|
144 |
throw new Error("can't locate javac classes"); |
ff5e6791d0bb
6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
2986
diff
changeset
|
145 |
URI uri = url.toURI(); |
ff5e6791d0bb
6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
2986
diff
changeset
|
146 |
String scheme = uri.getScheme(); |
ff5e6791d0bb
6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
2986
diff
changeset
|
147 |
String ssp = uri.getSchemeSpecificPart(); |
ff5e6791d0bb
6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
2986
diff
changeset
|
148 |
if (scheme.equals("jar")) { |
ff5e6791d0bb
6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
2986
diff
changeset
|
149 |
javac_classes = new File(new URI(ssp.substring(0, ssp.indexOf("!/")))); |
ff5e6791d0bb
6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
2986
diff
changeset
|
150 |
} else if (scheme.equals("file")) { |
ff5e6791d0bb
6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
2986
diff
changeset
|
151 |
javac_classes = new File(ssp.substring(0, ssp.indexOf(javacMainClass))); |
ff5e6791d0bb
6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
2986
diff
changeset
|
152 |
} else |
ff5e6791d0bb
6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
2986
diff
changeset
|
153 |
throw new Error("unknown URL: " + url); |
ff5e6791d0bb
6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
2986
diff
changeset
|
154 |
} catch (URISyntaxException e) { |
ff5e6791d0bb
6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
2986
diff
changeset
|
155 |
throw new Error(e); |
ff5e6791d0bb
6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
2986
diff
changeset
|
156 |
} |
ff5e6791d0bb
6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
2986
diff
changeset
|
157 |
System.err.println("javac_classes: " + javac_classes); |
ff5e6791d0bb
6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
2986
diff
changeset
|
158 |
|
ff5e6791d0bb
6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
2986
diff
changeset
|
159 |
StandardJavaFileManager fm = tool.getStandardFileManager(dl, null, encoding); |
ff5e6791d0bb
6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
2986
diff
changeset
|
160 |
try { |
ff5e6791d0bb
6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
2986
diff
changeset
|
161 |
fm.setLocation(SOURCE_PATH, Arrays.asList(test_src)); |
ff5e6791d0bb
6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
2986
diff
changeset
|
162 |
fm.setLocation(CLASS_PATH, Arrays.asList(test_classes, javac_classes)); |
ff5e6791d0bb
6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
2986
diff
changeset
|
163 |
fm.setLocation(CLASS_OUTPUT, Arrays.asList(test_classes)); |
ff5e6791d0bb
6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
2986
diff
changeset
|
164 |
} catch (IOException e) { |
ff5e6791d0bb
6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
2986
diff
changeset
|
165 |
throw new AssertionError(e); |
ff5e6791d0bb
6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
2986
diff
changeset
|
166 |
} |
ff5e6791d0bb
6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
2986
diff
changeset
|
167 |
return fm; |
ff5e6791d0bb
6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
2986
diff
changeset
|
168 |
} |
10 | 169 |
} |
170 |
||
171 |
class MyScanner extends Scanner { |
|
172 |
||
6716
71df48777dd1
6877202: Elements.getDocComment() is not getting JavaDocComments
jjg
parents:
5520
diff
changeset
|
173 |
public static class Factory extends ScannerFactory { |
2986 | 174 |
public Factory(Context context, TestJavacTaskScanner test) { |
10 | 175 |
super(context); |
2986 | 176 |
this.test = test; |
10 | 177 |
} |
178 |
||
179 |
@Override |
|
6716
71df48777dd1
6877202: Elements.getDocComment() is not getting JavaDocComments
jjg
parents:
5520
diff
changeset
|
180 |
public Scanner newScanner(CharSequence input, boolean keepDocComments) { |
71df48777dd1
6877202: Elements.getDocComment() is not getting JavaDocComments
jjg
parents:
5520
diff
changeset
|
181 |
assert !keepDocComments; |
10 | 182 |
if (input instanceof CharBuffer) { |
2986 | 183 |
return new MyScanner(this, (CharBuffer)input, test); |
10 | 184 |
} else { |
185 |
char[] array = input.toString().toCharArray(); |
|
6716
71df48777dd1
6877202: Elements.getDocComment() is not getting JavaDocComments
jjg
parents:
5520
diff
changeset
|
186 |
return newScanner(array, array.length, keepDocComments); |
10 | 187 |
} |
188 |
} |
|
189 |
||
190 |
@Override |
|
6716
71df48777dd1
6877202: Elements.getDocComment() is not getting JavaDocComments
jjg
parents:
5520
diff
changeset
|
191 |
public Scanner newScanner(char[] input, int inputLength, boolean keepDocComments) { |
71df48777dd1
6877202: Elements.getDocComment() is not getting JavaDocComments
jjg
parents:
5520
diff
changeset
|
192 |
assert !keepDocComments; |
2986 | 193 |
return new MyScanner(this, input, inputLength, test); |
10 | 194 |
} |
2986 | 195 |
|
196 |
private TestJavacTaskScanner test; |
|
10 | 197 |
} |
6716
71df48777dd1
6877202: Elements.getDocComment() is not getting JavaDocComments
jjg
parents:
5520
diff
changeset
|
198 |
protected MyScanner(ScannerFactory fac, CharBuffer buffer, TestJavacTaskScanner test) { |
10 | 199 |
super(fac, buffer); |
2986 | 200 |
this.test = test; |
10 | 201 |
} |
6716
71df48777dd1
6877202: Elements.getDocComment() is not getting JavaDocComments
jjg
parents:
5520
diff
changeset
|
202 |
protected MyScanner(ScannerFactory fac, char[] input, int inputLength, TestJavacTaskScanner test) { |
10 | 203 |
super(fac, input, inputLength); |
2986 | 204 |
this.test = test; |
10 | 205 |
} |
206 |
||
207 |
public void nextToken() { |
|
208 |
super.nextToken(); |
|
209 |
System.err.format("Saw token %s (%s)%n", token(), name()); |
|
2986 | 210 |
test.numTokens++; |
10 | 211 |
} |
2986 | 212 |
|
213 |
private TestJavacTaskScanner test; |
|
214 |
||
10 | 215 |
} |