LoginSignup
1
1

More than 3 years have passed since last update.

【Java】JavaでMethod/ConstructorからKFunctionを取得する【Kotlin】

Posted at

やること

JavaMethod/ConstructorからKFunctionを取り出します。

やり方

下記のMethod.kotlinFunction/Constructor<T>.kotlinFunctionJavaから呼び出せばできます。

具体的には以下のようになります。

KFunction<?> function = ReflectJvmMapping.getKotlinFunction(Foo.class.getConstructors()[0]);

注意点

ClassからのMethod/Constructor取得にはジェネリクスの型パラメータが設定されていない場合が多いため、型パラメータを入れたい場合はJava内でキャストする必要があります。

KFunction<Foo> function = ReflectJvmMapping.getKotlinFunction((Constructor<Foo>) Foo.class.getConstructors()[0]);

参考にさせて頂いた記事

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1