[Java. JavaScript] Nashornの機能強化に伴うドキュメントへの追加・修正

JDK 8u60がリリースされましたが、そのリリースノートの中で、Nashornの機能強化に伴うドキュメントの記載内容の変更が取り上げられています。
8u60 Update Release Notes
http://www.oracle.com/technetwork/java/javase/8u60-relnotes-2620227.html
データ型のマッピングに対する機能強化で、ドキュメントへの追加が1件、修正が2件です。いずれも以下の箇所に関係します。
Java Platform, Standard Edition Java Scripting Programmer's Guide
3.10 Mapping Data Types
http://docs.oracle.com/javase/8/docs/technotes/guides/scripting/prog_guide/javascript.html#A1147390

Oracle Java Platform, Standard Edition Javaスクリプト・プログラマーズ・ガイド
3.10 データ型のマッピング
http://docs.oracle.com/javase/jp/8/docs/technotes/guides/scripting/prog_guide/javascript.html#A1147390
それでは見ていきましょう。

  • Addition:
    In the previous section, we mentioned that every JavaScript object when exposed to Java APIs implements the java.util.Map interface. This is true even for JavaScript arrays. However, this behavior is often not desired or expected when the Java code expects JSON-parsed objects. Java libraries that manipulate JSON-parsed objects usually expect arrays to expose the java.util.List interface instead. If you need to expose your JavaScript objects so that arrays are exposed as lists and not maps, you can use the Java.asJSONCompatible(obj) function, where obj is the root of your JSON object tree.

  • 追加
    前章(3.10 データ型のマッピング)で、「APIでマップを直接受け取ることができるように、すべてのJavaScriptオブジェクトがjava.util.Mapインターフェースを実装している」、と述べました。これはJavaScript配列に対しては今なお変わりません。
    しかし、JavaコードがJSONパース済みのオブジェクトを期待している場合、これは予期せぬ、期待されない挙動になることがあります。JSONパース済みのオブジェクトを操作するJavaライブラリは通常java.util.Listインターフェースで受け取ることのできる配列を期待しています。配列をマップではなくリストとして受け取るよう、JavaScriptオブジェクトを受け取る必要がある場合、Java.asJSONCompatible(obj) 関数を利用することができます。ここでobjはJSONオブジェクトツリーのルートです。

  • Correction:
    The caution mentioned at the end of Mapping Data Types section, is no longer applicable.
    Nashorn ensures that internal JavaScript strings are converted to java.lang.String when exposed externally.

  • 修正
    「データ型のマッピング」の最後に記載した注意点は、JDK 8u60以後考慮する必要はありません。
    内部文字列を取り出す場合、Nashornは内部JavaScript文字列をjava.lang.Stringに変換します。

  • Correction:
    The statement in the section Mapping Data Types, that mentions "For example, arrays must be explicitly converted,........." is not correct.
    Arrays are automatically converted to Java array types, such as java.util.List, java.util.Collection, java.util.Queue and java.util.Deque and so on.

  • 修正
    「データ型のマッピング」で述べている、「たとえば、配列は明示的に変換する必要があり…」の記述は間違っています。
    配列は自動的にJavaの配列型に変換されます。(例:java.util.Listjava.util.Collectionjava.util.Queuejava.util.Dequeなど)

0 件のコメント:

コメントを投稿