test/tools/javac/api/TestJavacTaskScanner.java

changeset 1
9a66ca7c79fa
child 290
d4828eba4939
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/tools/javac/api/TestJavacTaskScanner.java	Sat Dec 01 00:00:00 2007 +0000
     1.3 @@ -0,0 +1,142 @@
     1.4 +/*
     1.5 + * Copyright 2005-2006 Sun Microsystems, Inc.  All Rights Reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    1.23 + * CA 95054 USA or visit www.sun.com if you need additional information or
    1.24 + * have any questions.
    1.25 + */
    1.26 +
    1.27 +/*
    1.28 + * @test
    1.29 + * @bug     4813736
    1.30 + * @summary Additional functionality test of task and JSR 269
    1.31 + * @author  Peter von der Ah\u00e9
    1.32 + * @ignore "misuse" of context breaks with 6358786
    1.33 + * @run main TestJavacTaskScanner TestJavacTaskScanner.java
    1.34 + */
    1.35 +
    1.36 +import com.sun.tools.javac.api.JavacTaskImpl;
    1.37 +import com.sun.tools.javac.parser.*; // XXX
    1.38 +import com.sun.tools.javac.util.*; // XXX
    1.39 +import java.io.*;
    1.40 +import java.nio.*;
    1.41 +import javax.lang.model.element.Element;
    1.42 +import javax.lang.model.element.TypeElement;
    1.43 +import javax.lang.model.type.DeclaredType;
    1.44 +import javax.lang.model.type.TypeMirror;
    1.45 +import javax.lang.model.util.Elements;
    1.46 +import javax.lang.model.util.Types;
    1.47 +import javax.tools.*;
    1.48 +import javax.tools.JavaFileManager;
    1.49 +
    1.50 +public class TestJavacTaskScanner implements Runnable {
    1.51 +
    1.52 +    final JavacTaskImpl task;
    1.53 +    final Elements elements;
    1.54 +    final Types types;
    1.55 +
    1.56 +    TestJavacTaskScanner(JavacTaskImpl task) {
    1.57 +        this.task = task;
    1.58 +        elements = task.getElements();
    1.59 +        types = task.getTypes();
    1.60 +    }
    1.61 +
    1.62 +    public void run() {
    1.63 +        Iterable<? extends TypeElement> toplevels;
    1.64 +        try {
    1.65 +            toplevels = task.enter(task.parse());
    1.66 +        } catch (IOException ex) {
    1.67 +            throw new AssertionError(ex);
    1.68 +        }
    1.69 +        for (TypeElement clazz : toplevels) {
    1.70 +            System.out.format("Testing %s:%n%n", clazz.getSimpleName());
    1.71 +            testParseType(clazz);
    1.72 +            testGetAllMembers(clazz);
    1.73 +            System.out.println();
    1.74 +            System.out.println();
    1.75 +            System.out.println();
    1.76 +        }
    1.77 +    }
    1.78 +
    1.79 +    void testParseType(TypeElement clazz) {
    1.80 +        DeclaredType type = (DeclaredType)task.parseType("List<String>", clazz);
    1.81 +        for (Element member : elements.getAllMembers((TypeElement)type.asElement())) {
    1.82 +            TypeMirror mt = types.asMemberOf(type, member);
    1.83 +            System.out.format("%s : %s -> %s%n", member.getSimpleName(), member.asType(), mt);
    1.84 +        }
    1.85 +    }
    1.86 +
    1.87 +    public static void main(String... args) throws IOException {
    1.88 +        JavaCompilerTool tool = ToolProvider.defaultJavaCompiler();
    1.89 +        JavaFileManager fm = tool.getStandardFileManager();
    1.90 +        String srcdir = System.getProperty("test.src");
    1.91 +        File file = new File(srcdir, args[0]);
    1.92 +        JavacTaskImpl task = (JavacTaskImpl)tool.run(null, fm.getFileForInput(file.toString()));
    1.93 +        MyScanner.Factory.preRegister(task.getContext());
    1.94 +        TestJavacTaskScanner tester = new TestJavacTaskScanner(task);
    1.95 +        tester.run();
    1.96 +    }
    1.97 +
    1.98 +    private void testGetAllMembers(TypeElement clazz) {
    1.99 +        for (Element member : elements.getAllMembers(clazz)) {
   1.100 +            System.out.format("%s : %s", member.getSimpleName(), member.asType());
   1.101 +        }
   1.102 +    }
   1.103 +}
   1.104 +
   1.105 +class MyScanner extends Scanner {
   1.106 +
   1.107 +    public static class Factory extends Scanner.Factory {
   1.108 +        public static void preRegister(final Context context) {
   1.109 +            context.put(scannerFactoryKey, new Context.Factory<Scanner.Factory>() {
   1.110 +                public Factory make() {
   1.111 +                    return new Factory(context);
   1.112 +                }
   1.113 +            });
   1.114 +        }
   1.115 +        public Factory(Context context) {
   1.116 +            super(context);
   1.117 +        }
   1.118 +
   1.119 +        @Override
   1.120 +        public Scanner newScanner(CharSequence input) {
   1.121 +            if (input instanceof CharBuffer) {
   1.122 +                return new MyScanner(this, (CharBuffer)input);
   1.123 +            } else {
   1.124 +                char[] array = input.toString().toCharArray();
   1.125 +                return newScanner(array, array.length);
   1.126 +            }
   1.127 +        }
   1.128 +
   1.129 +        @Override
   1.130 +        public Scanner newScanner(char[] input, int inputLength) {
   1.131 +            return new MyScanner(this, input, inputLength);
   1.132 +        }
   1.133 +    }
   1.134 +    protected MyScanner(Factory fac, CharBuffer buffer) {
   1.135 +        super(fac, buffer);
   1.136 +    }
   1.137 +    protected MyScanner(Factory fac, char[] input, int inputLength) {
   1.138 +        super(fac, input, inputLength);
   1.139 +    }
   1.140 +
   1.141 +    public void nextToken() {
   1.142 +        super.nextToken();
   1.143 +        System.err.format("Saw token %s (%s)%n", token(), name());
   1.144 +    }
   1.145 +}

mercurial