test/tools/javac/api/TestJavacTaskScanner.java

Fri, 22 Mar 2013 10:08:46 -0700

author
darcy
date
Fri, 22 Mar 2013 10:08:46 -0700
changeset 1657
f4500abff1fd
parent 1113
d346ab55031b
child 1711
4b0038f66d66
permissions
-rw-r--r--

7080464: langtools regression test failures when assertions are enabled
Reviewed-by: jjg

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

mercurial