2015/03/29 このエントリーをはてなブックマークに追加 はてなブックマーク - 【Kotlin】M11(マイルストーン11)リフレクションも便利になったよ

【Kotlin】M11(マイルストーン11)リフレクションも便利になったよ

カテゴリ:

JetBrains社サイトより引用




前回はKotlinのM11で変わったポイントでAndroid向けのものと、
Companion Object、lambda、関数オブジェクトなどについて紹介しました。



【Kotlin】M11(マイルストーン11)はAndroid開発者のためにある

【Kotlin】M11(マイルストーン11)の目玉、Companion Objectとlamdaと関数オブジェクト




なぜか連載ぽくなっていますが(笑)、今回はリフレクションについてです。
リフレクションの変更も結構Kotlinに影響与えるものだと思いますね。
Kotlinのコンパイルやリフレクションを使ったシンタックスなど色々と波及するところだと思うので、
注目していて損はない!!!


ということで、今回はリフレクションの変更について翻訳します。




注:翻訳について




===================== 以下、Kotlinブログの翻訳 =============================









Kotlin固有のリフレクションを実装することは
(むしろKotlinクラスをJavaリフレクションで使用するより)、
コンパイラに多くの作業を要求してプロジェクト実行がめちゃ遅かった。
だから必要に迫れられて、我々はコンパイラの大部分を考慮して、
ランタイムの一部としてリフレクション機能をリリースする必要があると考えた。
Implementing Kotlin-specific reflection (rather than making you use Java reflection on Kotlin classes) is a long-running project that has required a lot of work in the compiler. Essentially, we have to factor out a large portion of the compiler and ship it as part of the runtime.



Kotlinのリフレクションランタイムjarは
Javaの宣言をKotlinのものとしてロードし、Kotlinのシンボルをオブジェクトとして表す。
(今まで通り、私たちは記述子を呼び出す)
そして、バイナリからKotlin固有のメタデータをロードする。
( Kotlinのリフレクションは、Javaのオブジェクトとしても動作すべきなので)
This includes: loading Kotlin-specific metadata from the binaries, representing Kotlin symbols as objects (historically, we call them descriptors), loading Java declarations as Kotlin ones (because Kotlin reflection should work on Java objects too) and so on.


ついに、我々はこの作品(リフレクション機能)の最初の一歩をリリースする。
新しいkotlin - reflect.jarだ。 プロパティをイントロスペクトする能力を、コンパイラに付属している。(より多くの機能がすぐに追加される)
At last, we present the first results of this work: the ability to introspect properties, provided through a new kotlin-reflect.jar that ships with the compiler (a lot more functionality will be added soon).




jarは現状ではかなり大きいので、我々はリフレクションのjarを
kotlin - runtime.jarの一部としなかった。
別途kotlin - reflect.jarをリリースしたが、これは 1.8MB程度のサイズだ。
We ship kotlin-reflect.jar separately (not as part of kotlin-runtime.jar), because it is rather big at the moment: about 1.8MB.


jarのサイズを小さくしようとはしてるけど、
サイズのデカさを気にされる可能性が高いと思ってる、特にAndroidの開発者のとかね。
だからこのjarは必須のオプションにはしていない
We will look into reducing its size, but it is likely to always be rather substantial, so making everyone always ship it with their applications is not an option (especially for Android
developers).



ただ、プロパティのリテラル( :: propertyNameの)を使用したい場合、
クラスパスにこのjarファイルを追加する必要があるよ。
M11コンパイラは、jarがない場合にエラーを吐くが、すぐに緩和されるだろう。

IDEがプロジェクトに自動的にjarファイルを追加するクイックフィックスのアクションするよ。
As a consequence, you may need to add this jar to your classpath, if you use property literals (::propertyName). The M11 compiler will yield an error if you
don’t, but later this requirement will be relaxed. The IDE will offer you a quick-fix action that adds the jar automatically to your project.





Kotlinにクラスのためのリフレクションオブジェクトを取得するには、次の構文を使用する。

val c = MyClass::class
To obtain a reflection object for a class in Kotlin, use the following syntax:



このようにKClass < MyClass>のインスタンスを生成出来る。また、そこから例えばプロパティを取得したりも出来るね。
You get an instance of KClass, which you can introspect, e.g. get its properties.

詳しくはドキュメントを見てね。
See more in the user docs.






KotlinリフレクションAPIはKotlinとJavaクラスの両方に対して機能し、
JavaリフレクションオブジェクトにKotlinから「変換」することができます。その逆もできる。
たとえば、 kClass.javaからjava.lang.Classのインスタンスを取得出来ると言えるし、
逆に、のjlClass.kotlinからKClassのインスタンスが生成されるとも言える。


Kotlin reflection API works both for Kotlin and Java classes, and you can “convert” from Kotlin to Java reflection objects and back. For example, you can

say kClass.java and get a java.lang.Class instance, and vice versa: jlClass.kotlin gives you a KClass instance.











ということで、今回はKotlinのM11で変更されたリフレクションについての翻訳を行いました。

ポイントとしては


・コンパイル処理速度を考慮してリフレクションが改善された
・リフレクションはランタイムjarとは別にjarとして外部化された
・リフレクションjarはまだ容量がデカイが、今後改善する予定
・リフレクションjarはオプションとしている。
(Androidアプリケーションなどはapkの容量が重くなるのなどを懸念するだろうという配慮)
・クラス用のリフレクションなどを使う場合はリフレクションのjarが必要




ざっとこんな感じです。




今までちょっと不便だった(というかちょっと足りてない気がしてた)リフレクションの機能の強化です!!

多分、リフレクション使ったコードとか書かないと嬉しさがわかりづらいですね・・・(^^;

それはまた別エントリということで。。。




Don't by shy, write Kotlin!!!




0 件のコメント:

コメントを投稿

GA