test/tools/javac/api/TestJavacTaskScanner.java

Tue, 25 May 2010 15:54:51 -0700

author
ohair
date
Tue, 25 May 2010 15:54:51 -0700
changeset 554
9d9f26857129
parent 519
a23282f17d0b
child 695
3c9b64e55c5d
permissions
-rw-r--r--

6943119: Rebrand source copyright notices
Reviewed-by: darcy

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

mercurial