0N/A/*
694N/A * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
553N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
553N/A * or visit www.oracle.com if you need additional information or have any
553N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A * @test
0N/A * @bug 4813736
0N/A * @summary Additional functionality test of task and JSR 269
0N/A * @author Peter von der Ah\u00e9
289N/A * @library ./lib
1451N/A * @build ToolTester
0N/A * @run main TestJavacTaskScanner TestJavacTaskScanner.java
0N/A */
0N/A
0N/Aimport com.sun.tools.javac.api.JavacTaskImpl;
694N/Aimport com.sun.tools.javac.parser.*;
694N/Aimport com.sun.tools.javac.util.*;
0N/Aimport java.io.*;
518N/Aimport java.net.*;
0N/Aimport java.nio.*;
518N/Aimport java.nio.charset.Charset;
518N/Aimport java.util.Arrays;
0N/Aimport javax.lang.model.element.Element;
0N/Aimport javax.lang.model.element.TypeElement;
0N/Aimport javax.lang.model.type.DeclaredType;
0N/Aimport javax.lang.model.type.TypeMirror;
0N/Aimport javax.lang.model.util.Elements;
0N/Aimport javax.lang.model.util.Types;
0N/Aimport javax.tools.*;
0N/A
518N/Aimport static javax.tools.StandardLocation.CLASS_PATH;
518N/Aimport static javax.tools.StandardLocation.SOURCE_PATH;
518N/Aimport static javax.tools.StandardLocation.CLASS_OUTPUT;
518N/A
289N/Apublic class TestJavacTaskScanner extends ToolTester {
0N/A
0N/A final JavacTaskImpl task;
0N/A final Elements elements;
0N/A final Types types;
0N/A
289N/A int numTokens;
289N/A int numParseTypeElements;
289N/A int numAllMembers;
289N/A
289N/A TestJavacTaskScanner(File file) {
289N/A final Iterable<? extends JavaFileObject> compilationUnits =
289N/A fm.getJavaFileObjects(new File[] {file});
518N/A StandardJavaFileManager fm = getLocalFileManager(tool, null, null);
289N/A task = (JavacTaskImpl)tool.getTask(null, fm, null, null, null, compilationUnits);
694N/A task.getContext().put(ScannerFactory.scannerFactoryKey,
289N/A new MyScanner.Factory(task.getContext(), this));
0N/A elements = task.getElements();
0N/A types = task.getTypes();
0N/A }
0N/A
0N/A public void run() {
0N/A Iterable<? extends TypeElement> toplevels;
0N/A try {
0N/A toplevels = task.enter(task.parse());
0N/A } catch (IOException ex) {
0N/A throw new AssertionError(ex);
0N/A }
0N/A for (TypeElement clazz : toplevels) {
0N/A System.out.format("Testing %s:%n%n", clazz.getSimpleName());
0N/A testParseType(clazz);
0N/A testGetAllMembers(clazz);
0N/A System.out.println();
0N/A System.out.println();
0N/A System.out.println();
0N/A }
289N/A
289N/A System.out.println("#tokens: " + numTokens);
289N/A System.out.println("#parseTypeElements: " + numParseTypeElements);
289N/A System.out.println("#allMembers: " + numAllMembers);
289N/A
518N/A check(numTokens, "#Tokens", 1222);
289N/A check(numParseTypeElements, "#parseTypeElements", 136);
289N/A check(numAllMembers, "#allMembers", 67);
289N/A }
289N/A
289N/A void check(int value, String name, int expected) {
289N/A // allow some slop in the comparison to allow for minor edits in the
289N/A // test and in the platform
289N/A if (value < expected * 9 / 10)
289N/A throw new Error(name + " lower than expected; expected " + expected + "; found: " + value);
289N/A if (value > expected * 11 / 10)
289N/A throw new Error(name + " higher than expected; expected " + expected + "; found: " + value);
0N/A }
0N/A
0N/A void testParseType(TypeElement clazz) {
0N/A DeclaredType type = (DeclaredType)task.parseType("List<String>", clazz);
0N/A for (Element member : elements.getAllMembers((TypeElement)type.asElement())) {
0N/A TypeMirror mt = types.asMemberOf(type, member);
0N/A System.out.format("%s : %s -> %s%n", member.getSimpleName(), member.asType(), mt);
289N/A numParseTypeElements++;
0N/A }
0N/A }
0N/A
0N/A public static void main(String... args) throws IOException {
0N/A String srcdir = System.getProperty("test.src");
289N/A new TestJavacTaskScanner(new File(srcdir, args[0])).run();
0N/A }
0N/A
0N/A private void testGetAllMembers(TypeElement clazz) {
0N/A for (Element member : elements.getAllMembers(clazz)) {
289N/A System.out.format("%s : %s%n", member.getSimpleName(), member.asType());
289N/A numAllMembers++;
0N/A }
0N/A }
518N/A
518N/A /* Similar to ToolTester.getFileManager, except that this version also ensures
518N/A * javac classes will be available on the classpath. The javac classes are assumed
518N/A * to be on the classpath used to run this test (this is true when using jtreg).
518N/A * The classes are found by obtaining the URL for a sample javac class, using
518N/A * getClassLoader().getResource(), and then deconstructing the URL to find the
518N/A * underlying directory or jar file to place on the classpath.
518N/A */
518N/A public StandardJavaFileManager getLocalFileManager(JavaCompiler tool,
518N/A DiagnosticListener<JavaFileObject> dl,
518N/A Charset encoding) {
518N/A File javac_classes;
518N/A try {
518N/A final String javacMainClass = "com/sun/tools/javac/Main.class";
518N/A URL url = getClass().getClassLoader().getResource(javacMainClass);
518N/A if (url == null)
518N/A throw new Error("can't locate javac classes");
518N/A URI uri = url.toURI();
518N/A String scheme = uri.getScheme();
518N/A String ssp = uri.getSchemeSpecificPart();
518N/A if (scheme.equals("jar")) {
518N/A javac_classes = new File(new URI(ssp.substring(0, ssp.indexOf("!/"))));
518N/A } else if (scheme.equals("file")) {
518N/A javac_classes = new File(ssp.substring(0, ssp.indexOf(javacMainClass)));
518N/A } else
518N/A throw new Error("unknown URL: " + url);
518N/A } catch (URISyntaxException e) {
518N/A throw new Error(e);
518N/A }
518N/A System.err.println("javac_classes: " + javac_classes);
518N/A
518N/A StandardJavaFileManager fm = tool.getStandardFileManager(dl, null, encoding);
518N/A try {
518N/A fm.setLocation(SOURCE_PATH, Arrays.asList(test_src));
1451N/A fm.setLocation(CLASS_PATH, join(test_class_path, Arrays.asList(javac_classes)));
518N/A fm.setLocation(CLASS_OUTPUT, Arrays.asList(test_classes));
518N/A } catch (IOException e) {
518N/A throw new AssertionError(e);
518N/A }
518N/A return fm;
518N/A }
0N/A}
0N/A
0N/Aclass MyScanner extends Scanner {
0N/A
694N/A public static class Factory extends ScannerFactory {
289N/A public Factory(Context context, TestJavacTaskScanner test) {
0N/A super(context);
289N/A this.test = test;
0N/A }
0N/A
0N/A @Override
694N/A public Scanner newScanner(CharSequence input, boolean keepDocComments) {
694N/A assert !keepDocComments;
0N/A if (input instanceof CharBuffer) {
289N/A return new MyScanner(this, (CharBuffer)input, test);
0N/A } else {
0N/A char[] array = input.toString().toCharArray();
694N/A return newScanner(array, array.length, keepDocComments);
0N/A }
0N/A }
0N/A
0N/A @Override
694N/A public Scanner newScanner(char[] input, int inputLength, boolean keepDocComments) {
694N/A assert !keepDocComments;
289N/A return new MyScanner(this, input, inputLength, test);
0N/A }
289N/A
289N/A private TestJavacTaskScanner test;
0N/A }
694N/A protected MyScanner(ScannerFactory fac, CharBuffer buffer, TestJavacTaskScanner test) {
0N/A super(fac, buffer);
289N/A this.test = test;
0N/A }
694N/A protected MyScanner(ScannerFactory fac, char[] input, int inputLength, TestJavacTaskScanner test) {
0N/A super(fac, input, inputLength);
289N/A this.test = test;
0N/A }
0N/A
0N/A public void nextToken() {
0N/A super.nextToken();
0N/A System.err.format("Saw token %s (%s)%n", token(), name());
289N/A test.numTokens++;
0N/A }
289N/A
289N/A private TestJavacTaskScanner test;
289N/A
0N/A}