TestJavacTaskScanner.java revision 289:d4828eba4939
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 */
23
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 */
32
33import com.sun.tools.javac.api.JavacTaskImpl;
34import com.sun.tools.javac.parser.*; // XXX
35import com.sun.tools.javac.util.*; // XXX
36import java.io.*;
37import java.nio.*;
38import javax.lang.model.element.Element;
39import javax.lang.model.element.TypeElement;
40import javax.lang.model.type.DeclaredType;
41import javax.lang.model.type.TypeMirror;
42import javax.lang.model.util.Elements;
43import javax.lang.model.util.Types;
44import javax.tools.*;
45
46public class TestJavacTaskScanner extends ToolTester {
47
48    final JavacTaskImpl task;
49    final Elements elements;
50    final Types types;
51
52    int numTokens;
53    int numParseTypeElements;
54    int numAllMembers;
55
56    TestJavacTaskScanner(File file) {
57        final Iterable<? extends JavaFileObject> compilationUnits =
58            fm.getJavaFileObjects(new File[] {file});
59        task = (JavacTaskImpl)tool.getTask(null, fm, null, null, null, compilationUnits);
60        task.getContext().put(Scanner.Factory.scannerFactoryKey,
61                new MyScanner.Factory(task.getContext(), this));
62        elements = task.getElements();
63        types = task.getTypes();
64    }
65
66    public void run() {
67        Iterable<? extends TypeElement> toplevels;
68        try {
69            toplevels = task.enter(task.parse());
70        } catch (IOException ex) {
71            throw new AssertionError(ex);
72        }
73        for (TypeElement clazz : toplevels) {
74            System.out.format("Testing %s:%n%n", clazz.getSimpleName());
75            testParseType(clazz);
76            testGetAllMembers(clazz);
77            System.out.println();
78            System.out.println();
79            System.out.println();
80        }
81
82        System.out.println("#tokens: " + numTokens);
83        System.out.println("#parseTypeElements: " + numParseTypeElements);
84        System.out.println("#allMembers: " + numAllMembers);
85
86        check(numTokens, "#Tokens", 891);
87        check(numParseTypeElements, "#parseTypeElements", 136);
88        check(numAllMembers, "#allMembers", 67);
89    }
90
91    void check(int value, String name, int expected) {
92        // allow some slop in the comparison to allow for minor edits in the
93        // test and in the platform
94        if (value < expected * 9 / 10)
95            throw new Error(name + " lower than expected; expected " + expected + "; found: " + value);
96        if (value > expected * 11 / 10)
97            throw new Error(name + " higher than expected; expected " + expected + "; found: " + value);
98    }
99
100    void testParseType(TypeElement clazz) {
101        DeclaredType type = (DeclaredType)task.parseType("List<String>", clazz);
102        for (Element member : elements.getAllMembers((TypeElement)type.asElement())) {
103            TypeMirror mt = types.asMemberOf(type, member);
104            System.out.format("%s : %s -> %s%n", member.getSimpleName(), member.asType(), mt);
105            numParseTypeElements++;
106        }
107    }
108
109    public static void main(String... args) throws IOException {
110        String srcdir = System.getProperty("test.src");
111        new TestJavacTaskScanner(new File(srcdir, args[0])).run();
112    }
113
114    private void testGetAllMembers(TypeElement clazz) {
115        for (Element member : elements.getAllMembers(clazz)) {
116            System.out.format("%s : %s%n", member.getSimpleName(), member.asType());
117            numAllMembers++;
118        }
119    }
120}
121
122class MyScanner extends Scanner {
123
124    public static class Factory extends Scanner.Factory {
125        public Factory(Context context, TestJavacTaskScanner test) {
126            super(context);
127            this.test = test;
128        }
129
130        @Override
131        public Scanner newScanner(CharSequence input) {
132            if (input instanceof CharBuffer) {
133                return new MyScanner(this, (CharBuffer)input, test);
134            } else {
135                char[] array = input.toString().toCharArray();
136                return newScanner(array, array.length);
137            }
138        }
139
140        @Override
141        public Scanner newScanner(char[] input, int inputLength) {
142            return new MyScanner(this, input, inputLength, test);
143        }
144
145        private TestJavacTaskScanner test;
146    }
147    protected MyScanner(Factory fac, CharBuffer buffer, TestJavacTaskScanner test) {
148        super(fac, buffer);
149        this.test = test;
150    }
151    protected MyScanner(Factory fac, char[] input, int inputLength, TestJavacTaskScanner test) {
152        super(fac, input, inputLength);
153        this.test = test;
154    }
155
156    public void nextToken() {
157        super.nextToken();
158        System.err.format("Saw token %s (%s)%n", token(), name());
159        test.numTokens++;
160    }
161
162    private TestJavacTaskScanner test;
163
164}
165