test/tools/javac/api/TestJavacTaskScanner.java

Mon, 06 Sep 2010 12:55:09 -0700

author
jjg
date
Mon, 06 Sep 2010 12:55:09 -0700
changeset 672
ea54372637a5
parent 554
9d9f26857129
child 695
3c9b64e55c5d
permissions
-rw-r--r--

6930507: Symbols for anonymous and local classes made too late for use by java tree API
Reviewed-by: mcimadamore

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

mercurial