モジュール 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
-
インタフェースのサマリー インタフェース 説明 ArrayAccessTree 配列アクセス式のツリー・ノードです。ArrayLiteralTree ECMAScript配列リテラル式を表します。AssignmentTree 代入式のツリー・ノードです。BinaryTree バイナリ式のツリー・ノードです。BlockTree 文ブロックのツリー・ノードです。BreakTree break文のツリー・ノードです。CaseTree switch文中のcaseのツリー・ノードです。CatchTree try文中のcatchブロックのツリー・ノードです。ClassDeclarationTree 「クラス宣言」を表すツリー・ノード。ClassExpressionTree 「クラス式」を表すツリー・ノード。CompilationUnitTree コンパイル単位(ソース・ファイル)の抽象構文ツリーを表します。CompoundAssignmentTree 複合代入演算子のツリー・ノードです。ConditionalExpressionTree 条件演算子?のツリー・ノードです。ConditionalLoopTree 条件付き"loop"文のミックス・イン。ContinueTree continue文のツリー・ノードです。DebuggerTree 'debugger'文のツリー・ノード。Diagnostic ツールからの診断情報を表示するインタフェースです。DiagnosticListener Nashornパーサーから診断情報を受け取るためのインタフェース。DoWhileLoopTree do文のツリー・ノードです。EmptyStatementTree 空の文(スキップ文)のツリー・ノードです。ErroneousTree 不正な式の代用となるツリー・ノードです。ExportEntryTree 「モジュール情報」内のエクスポート・エントリ用のツリー・ノード。ExpressionStatementTree expression文のツリー・ノードです。ExpressionTree 異なる型の式の基底クラスとして使用されるツリー・ノードです。ForInLoopTree for..in文のツリー・ノード例:ForLoopTree 基本のforループ文のツリー・ノードです。ForOfLoopTree 「for..of文」のツリー・ノード。FunctionCallTree 関数呼び出し式のツリー・ノード。FunctionDeclarationTree 「関数宣言」のツリー・ノード。FunctionExpressionTree GotoTree ターゲットにジャンプする文のツリー・ノード。IdentifierTree 識別子の式のツリー・ノードです。IfTree if文のツリー・ノードです。ImportEntryTree 「モジュール情報」のインポート・エントリ用のツリー・ノード。InstanceOfTree instanceof式のツリー・ノードです。LabeledStatementTree ラベル付き文のツリー・ノードです。LineMap コンパイル・ユニットの文字位置と行番号を変換するメソッドを提供します。LiteralTree リテラル式のツリー・ノードです。LoopTree "loop"文用のミックス・イン。MemberSelectTree メンバー・アクセス式のツリー・ノードです。ModuleTree 「モジュール情報」のツリー・ノード。NewTree クラスの新しいインスタンスを宣言するためのツリー・ノードです。ObjectLiteralTree ECMAScriptオブジェクト・リテラル式を表します。ParenthesizedTree 括弧で囲まれた式のツリー・ノードです。Parser nashorn ECMAScriptパーサー・インスタンスを表します。PropertyTree オブジェクト・リテラル・ツリーのプロパティ設定を表現します。RegExpLiteralTree ソース・コード内の正規表現リテラルを表します。ReturnTree return文のツリー・ノードです。SpreadTree 配列要素内のスプレッド演算子のツリー・ノード、関数呼び出しの引数。StatementTree 異なる種類の文の基底クラスとして使用されるツリー・ノードです。SwitchTree switch文のツリー・ノードです。TemplateLiteralTree 「テンプレート・リテラル文字列」のツリー・ノード。ThrowTree throw文のツリー・ノードです。ツリー 抽象構文ツリー内のすべてのノードの汎用インタフェースです。TreeVisitor<R,P> ビジター・デザイン・パターンのスタイルによるツリーのビジターです。TryTree try文のツリー・ノードです。UnaryTree 後置式および単項式のツリー・ノードです。VariableTree 「変数宣言文」のツリー・ノード。WhileLoopTree whileループ文のツリー・ノードです。WithTree 'with'文のツリー・ノード。YieldTree ジェネレータ関数で使用される「yield式」のツリー・ノード。 -
クラスのサマリー クラス 説明 SimpleTreeVisitorES5_1<R,P> ECMAScript Edition 5.1用のTreeVisitorの簡単な実装です。SimpleTreeVisitorES6<R,P> ECMAScript版6用のTreeVisitorの簡単な実装です。 -
列挙型のサマリー 列挙型 説明 Diagnostic.Kind 診断情報の種類(エラー、警告など)です。Tree.Kind すべての種類のツリーを列挙します。 -
例外のサマリー Exception 説明 UnknownTreeException 未知の種類のツリーが見つかったことを示します。