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/17 22:20] – Stylized "this" clomclem | tutorial:mixin_tips [2023/12/18 01:54] – solidblock | ||
---|---|---|---|
Line 3: | Line 3: | ||
====== Mixin Tips (WIP) ====== | ====== Mixin Tips (WIP) ====== | ||
+ | This is a collection of different tips some might find useful. It's recommended to read the previous articles to understand the tips. | ||
- | ==== Why make a class abstract? ==== | + | ===== Why make a class abstract? |
- | **1. Prevent instantiation** | + | ==== 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 17: | 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 25: | Line 26: | ||
<code java> | <code java> | ||
@Shadow | @Shadow | ||
- | private | + | protected |
</ | </ | ||
Line 31: | Line 32: | ||
<code java> | <code java> | ||
@Shadow | @Shadow | ||
- | private | + | protected |
</ | </ | ||
+ | Note: this doesn' | ||
- | **3. Access the '' | + | ==== 3. Access the '' |
In mixin, if you want to access the " | In mixin, if you want to access the " | ||
Line 47: | Line 49: | ||
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. | 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 ===== | ||
- | ==== How to mixin inner classes ==== | + | ==== 1. Normal inaccessible |
- | + | ||
- | **1. Normal inaccessible inner classes ** | + | |
Since this you can't directly access (and hence mention) these classes from outside, you need to use the " | Since this you can't directly access (and hence mention) these classes from outside, you need to use the " | ||
Line 73: | Line 74: | ||
@Mixin(targets = " | @Mixin(targets = " | ||
public class MyMixin { | public class MyMixin { | ||
- | @Inject(method = " | + | @Inject(method = " |
private void injected(CallbackInfo ci) { | private void injected(CallbackInfo ci) { | ||
// your code here | // your code here | ||
Line 80: | Line 81: | ||
</ | </ | ||
- | The only caveat is that if you want to mixin into the inner class constructor, | + | The only caveat is that if you want to mixin into the inner class constructor, |
<code java> | <code java> | ||
- | @Inject(method = "< | + | @Inject(method = "< |
private void injected(CallbackInfo ci) { | private void injected(CallbackInfo ci) { | ||
// your code here | // your code here | ||
Line 90: | Line 91: | ||
- | **2. Static inaccessible inner classes** | + | ==== 2. Static inaccessible inner classes |
These are the same as above, the only difference is that the constructor doesn' | These are the same as above, the only difference is that the constructor doesn' | ||
- | **3. Anonymous inner classes** | + | ==== 3. Anonymous inner classes |
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: the anonymous inner class if declared in our previous example class first would be named Outer$1, a second one would be named Outer$2, a third one Outer$3 and so on (the declaration order is on source level). | 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: the anonymous inner class if declared in our previous example class first would be named Outer$1, a second one would be named Outer$2, a third one Outer$3 and so on (the declaration order is on source level). |
tutorial/mixin_tips.txt · Last modified: 2023/12/18 02:06 by solidblock