- JDK 23対応GraalVM (最新)
- JDK 24対応GraalVM (早期アクセス)
- JDK 21対応GraalVM
- JDK 17対応GraalVM
- アーカイブ
- 開発ビルド
アプリケーションへのInsightの埋め込み
JavaへのInsightの埋め込み #
Graal言語(Truffleフレームワークで実装された言語、たとえばJavaScript、Python、Ruby、Rなど)は、Polyglot Context APIを介してカスタムJavaアプリケーションに埋め込むことができます。 GraalVM Insightも同じAPIで制御できます。次のようにします。
final Engine engine = context.getEngine();
Instrument instrument = engine.getInstruments().get("insight");
Function<Source, AutoCloseable> access = instrument.lookup(Function.class);
AutoCloseable handle = access.apply(agentSrc);
Context
のEngine
を取得し、insight
インストゥルメントを要求します。
次に、GraalVM Insightスクリプトを使用して`Source`を作成し、インストゥルメンテーションハンドルを取得しながら適用します。 不要になったら、`handle.close()`を使用してスクリプトのすべてのインストゥルメンテーションを無効にします。 例: ```java Source instrument = Source.create("js", """ insight.on('return', function(ctx, frame) { console.log(`Instrumented where = ${frame.where}`); }, { roots: true, rootNameFilter: 'end', }); """); Source script = Source.create("js", """ function end() { var where = 'end'; console.log(where + ' invoked') } end(); """); try (Context context = Context.newBuilder().build()) { @SuppressWarnings("unchecked") Function<Source, AutoCloseable> insight = context.getEngine().getInstruments().get("insight").lookup(Function.class); // インストゥルメンテーションなしで実行 context.eval(script); // インストゥルメンテーションを使用して実行 try (AutoCloseable handle = insight.apply(instrument)) { context.eval(script); } // インストゥルメンテーションなしで実行 context.eval(script); } ``` [埋め込み依存関係の設定](/latest/reference-manual/embed-languages/#dependency-setup)を参照してください。 `insight`への依存関係を追加します: ```