tutorial:mixin_tips
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionLast revisionBoth sides next revision | ||
tutorial:mixin_tips [2022/08/05 12:28] – Added shadow annotation to second example (forgot before) clomclem | tutorial:mixin_tips [2023/12/18 01:54] – solidblock | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== Mixin Tips ====== | + | FIXME //This page is currently being written, and may change at any time.// |
+ | ====== Mixin Tips (WIP) ====== | ||
- | ==== Why make a class abstract? ==== | + | This is a collection of different tips some might find useful. It's recommended to read the previous articles to understand the tips. |
- | **1. Prevent instantiation** | + | ===== Why make a class abstract? ===== |
+ | |||
+ | ==== 1. Prevent instantiation | ||
It's fair to say that you should never instantiate a mixin class, mainly because the class doesn' | It's fair to say that you should never instantiate a mixin class, mainly because the class doesn' | ||
- | Declaring a mixin class abstract doesn' | + | Declaring a mixin class abstract doesn' |
<code java> | <code java> | ||
Line 15: | Line 18: | ||
- | **2. Make more elegant shadow methods** | + | ==== 2. Make more elegant shadow methods |
If you want to access a unaccessible method or field from your target class into your mixin class, you need to use '' | If you want to access a unaccessible method or field from your target class into your mixin class, you need to use '' | ||
Line 23: | Line 26: | ||
<code java> | <code java> | ||
@Shadow | @Shadow | ||
- | private | + | protected |
</ | </ | ||
Line 29: | Line 32: | ||
<code java> | <code java> | ||
@Shadow | @Shadow | ||
- | private | + | protected |
</ | </ | ||
+ | Note: this doesn' | ||
- | **3. Access the "this" | + | ==== 3. Access the '' |
- | In mixin, if you want to access the " | + | In mixin, if you want to access the " |
<code java> | <code java> | ||
Line 41: | Line 45: | ||
</ | </ | ||
- | But that only works if your mixin class extends/ | + | But that only works if your mixin class extends/ |
+ | |||
+ | Luckily, this can all be avoided by using an abstract class, in which case you don't have to implement methods and all problems are avoided. | ||
+ | |||
+ | ===== How to mixin inner classes ===== | ||
+ | |||
+ | ==== 1. Normal inaccessible inner classes ==== | ||
+ | |||
+ | Since this you can't directly access (and hence mention) these classes from outside, you need to use the " | ||
+ | |||
+ | You do this by using the complete name of the outer class, then a '' | ||
+ | |||
+ | Class: | ||
+ | |||
+ | <code java> | ||
+ | package some.random.package; | ||
+ | |||
+ | public class Outer { | ||
+ | | ||
+ | | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | Mixin with injection: | ||
+ | |||
+ | <code java> | ||
+ | @Mixin(targets = " | ||
+ | public class MyMixin { | ||
+ | @Inject(method = " | ||
+ | private void injected(CallbackInfo ci) { | ||
+ | // your code here | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | The only caveat is that if you want to mixin into the inner class constructor, | ||
+ | |||
+ | <code java> | ||
+ | @Inject(method = "< | ||
+ | private void injected(CallbackInfo ci) { | ||
+ | // your code here | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | |||
+ | ==== 2. Static inaccessible inner classes ==== | ||
+ | |||
+ | These are the same as above, the only difference is that the constructor doesn' | ||
+ | |||
+ | |||
+ | ==== 3. Anonymous inner classes ==== | ||
- | Luckily, this can all be avoided by using an abstract class, in which case you don't have to implement | + | These are the same as the static inaccessible inner classes, the only difference is that since they don't have a name, they are declared by appearance order, for example: |
** UNDER CONSTRUCTION ** | ** UNDER CONSTRUCTION ** | ||
tutorial/mixin_tips.txt · Last modified: 2023/12/18 02:06 by solidblock