1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/test/tools/javac/api/TestJavacTaskScanner.java Wed Apr 27 01:34:52 2016 +0800 1.3 @@ -0,0 +1,218 @@ 1.4 +/* 1.5 + * Copyright (c) 2005, 2014, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.23 + * or visit www.oracle.com if you need additional information or have any 1.24 + * questions. 1.25 + */ 1.26 + 1.27 +/* 1.28 + * @test 1.29 + * @bug 4813736 8013256 1.30 + * @summary Additional functionality test of task and JSR 269 1.31 + * @author Peter von der Ah\u00e9 1.32 + * @library ./lib 1.33 + * @build ToolTester 1.34 + * @run main TestJavacTaskScanner TestJavacTaskScanner.java 1.35 + */ 1.36 + 1.37 +import com.sun.tools.javac.api.JavacTaskImpl; 1.38 +import com.sun.tools.javac.parser.*; 1.39 +import com.sun.tools.javac.parser.Tokens.Token; 1.40 +import com.sun.tools.javac.util.*; 1.41 +import java.io.*; 1.42 +import java.net.*; 1.43 +import java.nio.*; 1.44 +import java.nio.charset.Charset; 1.45 +import java.util.Arrays; 1.46 +import javax.lang.model.element.Element; 1.47 +import javax.lang.model.element.TypeElement; 1.48 +import javax.lang.model.type.DeclaredType; 1.49 +import javax.lang.model.type.TypeMirror; 1.50 +import javax.lang.model.util.Elements; 1.51 +import javax.lang.model.util.Types; 1.52 +import javax.tools.*; 1.53 + 1.54 +import static javax.tools.StandardLocation.CLASS_PATH; 1.55 +import static javax.tools.StandardLocation.SOURCE_PATH; 1.56 +import static javax.tools.StandardLocation.CLASS_OUTPUT; 1.57 + 1.58 +public class TestJavacTaskScanner extends ToolTester { 1.59 + 1.60 + final JavacTaskImpl task; 1.61 + final Elements elements; 1.62 + final Types types; 1.63 + 1.64 + int numTokens; 1.65 + int numParseTypeElements; 1.66 + int numAllMembers; 1.67 + 1.68 + TestJavacTaskScanner(File file) { 1.69 + final Iterable<? extends JavaFileObject> compilationUnits = 1.70 + fm.getJavaFileObjects(new File[] {file}); 1.71 + StandardJavaFileManager fm = getLocalFileManager(tool, null, null); 1.72 + task = (JavacTaskImpl)tool.getTask(null, fm, null, null, null, compilationUnits); 1.73 + task.getContext().put(ScannerFactory.scannerFactoryKey, 1.74 + new MyScanner.Factory(task.getContext(), this)); 1.75 + elements = task.getElements(); 1.76 + types = task.getTypes(); 1.77 + } 1.78 + 1.79 + public void run() { 1.80 + Iterable<? extends TypeElement> toplevels; 1.81 + try { 1.82 + toplevels = task.enter(task.parse()); 1.83 + } catch (IOException ex) { 1.84 + throw new AssertionError(ex); 1.85 + } 1.86 + for (TypeElement clazz : toplevels) { 1.87 + System.out.format("Testing %s:%n%n", clazz.getSimpleName()); 1.88 + testParseType(clazz); 1.89 + testGetAllMembers(clazz); 1.90 + System.out.println(); 1.91 + System.out.println(); 1.92 + System.out.println(); 1.93 + } 1.94 + 1.95 + System.out.println("#tokens: " + numTokens); 1.96 + System.out.println("#parseTypeElements: " + numParseTypeElements); 1.97 + System.out.println("#allMembers: " + numAllMembers); 1.98 + 1.99 + check(numTokens, "#Tokens", 1222); 1.100 + check(numParseTypeElements, "#parseTypeElements", 158); 1.101 + check(numAllMembers, "#allMembers", 52); 1.102 + } 1.103 + 1.104 + void check(int value, String name, int expected) { 1.105 + // allow some slop in the comparison to allow for minor edits in the 1.106 + // test and in the platform 1.107 + if (value < expected * 9 / 10) 1.108 + throw new Error(name + " lower than expected; expected " + expected + "; found: " + value); 1.109 + if (value > expected * 11 / 10) 1.110 + throw new Error(name + " higher than expected; expected " + expected + "; found: " + value); 1.111 + } 1.112 + 1.113 + void testParseType(TypeElement clazz) { 1.114 + DeclaredType type = (DeclaredType)task.parseType("List<String>", clazz); 1.115 + for (Element member : elements.getAllMembers((TypeElement)type.asElement())) { 1.116 + TypeMirror mt = types.asMemberOf(type, member); 1.117 + System.out.format("type#%d: %s : %s -> %s%n", 1.118 + numParseTypeElements, member.getSimpleName(), member.asType(), mt); 1.119 + numParseTypeElements++; 1.120 + } 1.121 + } 1.122 + 1.123 + public static void main(String... args) throws IOException { 1.124 + String srcdir = System.getProperty("test.src"); 1.125 + new TestJavacTaskScanner(new File(srcdir, args[0])).run(); 1.126 + } 1.127 + 1.128 + private void testGetAllMembers(TypeElement clazz) { 1.129 + for (Element member : elements.getAllMembers(clazz)) { 1.130 + System.out.format("elem#%d: %s : %s%n", 1.131 + numAllMembers, member.getSimpleName(), member.asType()); 1.132 + numAllMembers++; 1.133 + } 1.134 + } 1.135 + 1.136 + /* Similar to ToolTester.getFileManager, except that this version also ensures 1.137 + * javac classes will be available on the classpath. The javac classes are assumed 1.138 + * to be on the classpath used to run this test (this is true when using jtreg). 1.139 + * The classes are found by obtaining the URL for a sample javac class, using 1.140 + * getClassLoader().getResource(), and then deconstructing the URL to find the 1.141 + * underlying directory or jar file to place on the classpath. 1.142 + */ 1.143 + public StandardJavaFileManager getLocalFileManager(JavaCompiler tool, 1.144 + DiagnosticListener<JavaFileObject> dl, 1.145 + Charset encoding) { 1.146 + File javac_classes; 1.147 + try { 1.148 + final String javacMainClass = "com/sun/tools/javac/Main.class"; 1.149 + URL url = getClass().getClassLoader().getResource(javacMainClass); 1.150 + if (url == null) 1.151 + throw new Error("can't locate javac classes"); 1.152 + URI uri = url.toURI(); 1.153 + String scheme = uri.getScheme(); 1.154 + String ssp = uri.getSchemeSpecificPart(); 1.155 + if (scheme.equals("jar")) { 1.156 + javac_classes = new File(new URI(ssp.substring(0, ssp.indexOf("!/")))); 1.157 + } else if (scheme.equals("file")) { 1.158 + javac_classes = new File(ssp.substring(0, ssp.indexOf(javacMainClass))); 1.159 + } else 1.160 + throw new Error("unknown URL: " + url); 1.161 + } catch (URISyntaxException e) { 1.162 + throw new Error(e); 1.163 + } 1.164 + System.err.println("javac_classes: " + javac_classes); 1.165 + 1.166 + StandardJavaFileManager fm = tool.getStandardFileManager(dl, null, encoding); 1.167 + try { 1.168 + fm.setLocation(SOURCE_PATH, Arrays.asList(test_src)); 1.169 + fm.setLocation(CLASS_PATH, join(test_class_path, Arrays.asList(javac_classes))); 1.170 + fm.setLocation(CLASS_OUTPUT, Arrays.asList(test_classes)); 1.171 + } catch (IOException e) { 1.172 + throw new AssertionError(e); 1.173 + } 1.174 + return fm; 1.175 + } 1.176 +} 1.177 + 1.178 +class MyScanner extends Scanner { 1.179 + 1.180 + public static class Factory extends ScannerFactory { 1.181 + public Factory(Context context, TestJavacTaskScanner test) { 1.182 + super(context); 1.183 + this.test = test; 1.184 + } 1.185 + 1.186 + @Override 1.187 + public Scanner newScanner(CharSequence input, boolean keepDocComments) { 1.188 + if (input instanceof CharBuffer) { 1.189 + return new MyScanner(this, (CharBuffer)input, test); 1.190 + } else { 1.191 + char[] array = input.toString().toCharArray(); 1.192 + return newScanner(array, array.length, keepDocComments); 1.193 + } 1.194 + } 1.195 + 1.196 + @Override 1.197 + public Scanner newScanner(char[] input, int inputLength, boolean keepDocComments) { 1.198 + return new MyScanner(this, input, inputLength, test); 1.199 + } 1.200 + 1.201 + private TestJavacTaskScanner test; 1.202 + } 1.203 + protected MyScanner(ScannerFactory fac, CharBuffer buffer, TestJavacTaskScanner test) { 1.204 + super(fac, buffer); 1.205 + this.test = test; 1.206 + } 1.207 + protected MyScanner(ScannerFactory fac, char[] input, int inputLength, TestJavacTaskScanner test) { 1.208 + super(fac, input, inputLength); 1.209 + this.test = test; 1.210 + } 1.211 + 1.212 + public void nextToken() { 1.213 + super.nextToken(); 1.214 + Token tk = token(); 1.215 + System.err.format("Saw token %s %n", tk.kind); 1.216 + test.numTokens++; 1.217 + } 1.218 + 1.219 + private TestJavacTaskScanner test; 1.220 + 1.221 +}