【发布时间】:2020-02-10 04:33:47
【问题描述】:
首先,我在 Kotlin 中寻找答案,但我正在与 Java 库进行交互。
我需要从一个私有静态嵌套类中获取一个实例,该实例派生自周围超类的一个实例。
假设您有这些(简化的)嵌套 Java 类
public abstract class GLFWKeyCallback extends Callback implements GLFWKeyCallbackI {
public static GLFWKeyCallback create(GLFWKeyCallbackI instance) {
new Container(instance.address(), instance);
}
private static final class Container extends GLFWKeyCallback {
private final GLFWKeyCallbackI delegate;
Container(long functionPointer, GLFWKeyCallbackI delegate) {
super(functionPointer);
this.delegate = delegate;
}
}
}
我通过另一种外部方法以 GLFWKeyCallback 的形式返回一个 Container 实例。你可以把这个方法想成:
public static GLFWKeyCallback getCallback() {
return GLFWKeyCallback.create(anInternalInstance)
}
在 Kotlin 中:
val callback:GLFWKeyCallback = getCallback()
// I would now want to cast,
// or in other ways use callback
// as the GLFWKeyCallback.Container class it actually is.
val callbackAsContainer = callback as GLFWKeyCallback.Container // Error: Container is private
val ContainerClass = GLFWKeyCallback::class.nestedClasses.find { it.simpleName?.contains("Container") ?: false }!!
// Gives me a KClass<*> that I don't know how to use, can't find documentation for this kind of circumstance
// If using the class instance itself is not possible I would at least want to get the
// Container.delegate of GLFWKeyCallbackI
val delegateField = ContainerClass.memberProperties.findLast { it.name == "delegate" }!!
val fieldValue = field.get(callback)
// Error: Out-projected type 'KProperty1<out Any, Any?>' prohibits the use of 'public abstract fun get(receiver: T): R defined in kotlin.reflect.KProperty1'
【问题讨论】:
-
首先,我想确定您确实需要引用私有静态嵌套类,因为这很难闻。 (一方面,它不是第三方库的公共接口的一部分,因此在未来的版本中可能会被更改/重命名/删除。另一方面,对库的内部进行处理可能会导致它意外中断方式。)当然,有时确实有必要——但你需要确定没有其他选择。
-
@gidds 你绝对是对的,但这次我需要。
标签: java kotlin kotlin-reflect