SpongePowered / Mixin

Mixin is a trait/mixin and bytecode weaving framework for Java using ASM
MIT License
1.41k stars 194 forks source link

How can I access the `this` of an outer class? #501

Closed DethRaid closed 3 years ago

DethRaid commented 3 years ago

I'm making a mixin to replace some code in a non-static inner class. I need to call a method on the outer class - which means I need a reference to the outer class. How can I get a reference to the outer class from within a mixin?

Geolykt commented 3 years ago

From what I am aware of Javac will create a field called this$0 or similar (check with a primitive decompiler that does not attempt to remove synthetics) that contains a reference to it. You can simply do a shadow on that field

DethRaid commented 3 years ago

Is there a primitive decompiler you'd recommend, or do you know how to tell IntelliJ's decompiler to not remove synthetics?

Geolykt commented 3 years ago

Recaf's (or most other GUI bytecode viewers) table mode does a good enough job for this, but from my experience procyon is also pretty good at such stuff

DethRaid commented 3 years ago

Ooh, perfect

Turns out, the field is named field_20839 in this class lol

Thank you for your help!

pentyum commented 2 years ago

I can compile successfully, and the mod works well, but an error is reported during the compilation: Cannot find target for @Shadow field in net.minecraft.item.map.MapState$PlayerUpdateTracker,my shadow field is

@Final
@Shadow()
MapState field_132;

I want to know how to remove this error report, thanks.

ryantheleach commented 2 years ago

@pentyum I don't believe you intended to comment as a reply to this ticket. You may want to check which issue / thread you are in.

pentyum commented 2 years ago

@pentyum I don't believe you intended to comment as a reply to this ticket. You may want to check which issue / thread you are in.

I have a same problem. I want to access MapState.this in the non-static inner class net.minecraft.item.map.MapState$PlayerUpdateTracker. I find this field has a name field_132 in the bytecode. MapState.this is indeed accessible through field_132 in my mixin class, but an error will be reported during compilation. I have tried adding the annotation @SuppressWarnings("ShadowTarget") but it doesn't work.

Mumfrey commented 2 years ago

This is an issue tracker not a support forum. Since your question doesn't relate to a bug or feature with mixin please use discord for support.