langtools/test/tools/javac/parser/extend/JavacExtensionTest.java
changeset 28587 ce5606145ea3
child 30730 d3ce7619db2c
equal deleted inserted replaced
28462:8327024a9955 28587:ce5606145ea3
       
     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 
       
    24 import com.sun.source.tree.CompilationUnitTree;
       
    25 import com.sun.source.tree.Tree;
       
    26 import com.sun.source.util.SourcePositions;
       
    27 import com.sun.source.util.TreePath;
       
    28 import com.sun.source.util.Trees;
       
    29 import com.sun.tools.javac.api.JavacTaskImpl;
       
    30 import com.sun.tools.javac.api.JavacTool;
       
    31 import com.sun.tools.javac.util.Context;
       
    32 import java.util.ArrayList;
       
    33 import java.util.Arrays;
       
    34 import java.util.Iterator;
       
    35 import java.util.List;
       
    36 import javax.lang.model.element.Element;
       
    37 import javax.tools.Diagnostic;
       
    38 import javax.tools.DiagnosticCollector;
       
    39 import javax.tools.JavaCompiler;
       
    40 import javax.tools.JavaFileManager;
       
    41 import javax.tools.JavaFileObject;
       
    42 import javax.tools.ToolProvider;
       
    43 import javax.tools.StandardJavaFileManager;
       
    44 import com.sun.source.tree.ImportTree;
       
    45 import java.util.Collections;
       
    46 import java.io.PrintWriter;
       
    47 import com.sun.source.tree.VariableTree;
       
    48 import static javax.tools.StandardLocation.CLASS_OUTPUT;
       
    49 
       
    50 /*
       
    51  * @test
       
    52  * @bug 8067384 8068488
       
    53  * @summary Verify that JavacParser can be extended
       
    54  */
       
    55 public 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 }