我想在Groovy中获取此接口的实例 KotlinDecompilerService https://github.com/JetBrains/kotlin/blob/master/idea/src/org/jetbrains/kotlin/idea/internal/KotlinDecompilerService.kt.
我尝试:
final KotlinDecompilerService decompilerService =
KotlinDecompilerService.Companion.getInstance()
Run Code Online (Sandbox Code Playgroud)
但是Groovy KotlinDecompilerService.Companion返回一个java.lang.Class实例.(静态字段和类具有相同的名称Companion)
2017-06-24 23:31:59,066 [9849885] ERROR - llij.ide.plugins.PluginManager - Cannot cast object 'class org.jetbrains.kotlin.idea.internal.KotlinDecompilerService$Companion' with class 'java.lang.Class' to class 'org.jetbrains.kotlin.idea.internal.KotlinDecompilerService$Companion'
org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'class org.jetbrains.kotlin.idea.internal.KotlinDecompilerService$Companion' with class 'java.lang.Class' to class 'org.jetbrains.kotlin.idea.internal.KotlinDecompilerService$Companion'
at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.continueCastOnSAM(DefaultTypeTransformation.java:405)
at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.continueCastOnNumber(DefaultTypeTransformation.java:319)
at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.castToType(DefaultTypeTransformation.java:232)
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.castToType(ScriptBytecodeAdapter.java:603)
Run Code Online (Sandbox Code Playgroud)
我必须使用反射来获取实例.
final KotlinDecompilerService decompilerService =
KotlinDecompilerService.Companion.newInstance().getInstance()
Run Code Online (Sandbox Code Playgroud)
它有效,但我只是想知道是否有更好的方法.
hol*_*ava 13
你可以直接访问成员字段,@因为它总是引用类而不是它在groovy中的实例.例如:
def decompilerService = KotlinDecompilerService.@Companion.instance
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
821 次 |
| 最近记录: |