tutorial:mixin_examples
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
tutorial:mixin_examples [2021/08/07 07:08] – Add "modifying a constant" example fracturedcode | tutorial:mixin_examples [2023/10/12 06:17] – Add note to double casting example daomephsta | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== Mixin Examples ====== | ====== Mixin Examples ====== | ||
This is a collection of frequently used mixins. | This is a collection of frequently used mixins. | ||
+ | This page is intended as a cheat sheet. | ||
+ | See [[tutorial: | ||
+ | |||
+ | ===== Mixing into a private inner class ===== | ||
+ | Use the targets parameter and a '' | ||
+ | <code java> | ||
+ | @Mixin(targets = " | ||
+ | public class AmbientOcclusionCalculatorMixin { | ||
+ | // do your stuff here | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ===== Access the this instance of the class your mixin is targeting ===== | ||
+ | Note: Double casting '' | ||
+ | If the method or field is from the target class, use `@Shadow` | ||
+ | If the method or field is from a parent of the target class, have your mixin extend the direct parent of the target class. | ||
+ | |||
+ | Mixin: | ||
+ | <code java> | ||
+ | @Mixin(TargetClass.class) | ||
+ | public class MyMixin extends EveryThingThatTargetClassExtends implements EverythingThatTargetClassImplements { | ||
+ | @Inject(method = " | ||
+ | private void injected(CallbackInfo ci) { | ||
+ | ((TargetClass)(Object)this).methodOfTheTargetClass(); | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ===== Injecting into the head of a static block ===== | ||
+ | Mixin: | ||
+ | <code java> | ||
+ | @Inject(method = "< | ||
+ | private void injected(CallbackInfo ci) { | ||
+ | doSomething3(); | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | Result: | ||
+ | <code diff> | ||
+ | static { | ||
+ | + | ||
+ | doSomething1(); | ||
+ | doSomething2(); | ||
+ | } | ||
+ | </ | ||
===== Injecting into the head of a method ===== | ===== Injecting into the head of a method ===== | ||
Line 101: | Line 146: | ||
+ | + | ||
doSomething2(); | doSomething2(); | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ===== Injecting into the point before a method call with shift amount ===== | ||
+ | Mixin: | ||
+ | <code java> | ||
+ | @Inject(method = " | ||
+ | private void injected(CallbackInfo ci) { | ||
+ | doSomething3(); | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | Result: | ||
+ | <code diff> | ||
+ | public void foo() { | ||
+ | doSomething1(); | ||
+ | Something something = new Something(); | ||
+ | something.doSomething(); | ||
+ | doSomething2(); | ||
+ | + | ||
} | } | ||
</ | </ | ||
Line 181: | Line 246: | ||
doSomething3(); | doSomething3(); | ||
return 10; | return 10; | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ===== Capturing local values ===== | ||
+ | |||
+ | Mixin: | ||
+ | <code java> | ||
+ | @Inject(method = " | ||
+ | private void injected(CallbackInfo ci, TypeArg1 arg1) { | ||
+ | // | ||
+ | arg1.doSomething4(); | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | Result: | ||
+ | <code diff> | ||
+ | public void foo() { | ||
+ | TypeArg1 arg1 = getArg1(); | ||
+ | arg1.doSomething1(); | ||
+ | arg1.doSomething2(); | ||
+ | TypeArg2 arg2 = getArg2(); | ||
+ | arg2.doSomething3(); | ||
+ | + | ||
} | } | ||
</ | </ | ||
Line 375: | Line 463: | ||
public void foo() { | public void foo() { | ||
- for (int i = 0; i < 4; i++) { | - for (int i = 0; i < 4; i++) { | ||
- | + for (int i = 0; i < 5; i++) { | + | + for (int i = 0; i < injected(4); i++) { |
doSomething(i); | doSomething(i); | ||
} | } | ||
} | } | ||
</ | </ |
tutorial/mixin_examples.txt · Last modified: 2024/01/13 15:02 by arkosammy12