Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow interface Mixins #51

Merged
merged 3 commits into from
Jun 28, 2021
Merged

Allow interface Mixins #51

merged 3 commits into from
Jun 28, 2021

Conversation

Chocohead
Copy link

Reimplements #13 more expansively, allows things like...

public interface Interface {
	String GENERATED_CONSTANT = generate();

	private static String generate() {
		return "generated";
	}

	public static String make() {
		return "made";
	}

	void eat(int food);

	default int next(int start) {
		return start + 1;
	}
}

@Mixin(Interface.class)
interface InterfaceMixin {
	@Inject(method = "make", at = @At("RETURN"), cancellable = true)
	private static void remake(CallbackInfoReturnable<String> call) {
		call.setReturnValue("remade");
	}

	@ModifyVariable(method = "next", argsOnly = true, at = @At("HEAD"))
	default int switchNext(int start) {
		hit();
		return start - 1;
	}

	@Shadow
	private static String generate() {
		throw new AssertionError();
	}

	private void hit() {
		System.out.println(generate());

		Arrays.asList("one", "two", "three").removeIf(thing -> {
			//No more IncompatibleClassChangeErrors from lambdas in interfaces
			return false; //The list is fixed length so we don't actually want it to succeed
		});
	}
}

Whilst SpongePowered#415 is generating things more correctly, there are mods (such as Better End) which rely on the current behaviour
@frqnny
Copy link

frqnny commented Jun 16, 2021

Will this allow people to inject into things like Inventory and run code every time, for example, getStack or something gets called?

Copy link

@sfPlayer1 sfPlayer1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks reasonable to me

@Chocohead
Copy link
Author

Will this allow people to inject into things like Inventory and run code every time, for example, getStack or something gets called?

No, this is strictly for Mixining into the interface itself. With Inventory that means you could inject into onOpen or onClose and anything using the default implementation would then run your injection, for example, but like with abstract classes you can't inject into an abstract method as it has no method body to inject into.

Copy link

@LambdAurora LambdAurora left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Quite an essential PR that I hope to see merged soon.

Copy link

@Mysterious-Dev Mysterious-Dev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants