简体   繁体   English

如何反映私有静态嵌套子类?

[英]How do I reflect a private static nested subclass?

First, I'm looking for an answer in Kotlin, but I'm interacting with a Java library.首先,我正在 Kotlin 中寻找答案,但我正在与 Java 库进行交互。

I need to get an instance from a private static nested class, derived from an instance of the surrounding superclass.我需要从一个私有静态嵌套类中获取一个实例,该类派生自周围超类的一个实例。

Given you have these (simplified) nested Java classes鉴于您有这些(简化的)嵌套 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;
    }
  }
}

I get back a Container instance as a GLFWKeyCallback, by way of another external method.我通过另一种外部方法将 Container 实例作为 GLFWKeyCallback 取回。 You can think of this method as:您可以将此方法视为:

public static GLFWKeyCallback getCallback() {
  return GLFWKeyCallback.create(anInternalInstance)
}

in 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'

Why you don't want to use Java reflection?为什么你不想使用 Java 反射? You can use it also from Kotlin:您也可以在 Kotlin 中使用它:

val callback = getCallback()
val field = callback::class.java.getDeclaredField("delegate")
field.isAccessible = true
val delegate = field.get(callback) as GLFWKeyCallbackI

You can still get the class via .getClass() .您仍然可以通过.getClass()获取课程。 This example prints '5':此示例打印“5”:

public class Example {
    public static void main(String[] args) throws Exception {
        Object o = Target.get();
        Field f = o.getClass().getDeclaredField("field");
        f.setAccessible(true);
        Integer i = (Integer) f.get(o);
        System.out.println(i);
    }
}

public class Target {
    public static Object get() { return new Inner(); }
    private static class Inner {
        private int field = 5;
    }
}

If you know precise names:如果您知道确切的名称:

Class<?> c = Class.forName("com.foo.pkgof.Target$Inner");
c.getDeclaredField("field");

works.作品。 Note the dollar.注意美元。 That's the separator to use between 'outer' and 'inner'.这是在“外部”和“内部”之间使用的分隔符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM