2007-12-01 00:00:00 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2005-2006 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
|
|
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
|
|
* have any questions.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* @test
|
|
|
|
* @bug 4813736
|
|
|
|
* @summary Additional functionality test of task and JSR 269
|
|
|
|
* @author Peter von der Ah\u00e9
|
2009-05-28 09:49:56 -07:00
|
|
|
* @library ./lib
|
2007-12-01 00:00:00 +00:00
|
|
|
* @run main TestJavacTaskScanner TestJavacTaskScanner.java
|
|
|
|
*/
|
|
|
|
|
|
|
|
import com.sun.tools.javac.api.JavacTaskImpl;
|
|
|
|
import com.sun.tools.javac.parser.*; // XXX
|
|
|
|
import com.sun.tools.javac.util.*; // XXX
|
|
|
|
import java.io.*;
|
|
|
|
import java.nio.*;
|
|
|
|
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.Elements;
|
|
|
|
import javax.lang.model.util.Types;
|
|
|
|
import javax.tools.*;
|
|
|
|
|
2009-05-28 09:49:56 -07:00
|
|
|
public class TestJavacTaskScanner extends ToolTester {
|
2007-12-01 00:00:00 +00:00
|
|
|
|
|
|
|
final JavacTaskImpl task;
|
|
|
|
final Elements elements;
|
|
|
|
final Types types;
|
|
|
|
|
2009-05-28 09:49:56 -07:00
|
|
|
int numTokens;
|
|
|
|
int numParseTypeElements;
|
|
|
|
int numAllMembers;
|
|
|
|
|
|
|
|
TestJavacTaskScanner(File file) {
|
|
|
|
final Iterable<? extends JavaFileObject> compilationUnits =
|
|
|
|
fm.getJavaFileObjects(new File[] {file});
|
|
|
|
task = (JavacTaskImpl)tool.getTask(null, fm, null, null, null, compilationUnits);
|
|
|
|
task.getContext().put(Scanner.Factory.scannerFactoryKey,
|
|
|
|
new MyScanner.Factory(task.getContext(), this));
|
2007-12-01 00:00:00 +00:00
|
|
|
elements = task.getElements();
|
|
|
|
types = task.getTypes();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void run() {
|
|
|
|
Iterable<? extends TypeElement> toplevels;
|
|
|
|
try {
|
|
|
|
toplevels = task.enter(task.parse());
|
|
|
|
} catch (IOException ex) {
|
|
|
|
throw new AssertionError(ex);
|
|
|
|
}
|
|
|
|
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();
|
|
|
|
}
|
2009-05-28 09:49:56 -07:00
|
|
|
|
|
|
|
System.out.println("#tokens: " + numTokens);
|
|
|
|
System.out.println("#parseTypeElements: " + numParseTypeElements);
|
|
|
|
System.out.println("#allMembers: " + numAllMembers);
|
|
|
|
|
|
|
|
check(numTokens, "#Tokens", 891);
|
|
|
|
check(numParseTypeElements, "#parseTypeElements", 136);
|
|
|
|
check(numAllMembers, "#allMembers", 67);
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2007-12-01 00:00:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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("%s : %s -> %s%n", member.getSimpleName(), member.asType(), mt);
|
2009-05-28 09:49:56 -07:00
|
|
|
numParseTypeElements++;
|
2007-12-01 00:00:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void main(String... args) throws IOException {
|
|
|
|
String srcdir = System.getProperty("test.src");
|
2009-05-28 09:49:56 -07:00
|
|
|
new TestJavacTaskScanner(new File(srcdir, args[0])).run();
|
2007-12-01 00:00:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void testGetAllMembers(TypeElement clazz) {
|
|
|
|
for (Element member : elements.getAllMembers(clazz)) {
|
2009-05-28 09:49:56 -07:00
|
|
|
System.out.format("%s : %s%n", member.getSimpleName(), member.asType());
|
|
|
|
numAllMembers++;
|
2007-12-01 00:00:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class MyScanner extends Scanner {
|
|
|
|
|
|
|
|
public static class Factory extends Scanner.Factory {
|
2009-05-28 09:49:56 -07:00
|
|
|
public Factory(Context context, TestJavacTaskScanner test) {
|
2007-12-01 00:00:00 +00:00
|
|
|
super(context);
|
2009-05-28 09:49:56 -07:00
|
|
|
this.test = test;
|
2007-12-01 00:00:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Scanner newScanner(CharSequence input) {
|
|
|
|
if (input instanceof CharBuffer) {
|
2009-05-28 09:49:56 -07:00
|
|
|
return new MyScanner(this, (CharBuffer)input, test);
|
2007-12-01 00:00:00 +00:00
|
|
|
} else {
|
|
|
|
char[] array = input.toString().toCharArray();
|
|
|
|
return newScanner(array, array.length);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Scanner newScanner(char[] input, int inputLength) {
|
2009-05-28 09:49:56 -07:00
|
|
|
return new MyScanner(this, input, inputLength, test);
|
2007-12-01 00:00:00 +00:00
|
|
|
}
|
2009-05-28 09:49:56 -07:00
|
|
|
|
|
|
|
private TestJavacTaskScanner test;
|
2007-12-01 00:00:00 +00:00
|
|
|
}
|
2009-05-28 09:49:56 -07:00
|
|
|
protected MyScanner(Factory fac, CharBuffer buffer, TestJavacTaskScanner test) {
|
2007-12-01 00:00:00 +00:00
|
|
|
super(fac, buffer);
|
2009-05-28 09:49:56 -07:00
|
|
|
this.test = test;
|
2007-12-01 00:00:00 +00:00
|
|
|
}
|
2009-05-28 09:49:56 -07:00
|
|
|
protected MyScanner(Factory fac, char[] input, int inputLength, TestJavacTaskScanner test) {
|
2007-12-01 00:00:00 +00:00
|
|
|
super(fac, input, inputLength);
|
2009-05-28 09:49:56 -07:00
|
|
|
this.test = test;
|
2007-12-01 00:00:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void nextToken() {
|
|
|
|
super.nextToken();
|
|
|
|
System.err.format("Saw token %s (%s)%n", token(), name());
|
2009-05-28 09:49:56 -07:00
|
|
|
test.numTokens++;
|
2007-12-01 00:00:00 +00:00
|
|
|
}
|
2009-05-28 09:49:56 -07:00
|
|
|
|
|
|
|
private TestJavacTaskScanner test;
|
|
|
|
|
2007-12-01 00:00:00 +00:00
|
|
|
}
|