Shadowing a non-void returning method (Sponge Mixins)

Hey all, I’m new here so I don’t know if this is the correct space to post this.

I’ve seen on the Mixin docs that you can shadow methods without a body like so
@Shadow private void update() {}

My question is how does shadowing work if the method returns something?
@Shadow public EntityType<?> getType() {}
-> cannot compile, “missing return statement”

(Background: I’m using sponge with fabric to mod MC 1.15.2)
Thanks in advance

A valid mixin has first to be a syntactically correct java class. The best way to make this compiles is to declare your class abstract and declare the shadowed methods abstract too so you don’t need any method body.

@Shadow private abstract void update();
@Shadow public abstract EntityType<?> getType();
1 Like

Ohh I see. I thought that making the class abstract would invalidate the mixin call unless the class being mixed into was also abstract.

As a note, you can’t have a private abstract method in Java because you wouldn’t be able to override it. In these cases, and when there is a non-void return type, just declare some dummy body (I’d advise throwing some sort of runtime exception so it’s clear if something has gone wonky like we do here) - it won’t get merged into the actual class at runtime.

Otherwise, best practice to declare all your mixins as abstract and when you can, make the definition of methods abstract.