TestJavacTaskScanner.java revision 3376:4c740bddc648
1199086Srpaulo/* 2262417Shselasky * Copyright (c) 2005, 2016, Oracle and/or its affiliates. All rights reserved. 3199086Srpaulo * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4199086Srpaulo * 5199086Srpaulo * This code is free software; you can redistribute it and/or modify it 6199086Srpaulo * under the terms of the GNU General Public License version 2 only, as 7199086Srpaulo * published by the Free Software Foundation. 8199086Srpaulo * 9199086Srpaulo * This code is distributed in the hope that it will be useful, but WITHOUT 10199086Srpaulo * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11199086Srpaulo * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12199086Srpaulo * version 2 for more details (a copy is included in the LICENSE file that 13199086Srpaulo * accompanied this code). 14199086Srpaulo * 15199086Srpaulo * You should have received a copy of the GNU General Public License version 16199086Srpaulo * 2 along with this work; if not, write to the Free Software Foundation, 17199086Srpaulo * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18199086Srpaulo * 19199086Srpaulo * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20199086Srpaulo * or visit www.oracle.com if you need additional information or have any 21199086Srpaulo * questions. 22199086Srpaulo */ 23199086Srpaulo 24199086Srpaulo/* 25199086Srpaulo * @test 26199086Srpaulo * @bug 4813736 8013256 27262417Shselasky * @summary Additional functionality test of task and JSR 269 28262417Shselasky * @author Peter von der Ah\u00e9 29262417Shselasky * @library ./lib 30262417Shselasky * @modules jdk.compiler/com.sun.tools.javac.api 31262417Shselasky * jdk.compiler/com.sun.tools.javac.code 32262417Shselasky * jdk.compiler/com.sun.tools.javac.parser 33262417Shselasky * jdk.compiler/com.sun.tools.javac.util 34262417Shselasky * @build ToolTester 35262417Shselasky * @run main TestJavacTaskScanner TestJavacTaskScanner.java 36262417Shselasky */ 37262417Shselasky 38262417Shselaskyimport com.sun.tools.javac.api.JavacTaskImpl; 39262417Shselaskyimport com.sun.tools.javac.parser.*; 40262417Shselaskyimport com.sun.tools.javac.parser.Tokens.Token; 41262417Shselaskyimport com.sun.tools.javac.util.*; 42262417Shselasky 43262417Shselaskyimport java.io.*; 44262417Shselaskyimport java.net.*; 45262417Shselaskyimport java.nio.*; 46262417Shselaskyimport java.nio.charset.Charset; 47262417Shselaskyimport java.util.Arrays; 48262417Shselasky 49262417Shselaskyimport javax.lang.model.element.Element; 50262417Shselaskyimport javax.lang.model.element.TypeElement; 51262417Shselaskyimport javax.lang.model.type.DeclaredType; 52262417Shselaskyimport javax.lang.model.type.TypeMirror; 53262417Shselaskyimport javax.lang.model.util.ElementFilter; 54262417Shselaskyimport javax.lang.model.util.Elements; 55262417Shselaskyimport javax.lang.model.util.Types; 56262417Shselaskyimport javax.tools.*; 57262417Shselasky 58262417Shselaskyimport static javax.tools.StandardLocation.CLASS_PATH; 59199086Srpauloimport static javax.tools.StandardLocation.SOURCE_PATH; 60199086Srpauloimport static javax.tools.StandardLocation.CLASS_OUTPUT; 61199086Srpaulo 62262417Shselaskypublic class TestJavacTaskScanner extends ToolTester { 63262417Shselasky 64199086Srpaulo final JavacTaskImpl task; 65262417Shselasky final Elements elements; 66199086Srpaulo final Types types; 67199086Srpaulo 68262417Shselasky int numTokens; 69199086Srpaulo int numParseTypeElements; 70199086Srpaulo int numAllMembers; 71199086Srpaulo 72262417Shselasky TestJavacTaskScanner(File file) { 73262417Shselasky final Iterable<? extends JavaFileObject> compilationUnits = 74199086Srpaulo fm.getJavaFileObjects(new File[] {file}); 75199086Srpaulo StandardJavaFileManager fm = getLocalFileManager(tool, null, null); 76199086Srpaulo java.util.List<String> options = Arrays.asList( 77199086Srpaulo "-XaddExports:jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED", 78199086Srpaulo "-XaddExports:jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED", 79199086Srpaulo "-XaddExports:jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED", 80199086Srpaulo "-XaddExports:jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED"); 81199086Srpaulo task = (JavacTaskImpl)tool.getTask(null, fm, null, options, null, compilationUnits); 82199086Srpaulo task.getContext().put(ScannerFactory.scannerFactoryKey, 83199086Srpaulo new MyScanner.Factory(task.getContext(), this)); 84262417Shselasky elements = task.getElements(); 85199086Srpaulo types = task.getTypes(); 86199086Srpaulo } 87199086Srpaulo 88199086Srpaulo public void run() { 89199086Srpaulo Iterable<? extends TypeElement> toplevels; 90199086Srpaulo toplevels = ElementFilter.typesIn(task.enter(task.parse())); 91199086Srpaulo for (TypeElement clazz : toplevels) { 92199086Srpaulo System.out.format("Testing %s:%n%n", clazz.getSimpleName()); 93199086Srpaulo testParseType(clazz); 94199086Srpaulo testGetAllMembers(clazz); 95199086Srpaulo System.out.println(); 96199086Srpaulo System.out.println(); 97199086Srpaulo System.out.println(); 98199086Srpaulo } 99262417Shselasky 100199086Srpaulo System.out.println("#tokens: " + numTokens); 101262417Shselasky System.out.println("#parseTypeElements: " + numParseTypeElements); 102199086Srpaulo System.out.println("#allMembers: " + numAllMembers); 103199086Srpaulo 104262417Shselasky check(numTokens, "#Tokens", 1054); 105262417Shselasky check(numParseTypeElements, "#parseTypeElements", 158); 106262417Shselasky check(numAllMembers, "#allMembers", 52); 107262417Shselasky } 108262417Shselasky 109262417Shselasky void check(int value, String name, int expected) { 110262417Shselasky // allow some slop in the comparison to allow for minor edits in the 111262417Shselasky // test and in the platform 112262417Shselasky if (value < expected * 9 / 10) 113262417Shselasky throw new Error(name + " lower than expected; expected " + expected + "; found: " + value); 114199086Srpaulo if (value > expected * 11 / 10) 115262417Shselasky throw new Error(name + " higher than expected; expected " + expected + "; found: " + value); 116262417Shselasky } 117199086Srpaulo 118199086Srpaulo void testParseType(TypeElement clazz) { 119262417Shselasky DeclaredType type = (DeclaredType)task.parseType("List<String>", clazz); 120199086Srpaulo for (Element member : elements.getAllMembers((TypeElement)type.asElement())) { 121199086Srpaulo TypeMirror mt = types.asMemberOf(type, member); 122262417Shselasky System.out.format("type#%d: %s : %s -> %s%n", 123262417Shselasky numParseTypeElements, member.getSimpleName(), member.asType(), mt); 124262417Shselasky numParseTypeElements++; 125262417Shselasky } 126262417Shselasky } 127262417Shselasky 128262417Shselasky public static void main(String... args) throws IOException { 129262417Shselasky String srcdir = System.getProperty("test.src"); 130199086Srpaulo try (TestJavacTaskScanner t = new TestJavacTaskScanner(new File(srcdir, args[0]))) { 131199086Srpaulo t.run(); 132199086Srpaulo } 133199086Srpaulo } 134199086Srpaulo 135199086Srpaulo private void testGetAllMembers(TypeElement clazz) { 136199086Srpaulo for (Element member : elements.getAllMembers(clazz)) { 137199086Srpaulo System.out.format("elem#%d: %s : %s%n", 138262417Shselasky numAllMembers, member.getSimpleName(), member.asType()); 139199086Srpaulo numAllMembers++; 140199086Srpaulo } 141199086Srpaulo } 142262417Shselasky 143262417Shselasky /* Similar to ToolTester.getFileManager, except that this version also ensures 144199086Srpaulo * javac classes will be available on the classpath. The javac classes are assumed 145262417Shselasky * to be on the classpath used to run this test (this is true when using jtreg). 146199086Srpaulo * The classes are found by obtaining the URL for a sample javac class, using 147262417Shselasky * getClassLoader().getResource(), and then deconstructing the URL to find the 148262417Shselasky * underlying directory or jar file to place on the classpath. 149262417Shselasky */ 150199086Srpaulo public StandardJavaFileManager getLocalFileManager(JavaCompiler tool, 151262417Shselasky DiagnosticListener<JavaFileObject> dl, 152262417Shselasky Charset encoding) { 153262417Shselasky StandardJavaFileManager fm = tool.getStandardFileManager(dl, null, encoding); 154199086Srpaulo try { 155262417Shselasky fm.setLocation(SOURCE_PATH, Arrays.asList(test_src)); 156262417Shselasky fm.setLocation(CLASS_PATH, test_class_path); 157262417Shselasky fm.setLocation(CLASS_OUTPUT, Arrays.asList(test_classes)); 158262417Shselasky } catch (IOException e) { 159262417Shselasky throw new AssertionError(e); 160199086Srpaulo } 161199086Srpaulo return fm; 162262417Shselasky } 163262417Shselasky} 164262417Shselasky 165262417Shselaskyclass MyScanner extends Scanner { 166262417Shselasky 167262417Shselasky public static class Factory extends ScannerFactory { 168199086Srpaulo public Factory(Context context, TestJavacTaskScanner test) { 169199086Srpaulo super(context); 170199086Srpaulo this.test = test; 171262417Shselasky } 172199086Srpaulo 173207077Sthompsa @Override 174199086Srpaulo public Scanner newScanner(CharSequence input, boolean keepDocComments) { 175199086Srpaulo if (input instanceof CharBuffer) { 176199086Srpaulo return new MyScanner(this, (CharBuffer)input, test); 177199086Srpaulo } else { 178199086Srpaulo char[] array = input.toString().toCharArray(); 179199086Srpaulo return newScanner(array, array.length, keepDocComments); 180199086Srpaulo } 181276701Shselasky } 182199086Srpaulo 183207077Sthompsa @Override 184199086Srpaulo public Scanner newScanner(char[] input, int inputLength, boolean keepDocComments) { 185199086Srpaulo return new MyScanner(this, input, inputLength, test); 186276701Shselasky } 187262417Shselasky 188199086Srpaulo private TestJavacTaskScanner test; 189199086Srpaulo } 190276701Shselasky protected MyScanner(ScannerFactory fac, CharBuffer buffer, TestJavacTaskScanner test) { 191199086Srpaulo super(fac, buffer); 192262417Shselasky this.test = test; 193262417Shselasky } 194199086Srpaulo protected MyScanner(ScannerFactory fac, char[] input, int inputLength, TestJavacTaskScanner test) { 195199086Srpaulo super(fac, input, inputLength); 196199086Srpaulo this.test = test; 197276701Shselasky } 198199086Srpaulo 199199086Srpaulo public void nextToken() { 200199086Srpaulo super.nextToken(); 201262417Shselasky Token tk = token(); 202276701Shselasky System.err.format("Saw token %s %n", tk.kind); 203262417Shselasky test.numTokens++; 204199086Srpaulo } 205262417Shselasky 206262417Shselasky private TestJavacTaskScanner test; 207276701Shselasky 208262417Shselasky} 209262417Shselasky