JavacExtensionTest.java revision 2797:52227644abfa
1/*
2 * Copyright (c) 2015, 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
24import com.sun.source.tree.CompilationUnitTree;
25import com.sun.source.tree.Tree;
26import com.sun.source.util.SourcePositions;
27import com.sun.source.util.TreePath;
28import com.sun.source.util.Trees;
29import com.sun.tools.javac.api.JavacTaskImpl;
30import com.sun.tools.javac.api.JavacTool;
31import com.sun.tools.javac.util.Context;
32import java.util.ArrayList;
33import java.util.Arrays;
34import java.util.Iterator;
35import java.util.List;
36import javax.lang.model.element.Element;
37import javax.tools.Diagnostic;
38import javax.tools.DiagnosticCollector;
39import javax.tools.JavaCompiler;
40import javax.tools.JavaFileManager;
41import javax.tools.JavaFileObject;
42import javax.tools.ToolProvider;
43import javax.tools.StandardJavaFileManager;
44import com.sun.source.tree.ImportTree;
45import java.util.Collections;
46import java.io.PrintWriter;
47import com.sun.source.tree.VariableTree;
48import static javax.tools.StandardLocation.CLASS_OUTPUT;
49
50/*
51 * @test
52 * @bug 8067384 8068488
53 * @summary Verify that JavacParser can be extended
54 */
55public class JavacExtensionTest {
56
57    public static void main(String[] args) throws Exception {
58        PrintWriter pw = new PrintWriter("trialSource.java", "UTF-8");
59        pw.println("int x = 9;");
60        pw.close();
61        List<? extends Tree> defs = parse("trialSource.java");
62        if (defs.size() != 1) {
63            throw new AssertionError("Expected only one def, got: " + defs.size());
64        }
65        Tree tree = defs.get(0);
66        if (tree instanceof VariableTree) {
67            System.out.println("Passes!  --- " + tree);
68        } else {
69            throw new AssertionError("Expected VariableTree, got: " + tree);
70        }
71    }
72
73    static List<? extends Tree> parse(String srcfile) throws Exception {
74        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
75        StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
76        Iterable<? extends JavaFileObject> fileObjects = fileManager.getJavaFileObjects(srcfile);
77        String classPath = System.getProperty("java.class.path");
78        List<String> options = Arrays.asList("-classpath", classPath);
79        DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<>();
80        Context context = new Context();
81        JavacTaskImpl task = (JavacTaskImpl) ((JavacTool) compiler).getTask(null, null,
82                diagnostics, options, null, fileObjects, context);
83        TrialParserFactory.instance(context);
84        Iterable<? extends CompilationUnitTree> asts = task.parse();
85        Iterator<? extends CompilationUnitTree> it = asts.iterator();
86        if (it.hasNext()) {
87            CompilationUnitTree cut = it.next();
88            return cut.getTypeDecls();
89        } else {
90            throw new AssertionError("Expected compilation unit");
91        }
92    }
93}
94