Skip to content
This repository has been archived by the owner on Sep 5, 2024. It is now read-only.

Commit

Permalink
Shadow annotation arguments and usages
Browse files Browse the repository at this point in the history
Added `def_list` markdown extension
  • Loading branch information
Rongmario committed Sep 21, 2023
1 parent d0c5305 commit dfad9d2
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 0 deletions.
101 changes: 101 additions & 0 deletions docs/mod-development/mixin/annotation/shadow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
`@Shadow` is used as placeholders for in a Mixin class. Where you would want to access fields and methods like how you would if you were working in the class you are Mixin'ing.

### ^^Arguments^^
`remap`

: `[Optional Boolean, default value: true`

The annotation processor will skip this if this is false. With the refmap skipping over this member. This is useful for members originating from mods and not Vanilla Minecraft.

`aliases`

: `[Optional String Array, default value: {}`

This can be populated with other aliases, particularly useful when shadowing synthetic members, or if the member is known to have name-changes at runtime from other sources of transformation

`prefix`

: `[Optional String, default value: "shadow$"]`

For compilers it is illegal to allow methods to have the same name, same arguments, regardless of the return types. This may be an issue when using `@Shadow`. Hence the `prefix` takes care of that for you, at runtime when the mixin is being applied, the name will be restored with the prefix removed.

```java
public class Illegal {

protected String method(String argument) {
return "";
}

protected int method(String argument) {
return 0;
}

}

public class Legal {

@Shadow(prefix = "alt$")
protected String alt$method(String argument) {
return "";
}

protected int method(String argument) {
return 0;
}

}
```

### ^^Usages^^

??? abstract "Shadowing a Field"
```java title="Target.java"
public class Target {

private String privateValue = "Hello World";
protected int protectedValue = 42;

}
```

```java title="Example.java"
@Mixin(Target.class)
public class Example {

@Shadow private String privateValue = null; // Will not affect original field

@Shadow protected int protectedValue = 0; // Will not affect original field

}
```

??? abstract "Shadowing a Method"
```java title="Target.java"
public class Target {

public void method() {
// ...
}

public int fruitfulMethod() {
// ...
}

}
```

```java title="Example.java"
@Mixin(Target.class)
public class Example {

@Shadow public void method() { }

// If the method needs to return something
// You can return primitive defaults or null
// But throwing an exception here helps if something goes extremely wrong
@Shadow public int fruitfulMethod() {
throw new AssertionError();
}

}
```
3 changes: 3 additions & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ nav:
- Mixin:
- Preface: mod-development/mixin/preface.md
- MixinBooter: mod-development/mixin/mixinbooter.md
- Annotation:
- "@Shadow": mod-development/mixin/annotation/shadow.md
- Environment:
- Registration: mod-development/mixin/environment/registration.md
- Configuration: mod-development/mixin/environment/configuration.md
Expand Down Expand Up @@ -63,6 +65,7 @@ plugins:

markdown_extensions:
- admonition
- def_list
- attr_list
- md_in_html
- footnotes
Expand Down

0 comments on commit dfad9d2

Please sign in to comment.