test/tools/javac/api/TestJavacTaskScanner.java

Wed, 27 Apr 2016 01:34:52 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:34:52 +0800
changeset 0
959103a6100f
child 2525
2eb010b6cb22
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/langtools/
changeset: 2573:53ca196be1ae
tag: jdk8u25-b17

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2005, 2014, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 */
aoqi@0 23
aoqi@0 24 /*
aoqi@0 25 * @test
aoqi@0 26 * @bug 4813736 8013256
aoqi@0 27 * @summary Additional functionality test of task and JSR 269
aoqi@0 28 * @author Peter von der Ah\u00e9
aoqi@0 29 * @library ./lib
aoqi@0 30 * @build ToolTester
aoqi@0 31 * @run main TestJavacTaskScanner TestJavacTaskScanner.java
aoqi@0 32 */
aoqi@0 33
aoqi@0 34 import com.sun.tools.javac.api.JavacTaskImpl;
aoqi@0 35 import com.sun.tools.javac.parser.*;
aoqi@0 36 import com.sun.tools.javac.parser.Tokens.Token;
aoqi@0 37 import com.sun.tools.javac.util.*;
aoqi@0 38 import java.io.*;
aoqi@0 39 import java.net.*;
aoqi@0 40 import java.nio.*;
aoqi@0 41 import java.nio.charset.Charset;
aoqi@0 42 import java.util.Arrays;
aoqi@0 43 import javax.lang.model.element.Element;
aoqi@0 44 import javax.lang.model.element.TypeElement;
aoqi@0 45 import javax.lang.model.type.DeclaredType;
aoqi@0 46 import javax.lang.model.type.TypeMirror;
aoqi@0 47 import javax.lang.model.util.Elements;
aoqi@0 48 import javax.lang.model.util.Types;
aoqi@0 49 import javax.tools.*;
aoqi@0 50
aoqi@0 51 import static javax.tools.StandardLocation.CLASS_PATH;
aoqi@0 52 import static javax.tools.StandardLocation.SOURCE_PATH;
aoqi@0 53 import static javax.tools.StandardLocation.CLASS_OUTPUT;
aoqi@0 54
aoqi@0 55 public class TestJavacTaskScanner extends ToolTester {
aoqi@0 56
aoqi@0 57 final JavacTaskImpl task;
aoqi@0 58 final Elements elements;
aoqi@0 59 final Types types;
aoqi@0 60
aoqi@0 61 int numTokens;
aoqi@0 62 int numParseTypeElements;
aoqi@0 63 int numAllMembers;
aoqi@0 64
aoqi@0 65 TestJavacTaskScanner(File file) {
aoqi@0 66 final Iterable<? extends JavaFileObject> compilationUnits =
aoqi@0 67 fm.getJavaFileObjects(new File[] {file});
aoqi@0 68 StandardJavaFileManager fm = getLocalFileManager(tool, null, null);
aoqi@0 69 task = (JavacTaskImpl)tool.getTask(null, fm, null, null, null, compilationUnits);
aoqi@0 70 task.getContext().put(ScannerFactory.scannerFactoryKey,
aoqi@0 71 new MyScanner.Factory(task.getContext(), this));
aoqi@0 72 elements = task.getElements();
aoqi@0 73 types = task.getTypes();
aoqi@0 74 }
aoqi@0 75
aoqi@0 76 public void run() {
aoqi@0 77 Iterable<? extends TypeElement> toplevels;
aoqi@0 78 try {
aoqi@0 79 toplevels = task.enter(task.parse());
aoqi@0 80 } catch (IOException ex) {
aoqi@0 81 throw new AssertionError(ex);
aoqi@0 82 }
aoqi@0 83 for (TypeElement clazz : toplevels) {
aoqi@0 84 System.out.format("Testing %s:%n%n", clazz.getSimpleName());
aoqi@0 85 testParseType(clazz);
aoqi@0 86 testGetAllMembers(clazz);
aoqi@0 87 System.out.println();
aoqi@0 88 System.out.println();
aoqi@0 89 System.out.println();
aoqi@0 90 }
aoqi@0 91
aoqi@0 92 System.out.println("#tokens: " + numTokens);
aoqi@0 93 System.out.println("#parseTypeElements: " + numParseTypeElements);
aoqi@0 94 System.out.println("#allMembers: " + numAllMembers);
aoqi@0 95
aoqi@0 96 check(numTokens, "#Tokens", 1222);
aoqi@0 97 check(numParseTypeElements, "#parseTypeElements", 158);
aoqi@0 98 check(numAllMembers, "#allMembers", 52);
aoqi@0 99 }
aoqi@0 100
aoqi@0 101 void check(int value, String name, int expected) {
aoqi@0 102 // allow some slop in the comparison to allow for minor edits in the
aoqi@0 103 // test and in the platform
aoqi@0 104 if (value < expected * 9 / 10)
aoqi@0 105 throw new Error(name + " lower than expected; expected " + expected + "; found: " + value);
aoqi@0 106 if (value > expected * 11 / 10)
aoqi@0 107 throw new Error(name + " higher than expected; expected " + expected + "; found: " + value);
aoqi@0 108 }
aoqi@0 109
aoqi@0 110 void testParseType(TypeElement clazz) {
aoqi@0 111 DeclaredType type = (DeclaredType)task.parseType("List<String>", clazz);
aoqi@0 112 for (Element member : elements.getAllMembers((TypeElement)type.asElement())) {
aoqi@0 113 TypeMirror mt = types.asMemberOf(type, member);
aoqi@0 114 System.out.format("type#%d: %s : %s -> %s%n",
aoqi@0 115 numParseTypeElements, member.getSimpleName(), member.asType(), mt);
aoqi@0 116 numParseTypeElements++;
aoqi@0 117 }
aoqi@0 118 }
aoqi@0 119
aoqi@0 120 public static void main(String... args) throws IOException {
aoqi@0 121 String srcdir = System.getProperty("test.src");
aoqi@0 122 new TestJavacTaskScanner(new File(srcdir, args[0])).run();
aoqi@0 123 }
aoqi@0 124
aoqi@0 125 private void testGetAllMembers(TypeElement clazz) {
aoqi@0 126 for (Element member : elements.getAllMembers(clazz)) {
aoqi@0 127 System.out.format("elem#%d: %s : %s%n",
aoqi@0 128 numAllMembers, member.getSimpleName(), member.asType());
aoqi@0 129 numAllMembers++;
aoqi@0 130 }
aoqi@0 131 }
aoqi@0 132
aoqi@0 133 /* Similar to ToolTester.getFileManager, except that this version also ensures
aoqi@0 134 * javac classes will be available on the classpath. The javac classes are assumed
aoqi@0 135 * to be on the classpath used to run this test (this is true when using jtreg).
aoqi@0 136 * The classes are found by obtaining the URL for a sample javac class, using
aoqi@0 137 * getClassLoader().getResource(), and then deconstructing the URL to find the
aoqi@0 138 * underlying directory or jar file to place on the classpath.
aoqi@0 139 */
aoqi@0 140 public StandardJavaFileManager getLocalFileManager(JavaCompiler tool,
aoqi@0 141 DiagnosticListener<JavaFileObject> dl,
aoqi@0 142 Charset encoding) {
aoqi@0 143 File javac_classes;
aoqi@0 144 try {
aoqi@0 145 final String javacMainClass = "com/sun/tools/javac/Main.class";
aoqi@0 146 URL url = getClass().getClassLoader().getResource(javacMainClass);
aoqi@0 147 if (url == null)
aoqi@0 148 throw new Error("can't locate javac classes");
aoqi@0 149 URI uri = url.toURI();
aoqi@0 150 String scheme = uri.getScheme();
aoqi@0 151 String ssp = uri.getSchemeSpecificPart();
aoqi@0 152 if (scheme.equals("jar")) {
aoqi@0 153 javac_classes = new File(new URI(ssp.substring(0, ssp.indexOf("!/"))));
aoqi@0 154 } else if (scheme.equals("file")) {
aoqi@0 155 javac_classes = new File(ssp.substring(0, ssp.indexOf(javacMainClass)));
aoqi@0 156 } else
aoqi@0 157 throw new Error("unknown URL: " + url);
aoqi@0 158 } catch (URISyntaxException e) {
aoqi@0 159 throw new Error(e);
aoqi@0 160 }
aoqi@0 161 System.err.println("javac_classes: " + javac_classes);
aoqi@0 162
aoqi@0 163 StandardJavaFileManager fm = tool.getStandardFileManager(dl, null, encoding);
aoqi@0 164 try {
aoqi@0 165 fm.setLocation(SOURCE_PATH, Arrays.asList(test_src));
aoqi@0 166 fm.setLocation(CLASS_PATH, join(test_class_path, Arrays.asList(javac_classes)));
aoqi@0 167 fm.setLocation(CLASS_OUTPUT, Arrays.asList(test_classes));
aoqi@0 168 } catch (IOException e) {
aoqi@0 169 throw new AssertionError(e);
aoqi@0 170 }
aoqi@0 171 return fm;
aoqi@0 172 }
aoqi@0 173 }
aoqi@0 174
aoqi@0 175 class MyScanner extends Scanner {
aoqi@0 176
aoqi@0 177 public static class Factory extends ScannerFactory {
aoqi@0 178 public Factory(Context context, TestJavacTaskScanner test) {
aoqi@0 179 super(context);
aoqi@0 180 this.test = test;
aoqi@0 181 }
aoqi@0 182
aoqi@0 183 @Override
aoqi@0 184 public Scanner newScanner(CharSequence input, boolean keepDocComments) {
aoqi@0 185 if (input instanceof CharBuffer) {
aoqi@0 186 return new MyScanner(this, (CharBuffer)input, test);
aoqi@0 187 } else {
aoqi@0 188 char[] array = input.toString().toCharArray();
aoqi@0 189 return newScanner(array, array.length, keepDocComments);
aoqi@0 190 }
aoqi@0 191 }
aoqi@0 192
aoqi@0 193 @Override
aoqi@0 194 public Scanner newScanner(char[] input, int inputLength, boolean keepDocComments) {
aoqi@0 195 return new MyScanner(this, input, inputLength, test);
aoqi@0 196 }
aoqi@0 197
aoqi@0 198 private TestJavacTaskScanner test;
aoqi@0 199 }
aoqi@0 200 protected MyScanner(ScannerFactory fac, CharBuffer buffer, TestJavacTaskScanner test) {
aoqi@0 201 super(fac, buffer);
aoqi@0 202 this.test = test;
aoqi@0 203 }
aoqi@0 204 protected MyScanner(ScannerFactory fac, char[] input, int inputLength, TestJavacTaskScanner test) {
aoqi@0 205 super(fac, input, inputLength);
aoqi@0 206 this.test = test;
aoqi@0 207 }
aoqi@0 208
aoqi@0 209 public void nextToken() {
aoqi@0 210 super.nextToken();
aoqi@0 211 Token tk = token();
aoqi@0 212 System.err.format("Saw token %s %n", tk.kind);
aoqi@0 213 test.numTokens++;
aoqi@0 214 }
aoqi@0 215
aoqi@0 216 private TestJavacTaskScanner test;
aoqi@0 217
aoqi@0 218 }

mercurial