User Tools

Site Tools


tutorial:interface_injection

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
tutorial:interface_injection [2025/11/10 02:07] – Add headings to interface injection article earthcomputertutorial:interface_injection [2025/11/10 03:38] (current) – Add info on how to inject generic interfaces earthcomputer
Line 70: Line 70:
  
 :!: Sometimes, your interface injections may need to include the ''$'' symbol, but the Groovy template processor may interpret this as a template variable if you are replacing variables (such as ''${version}'') in your ''fabric.mod.json''. A workaround for this is to use the Unicode escape for ''$'', which is ''\u0024''. :!: Sometimes, your interface injections may need to include the ''$'' symbol, but the Groovy template processor may interpret this as a template variable if you are replacing variables (such as ''${version}'') in your ''fabric.mod.json''. A workaround for this is to use the Unicode escape for ''$'', which is ''\u0024''.
 +
 +==== Generic interfaces ====
 +
 +If your interface has generics, you can specify them when you add the injected interface. For this, you need to add ''<>'' angled brackets and write the generics in Java bytecode signature format between them.
 +
 +^ Description                                   ^ Java example                 ^ Syntax                                                                                                                                                                         ^ Signature format example       ^
 +| Class type                                    | ''java.lang.String''         | ''L'' + internal name + '';''                                                                                                                                                  | ''Ljava/lang/String;''         |
 +| Array type                                    | ''java.lang.String[]''       | ''['' + element type                                                                                                                                                           | ''[Ljava/lang/String;''        |
 +| Primitive type (may appear as array elements) | ''double''                   | A single capital letter representing the type. Mostly logical, such as ''int'' = ''I'', ''double'' = ''D'', with the 2 exceptions of ''boolean'' = ''Z'' and ''long'' = ''J''. | ''D''                          |
 +| Type variable                                 | ''T''                        | ''T'' + name + '';''                                                                                                                                                           | ''TT;''                        |
 +| Generic class type                            | ''java.util.List<T>''        | ''L'' + internal name + ''<'' + generics + ''>;''                                                                                                                              | ''Ljava/util/List<TT;>;''      |
 +| Wildcard                                      | ''?'', ''java.util.List<?>'' | The ''*'' character                                                                                                                                                            | ''*'', ''Ljava/util/List<*>;'' |
 +| Extends wildcard bound                        | ''? extends String''         | ''+'' + the bound                                                                                                                                                              | ''+Ljava/lang/String;''        |
 +| Super wildcard bound                          | ''? super String''           | ''-'' + the bound                                                                                                                                                              | ''-Ljava/lang/String;''        |
 +
 +Here is a full example using generics:
 +<code json fabric.mod.json [enable_line_numbers="false"]>
 +{
 + "custom": {
 + "loom:injected_interfaces": {
 + "net/minecraft/class_3609": ["net/fabricmc/example/MyGenericInterface<+TT;TU;>"]
 + }
 + }
 +}
 +</code>
 +which would generate the implementation:
 +<yarncode java [enable_line_numbers="false"]>
 +public class class_3609 implements MyGenericInterface<? extends T, U> {
 +   // ...
 +}
 +</yarncode>
  
 ===== Step 4: Using the Injected Method ===== ===== Step 4: Using the Injected Method =====
tutorial/interface_injection.1762740421.txt.gz · Last modified: 2025/11/10 02:07 by earthcomputer