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