test/tools/javac/api/TestJavacTaskScanner.java

Sat, 01 Dec 2007 00:00:00 +0000

author
duke
date
Sat, 01 Dec 2007 00:00:00 +0000
changeset 1
9a66ca7c79fa
child 290
d4828eba4939
permissions
-rw-r--r--

Initial load

     1 /*
     2  * Copyright 2005-2006 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    20  * CA 95054 USA or visit www.sun.com if you need additional information or
    21  * have any 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  * @ignore "misuse" of context breaks with 6358786
    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.nio.*;
    38 import javax.lang.model.element.Element;
    39 import javax.lang.model.element.TypeElement;
    40 import javax.lang.model.type.DeclaredType;
    41 import javax.lang.model.type.TypeMirror;
    42 import javax.lang.model.util.Elements;
    43 import javax.lang.model.util.Types;
    44 import javax.tools.*;
    45 import javax.tools.JavaFileManager;
    47 public class TestJavacTaskScanner implements Runnable {
    49     final JavacTaskImpl task;
    50     final Elements elements;
    51     final Types types;
    53     TestJavacTaskScanner(JavacTaskImpl task) {
    54         this.task = task;
    55         elements = task.getElements();
    56         types = task.getTypes();
    57     }
    59     public void run() {
    60         Iterable<? extends TypeElement> toplevels;
    61         try {
    62             toplevels = task.enter(task.parse());
    63         } catch (IOException ex) {
    64             throw new AssertionError(ex);
    65         }
    66         for (TypeElement clazz : toplevels) {
    67             System.out.format("Testing %s:%n%n", clazz.getSimpleName());
    68             testParseType(clazz);
    69             testGetAllMembers(clazz);
    70             System.out.println();
    71             System.out.println();
    72             System.out.println();
    73         }
    74     }
    76     void testParseType(TypeElement clazz) {
    77         DeclaredType type = (DeclaredType)task.parseType("List<String>", clazz);
    78         for (Element member : elements.getAllMembers((TypeElement)type.asElement())) {
    79             TypeMirror mt = types.asMemberOf(type, member);
    80             System.out.format("%s : %s -> %s%n", member.getSimpleName(), member.asType(), mt);
    81         }
    82     }
    84     public static void main(String... args) throws IOException {
    85         JavaCompilerTool tool = ToolProvider.defaultJavaCompiler();
    86         JavaFileManager fm = tool.getStandardFileManager();
    87         String srcdir = System.getProperty("test.src");
    88         File file = new File(srcdir, args[0]);
    89         JavacTaskImpl task = (JavacTaskImpl)tool.run(null, fm.getFileForInput(file.toString()));
    90         MyScanner.Factory.preRegister(task.getContext());
    91         TestJavacTaskScanner tester = new TestJavacTaskScanner(task);
    92         tester.run();
    93     }
    95     private void testGetAllMembers(TypeElement clazz) {
    96         for (Element member : elements.getAllMembers(clazz)) {
    97             System.out.format("%s : %s", member.getSimpleName(), member.asType());
    98         }
    99     }
   100 }
   102 class MyScanner extends Scanner {
   104     public static class Factory extends Scanner.Factory {
   105         public static void preRegister(final Context context) {
   106             context.put(scannerFactoryKey, new Context.Factory<Scanner.Factory>() {
   107                 public Factory make() {
   108                     return new Factory(context);
   109                 }
   110             });
   111         }
   112         public Factory(Context context) {
   113             super(context);
   114         }
   116         @Override
   117         public Scanner newScanner(CharSequence input) {
   118             if (input instanceof CharBuffer) {
   119                 return new MyScanner(this, (CharBuffer)input);
   120             } else {
   121                 char[] array = input.toString().toCharArray();
   122                 return newScanner(array, array.length);
   123             }
   124         }
   126         @Override
   127         public Scanner newScanner(char[] input, int inputLength) {
   128             return new MyScanner(this, input, inputLength);
   129         }
   130     }
   131     protected MyScanner(Factory fac, CharBuffer buffer) {
   132         super(fac, buffer);
   133     }
   134     protected MyScanner(Factory fac, char[] input, int inputLength) {
   135         super(fac, input, inputLength);
   136     }
   138     public void nextToken() {
   139         super.nextToken();
   140         System.err.format("Saw token %s (%s)%n", token(), name());
   141     }
   142 }

mercurial