test/tools/javac/api/TestJavacTaskScanner.java

Mon, 23 Sep 2013 17:27:38 +0400

author
kizune
date
Mon, 23 Sep 2013 17:27:38 +0400
changeset 2048
809a50f24d6f
parent 1755
ddb4a2bfcd82
child 2255
b2e4c5ca111f
permissions
-rw-r--r--

7154966: CRs found to be in Fixed state with no test and no noreg- keyword.
Reviewed-by: ksrini

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

mercurial