TestJavacTaskScanner.java revision 1732:e39669aea0bd
1/*
2 * Copyright (c) 2005, 2013, 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 */
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 * @build ToolTester
31 * @run main TestJavacTaskScanner TestJavacTaskScanner.java
32 */
33
34import com.sun.tools.javac.api.JavacTaskImpl;
35import com.sun.tools.javac.parser.*;
36import com.sun.tools.javac.parser.Tokens.Token;
37import com.sun.tools.javac.util.*;
38import java.io.*;
39import java.net.*;
40import java.nio.*;
41import java.nio.charset.Charset;
42import java.util.Arrays;
43import javax.lang.model.element.Element;
44import javax.lang.model.element.TypeElement;
45import javax.lang.model.type.DeclaredType;
46import javax.lang.model.type.TypeMirror;
47import javax.lang.model.util.Elements;
48import javax.lang.model.util.Types;
49import javax.tools.*;
50
51import static javax.tools.StandardLocation.CLASS_PATH;
52import static javax.tools.StandardLocation.SOURCE_PATH;
53import static javax.tools.StandardLocation.CLASS_OUTPUT;
54
55public class TestJavacTaskScanner extends ToolTester {
56
57    final JavacTaskImpl task;
58    final Elements elements;
59    final Types types;
60
61    int numTokens;
62    int numParseTypeElements;
63    int numAllMembers;
64
65    TestJavacTaskScanner(File file) {
66        final Iterable<? extends JavaFileObject> compilationUnits =
67            fm.getJavaFileObjects(new File[] {file});
68        StandardJavaFileManager fm = getLocalFileManager(tool, null, null);
69        task = (JavacTaskImpl)tool.getTask(null, fm, null, null, null, compilationUnits);
70        task.getContext().put(ScannerFactory.scannerFactoryKey,
71                new MyScanner.Factory(task.getContext(), this));
72        elements = task.getElements();
73        types = task.getTypes();
74    }
75
76    public void run() {
77        Iterable<? extends TypeElement> toplevels;
78        try {
79            toplevels = task.enter(task.parse());
80        } catch (IOException ex) {
81            throw new AssertionError(ex);
82        }
83        for (TypeElement clazz : toplevels) {
84            System.out.format("Testing %s:%n%n", clazz.getSimpleName());
85            testParseType(clazz);
86            testGetAllMembers(clazz);
87            System.out.println();
88            System.out.println();
89            System.out.println();
90        }
91
92        System.out.println("#tokens: " + numTokens);
93        System.out.println("#parseTypeElements: " + numParseTypeElements);
94        System.out.println("#allMembers: " + numAllMembers);
95
96        check(numTokens, "#Tokens", 1222);
97        check(numParseTypeElements, "#parseTypeElements", 158);
98        check(numAllMembers, "#allMembers", 52);
99    }
100
101    void check(int value, String name, int expected) {
102        // allow some slop in the comparison to allow for minor edits in the
103        // test and in the platform
104        if (value < expected * 9 / 10)
105            throw new Error(name + " lower than expected; expected " + expected + "; found: " + value);
106        if (value > expected * 11 / 10)
107            throw new Error(name + " higher than expected; expected " + expected + "; found: " + value);
108    }
109
110    void testParseType(TypeElement clazz) {
111        DeclaredType type = (DeclaredType)task.parseType("List<String>", clazz);
112        for (Element member : elements.getAllMembers((TypeElement)type.asElement())) {
113            TypeMirror mt = types.asMemberOf(type, member);
114            System.out.format("%s : %s -> %s%n", member.getSimpleName(), member.asType(), mt);
115            numParseTypeElements++;
116        }
117    }
118
119    public static void main(String... args) throws IOException {
120        String srcdir = System.getProperty("test.src");
121        new TestJavacTaskScanner(new File(srcdir, args[0])).run();
122    }
123
124    private void testGetAllMembers(TypeElement clazz) {
125        for (Element member : elements.getAllMembers(clazz)) {
126            System.out.format("%s : %s%n", member.getSimpleName(), member.asType());
127            numAllMembers++;
128        }
129    }
130
131    /* Similar to ToolTester.getFileManager, except that this version also ensures
132     * javac classes will be available on the classpath.  The javac classes are assumed
133     * to be on the classpath used to run this test (this is true when using jtreg).
134     * The classes are found by obtaining the URL for a sample javac class, using
135     * getClassLoader().getResource(), and then deconstructing the URL to find the
136     * underlying directory or jar file to place on the classpath.
137     */
138    public StandardJavaFileManager getLocalFileManager(JavaCompiler tool,
139                                                        DiagnosticListener<JavaFileObject> dl,
140                                                        Charset encoding) {
141        File javac_classes;
142        try {
143            final String javacMainClass = "com/sun/tools/javac/Main.class";
144            URL url = getClass().getClassLoader().getResource(javacMainClass);
145            if (url == null)
146                throw new Error("can't locate javac classes");
147            URI uri = url.toURI();
148            String scheme = uri.getScheme();
149            String ssp = uri.getSchemeSpecificPart();
150            if (scheme.equals("jar")) {
151                javac_classes = new File(new URI(ssp.substring(0, ssp.indexOf("!/"))));
152            } else if (scheme.equals("file")) {
153                javac_classes = new File(ssp.substring(0, ssp.indexOf(javacMainClass)));
154            } else
155                throw new Error("unknown URL: " + url);
156        } catch (URISyntaxException e) {
157            throw new Error(e);
158        }
159        System.err.println("javac_classes: " + javac_classes);
160
161        StandardJavaFileManager fm = tool.getStandardFileManager(dl, null, encoding);
162        try {
163            fm.setLocation(SOURCE_PATH,  Arrays.asList(test_src));
164            fm.setLocation(CLASS_PATH,   join(test_class_path, Arrays.asList(javac_classes)));
165            fm.setLocation(CLASS_OUTPUT, Arrays.asList(test_classes));
166        } catch (IOException e) {
167            throw new AssertionError(e);
168        }
169        return fm;
170    }
171}
172
173class MyScanner extends Scanner {
174
175    public static class Factory extends ScannerFactory {
176        public Factory(Context context, TestJavacTaskScanner test) {
177            super(context);
178            this.test = test;
179        }
180
181        @Override
182        public Scanner newScanner(CharSequence input, boolean keepDocComments) {
183            if (input instanceof CharBuffer) {
184                return new MyScanner(this, (CharBuffer)input, test);
185            } else {
186                char[] array = input.toString().toCharArray();
187                return newScanner(array, array.length, keepDocComments);
188            }
189        }
190
191        @Override
192        public Scanner newScanner(char[] input, int inputLength, boolean keepDocComments) {
193            return new MyScanner(this, input, inputLength, test);
194        }
195
196        private TestJavacTaskScanner test;
197    }
198    protected MyScanner(ScannerFactory fac, CharBuffer buffer, TestJavacTaskScanner test) {
199        super(fac, buffer);
200        this.test = test;
201    }
202    protected MyScanner(ScannerFactory fac, char[] input, int inputLength, TestJavacTaskScanner test) {
203        super(fac, input, inputLength);
204        this.test = test;
205    }
206
207    public void nextToken() {
208        super.nextToken();
209        Token tk = token();
210        System.err.format("Saw token %s %n", tk.kind);
211        test.numTokens++;
212    }
213
214    private TestJavacTaskScanner test;
215
216}
217