4

I want to know if there is a way to recover a reference "manually" of the this$0, in other words, to the outer class in a nested class?

This means references to any method or attribute of the outer class using the name or methodOuterClass() or OuterClass.this.Method() result in a NullPointerException.

Due to the problems listed below, e.g. Gson, construct objects without reference to the outer class. It is possible to repair this after the object has being created?

References

GSON does not deserialize reference to outer class

using member of outer class in a inner class makes null exception?

Community
  • 1
  • 1
ender.an27
  • 703
  • 1
  • 13
  • 35

2 Answers2

4

Thanks to @BeyelerStudios, unfortunately, he didn't put his comment as an answer.

I am putting an answer with example code:

public class Outer {     
    protected Inner inner = null; 
    public Inner getInner() {
        return inner;
    }      
    protected void something(){ }
    public void function(){
       inner.fixParent(this); // solve the reference lost
       inner.innerFunction(this);
    }
    public static void main(String[] args){
        String str = "{\"inner\":{\"name\"=\"test\"}}";
        Outer outer = (new Gson()).fromJson(str,Outer.class);
        System.out.println(outer.getInner().name);
        outer.function();
    }

    public class Inner {
        public String name = null;

        private void fixParent(Outer parent){// solution here
            try {
                field = Inner.class.getDeclaredField("this$0");
                field.setAccessible(true);
                field.set(this, parent);
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
       }
        protected void innerFunction(){
            Field field = null;                       
            something();//now works 
        }
    }
}
Community
  • 1
  • 1
ender.an27
  • 703
  • 1
  • 13
  • 35
0

There is a phenomenon that I encountered:

  • An inner class that is Serializable -The outer class is not

After deserializing and serializing again of the inner classes object,

  • the OuterClass.this was null.

The inner class should probably have been made static. Or very expensive, the outer class made Serializable.

Evidently you need the OuterClass. If you got the serialized file, you might be able to create a patching inner class, with same serialVersionUID and convert it to a correct variant of the class under other name.

That would be some accomplishment. It might be easier to transfer such a wrong instance to a new classes object.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
  • is that even possible? http://stackoverflow.com/questions/26422665/is-it-possible-to-serialize-anonymous-class-without-outer-class – AdamSkywalker Nov 20 '15 at 11:37
  • @AdamSkywalker now it was legacy software with a NPE of the `Outer.this` before a long time. I was very incredulous; still in fact, Now I can no longer say whether the class definitions changed in any way - between serialization and deserialization. "One could try it out" – Joop Eggen Nov 20 '15 at 11:49