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

时间:2019-02-02 10:41:41

标签: java kotlin kotlin-reflect

首先,我正在寻找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;
    }

  }

}

我通过另一个外部方法将Container实例作为GLFWKeyCallback取回。您可以将这种方法视为:

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

在科特林:


  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'


任何帮助都会很好,解决方案不必看起来很漂亮。它用于测试用例,而不是生产代码。

谢谢!

2 个答案:

答案 0 :(得分:2)

为什么不想使用Java反射?您也可以从Kotlin使用它:)对我来说,这很好用

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

答案 1 :(得分:1)

您仍然可以通过.getClass()来上课。此示例显示“ 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;
    }
}

如果您知道确切的名字:

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

有效。注意美元。那是在“外部”和“内部”之间使用的分隔符。