モジュール jdk.scripting.nashorn

パッケージ jdk.nashorn.api.tree

NashornパーサーAPIは、ECMAScriptソース・コードを抽象構文ツリー(AST)およびParserとして表現するためのインタフェースを提供し、ECMAScriptソース・スクリプトを解析します。

パーサーAPIを使用すると、ECMAScriptソースの構文解析ツリーにアクセスするためのJavaコードを記述できます。 スクリプト・ソースは、ファイル、URL、または文字列です。 特に指定のない限り、このパッケージのメソッドのnull引数はNullPointerExceptionをスローします。

 
 import jdk.nashorn.api.tree.*;
 import java.io.File;

 // Simple example that prints warning on 'with' statements
 public class Main {
     public static void main(String[] args) throws Exception {
         // Create a new parser instance
         Parser parser = Parser.create();
         File sourceFile = new File(args[0]);

         // Parse given source File using parse method.
         // Pass a diagnostic listener to print error messages.
         CompilationUnitTree cut = parser.parse(sourceFile,
             (d) -> { System.out.println(d); });

         if (cut != null) {
             // call Tree.accept method passing a SimpleTreeVisitor
             cut.accept(new SimpleTreeVisitor<Void, Void>() {
                 // visit method for 'with' statement
                 public Void visitWith(WithTree wt, Void v) {
                     // print warning on 'with' statement
                     System.out.println("Warning: using 'with' statement!");
                     return null;
                 }
             }, null);
         }
     }
 }
 
 
導入されたバージョン:
9