Mon, 31 Aug 2015 14:49:47 -0700
Merge
1 /*
2 * Copyright (c) 2005, 2014, Oracle and/or its affiliates. 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 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.
22 */
24 /*
25 * @test
26 * @bug 4813736 8013256
27 * @summary Additional functionality test of task and JSR 269
28 * @author Peter von der Ah\u00e9
29 * @library ./lib
30 * @build ToolTester
31 * @run main TestJavacTaskScanner TestJavacTaskScanner.java
32 */
34 import com.sun.tools.javac.api.JavacTaskImpl;
35 import com.sun.tools.javac.parser.*;
36 import com.sun.tools.javac.parser.Tokens.Token;
37 import com.sun.tools.javac.util.*;
38 import java.io.*;
39 import java.net.*;
40 import java.nio.*;
41 import java.nio.charset.Charset;
42 import java.util.Arrays;
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.*;
51 import static javax.tools.StandardLocation.CLASS_PATH;
52 import static javax.tools.StandardLocation.SOURCE_PATH;
53 import static javax.tools.StandardLocation.CLASS_OUTPUT;
55 public class TestJavacTaskScanner extends ToolTester {
57 final JavacTaskImpl task;
58 final Elements elements;
59 final Types types;
61 int numTokens;
62 int numParseTypeElements;
63 int numAllMembers;
65 TestJavacTaskScanner(File file) {
66 final Iterable<? extends JavaFileObject> compilationUnits =
67 fm.getJavaFileObjects(new File[] {file});
68 StandardJavaFileManager fm = getLocalFileManager(tool, null, null);
69 task = (JavacTaskImpl)tool.getTask(null, fm, null, null, null, compilationUnits);
70 task.getContext().put(ScannerFactory.scannerFactoryKey,
71 new MyScanner.Factory(task.getContext(), this));
72 elements = task.getElements();
73 types = task.getTypes();
74 }
76 public void run() {
77 Iterable<? extends TypeElement> toplevels;
78 try {
79 toplevels = task.enter(task.parse());
80 } catch (IOException ex) {
81 throw new AssertionError(ex);
82 }
83 for (TypeElement clazz : toplevels) {
84 System.out.format("Testing %s:%n%n", clazz.getSimpleName());
85 testParseType(clazz);
86 testGetAllMembers(clazz);
87 System.out.println();
88 System.out.println();
89 System.out.println();
90 }
92 System.out.println("#tokens: " + numTokens);
93 System.out.println("#parseTypeElements: " + numParseTypeElements);
94 System.out.println("#allMembers: " + numAllMembers);
96 check(numTokens, "#Tokens", 1222);
97 check(numParseTypeElements, "#parseTypeElements", 158);
98 check(numAllMembers, "#allMembers", 52);
99 }
101 void check(int value, String name, int expected) {
102 // allow some slop in the comparison to allow for minor edits in the
103 // test and in the platform
104 if (value < expected * 9 / 10)
105 throw new Error(name + " lower than expected; expected " + expected + "; found: " + value);
106 if (value > expected * 11 / 10)
107 throw new Error(name + " higher than expected; expected " + expected + "; found: " + value);
108 }
110 void testParseType(TypeElement clazz) {
111 DeclaredType type = (DeclaredType)task.parseType("List<String>", clazz);
112 for (Element member : elements.getAllMembers((TypeElement)type.asElement())) {
113 TypeMirror mt = types.asMemberOf(type, member);
114 System.out.format("type#%d: %s : %s -> %s%n",
115 numParseTypeElements, member.getSimpleName(), member.asType(), mt);
116 numParseTypeElements++;
117 }
118 }
120 public static void main(String... args) throws IOException {
121 String srcdir = System.getProperty("test.src");
122 new TestJavacTaskScanner(new File(srcdir, args[0])).run();
123 }
125 private void testGetAllMembers(TypeElement clazz) {
126 for (Element member : elements.getAllMembers(clazz)) {
127 System.out.format("elem#%d: %s : %s%n",
128 numAllMembers, member.getSimpleName(), member.asType());
129 numAllMembers++;
130 }
131 }
133 /* Similar to ToolTester.getFileManager, except that this version also ensures
134 * javac classes will be available on the classpath. The javac classes are assumed
135 * to be on the classpath used to run this test (this is true when using jtreg).
136 * The classes are found by obtaining the URL for a sample javac class, using
137 * getClassLoader().getResource(), and then deconstructing the URL to find the
138 * underlying directory or jar file to place on the classpath.
139 */
140 public StandardJavaFileManager getLocalFileManager(JavaCompiler tool,
141 DiagnosticListener<JavaFileObject> dl,
142 Charset encoding) {
143 File javac_classes;
144 try {
145 final String javacMainClass = "com/sun/tools/javac/Main.class";
146 URL url = getClass().getClassLoader().getResource(javacMainClass);
147 if (url == null)
148 throw new Error("can't locate javac classes");
149 URI uri = url.toURI();
150 String scheme = uri.getScheme();
151 String ssp = uri.getSchemeSpecificPart();
152 if (scheme.equals("jar")) {
153 javac_classes = new File(new URI(ssp.substring(0, ssp.indexOf("!/"))));
154 } else if (scheme.equals("file")) {
155 javac_classes = new File(ssp.substring(0, ssp.indexOf(javacMainClass)));
156 } else
157 throw new Error("unknown URL: " + url);
158 } catch (URISyntaxException e) {
159 throw new Error(e);
160 }
161 System.err.println("javac_classes: " + javac_classes);
163 StandardJavaFileManager fm = tool.getStandardFileManager(dl, null, encoding);
164 try {
165 fm.setLocation(SOURCE_PATH, Arrays.asList(test_src));
166 fm.setLocation(CLASS_PATH, join(test_class_path, Arrays.asList(javac_classes)));
167 fm.setLocation(CLASS_OUTPUT, Arrays.asList(test_classes));
168 } catch (IOException e) {
169 throw new AssertionError(e);
170 }
171 return fm;
172 }
173 }
175 class MyScanner extends Scanner {
177 public static class Factory extends ScannerFactory {
178 public Factory(Context context, TestJavacTaskScanner test) {
179 super(context);
180 this.test = test;
181 }
183 @Override
184 public Scanner newScanner(CharSequence input, boolean keepDocComments) {
185 if (input instanceof CharBuffer) {
186 return new MyScanner(this, (CharBuffer)input, test);
187 } else {
188 char[] array = input.toString().toCharArray();
189 return newScanner(array, array.length, keepDocComments);
190 }
191 }
193 @Override
194 public Scanner newScanner(char[] input, int inputLength, boolean keepDocComments) {
195 return new MyScanner(this, input, inputLength, test);
196 }
198 private TestJavacTaskScanner test;
199 }
200 protected MyScanner(ScannerFactory fac, CharBuffer buffer, TestJavacTaskScanner test) {
201 super(fac, buffer);
202 this.test = test;
203 }
204 protected MyScanner(ScannerFactory fac, char[] input, int inputLength, TestJavacTaskScanner test) {
205 super(fac, input, inputLength);
206 this.test = test;
207 }
209 public void nextToken() {
210 super.nextToken();
211 Token tk = token();
212 System.err.format("Saw token %s %n", tk.kind);
213 test.numTokens++;
214 }
216 private TestJavacTaskScanner test;
218 }