简体   繁体   English

如何在Android上通过反射实例化成员类

[英]How can I instantiate a member class through reflection on Android

I have a saving/loading framework that is supposed to save arbitrary object graphs. 我有一个保存/加载框架,该框架应该保存任意对象图。 This includes instances of non-static nested classes. 这包括非静态嵌套类的实例。

Nested classes require are owned by the instance of their parent class that created them, and the nested class knows what instance it belongs to using a synthetic field. 嵌套类require为其创建它们的父类的实例所拥有,并且嵌套类使用合成字段知道它属于哪个实例。

As a simple example, I present this class: 作为一个简单的例子,我介绍这个类:

public class Foo implements Savable {
  private class Bar implements Savable {
    public void saveState(Saver saver) {
      saver.putInt(3);
    }
  }
  private Bar myBar = new Bar();
  public void saveState(Saver saver) {
    saver.putSavable(myBar);
  }
}

On the "standard" compiler, the following code works just fine to take in an object ( myBar ), find its parent (a particular instance of Foo ), and keep a reference of it with the child: 在“标准”编译器上,以下代码可以很好地接收一个对象( myBar ),找到其父对象( Foo的特定实例)并保留其对子对象的引用:

if (objectClass.isMemberClass()) {
  //We are a member class, which means we are a non-static inner class, and therefore must save our parent.
  Field[] fields = objectClass.getDeclaredFields();
  //We loop through each of our fields to find the synthetic field created by the compiler that points to our parent.
  for (Field f : fields) {
    String name = f.getName();
    //The synthetic field pointing to the parent is named something like "this$0".  At least, with the "standard" compiler it is.
    if (name.startsWith("this$")) {
      f.setAccessible(true);
      Savable parent = (Savable)f.get(objectClass);
      saver.putSavable("_parent", parent);
      break;
    }
  }
  if (!saver.containsKey("_parent")) {
    throw new RuntimeException("Could not find the owner of inner class: " + objectClass);
  }
}

So, like I said, this works just fine in on the "standard" compiler. 因此,就像我说的那样,这在“标准”编译器上可以正常工作。 Then at load time, something similar happens, except I look for a constructor that takes in an instance of the parent class and instantiate it, passing in the parent. 然后在加载时,会发生类似的事情,除了我寻找一个构造函数,该构造函数接受父类的实例并实例化它,并传入父类。

But! 但!

It doesn't work on the Android VM. 它不适用于Android VM。 There are no synthetic fields, and the constructors all look normal, like they don't take in a parent instance. 没有合成字段,并且构造函数都看起来很正常,就像它们没有采用父实例一样。

Am I SOL here? 我在这里吗? This VM obviously does not like my approach. 该虚拟机显然不喜欢我的方法。 Is there anything I can do here without requiring inner classes to know that they need to save a reference to their parent class instance? 在不要求内部类知道它们需要保存对其父类实例的引用的情况下,我在这里可以做些什么吗?

Okay, turns out I was barking up the wrong tree. 好吧,原来我在吠错树。 The method I've outlined works perfectly well on Android just as well as anywhere else. 我概述的方法在Android以及其他任何地方都可以很好地工作。 The problem was that the class I was trying to instantiate in this case was a static internal class. 问题是在这种情况下我要实例化的类是静态内部类。 So, while it was a member class, it didn't have a reference to its outer class. 因此,虽然它是成员类,但没有对其外部类的引用。 I just have to check if (!Modifier.isStatic(objectClass.getModifiers())) before looking for a synthetic constructor/field. 我只需要在查找综合构造函数/字段之前检查if (!Modifier.isStatic(objectClass.getModifiers()))

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

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