> For the complete documentation index, see [llms.txt](https://ddozzi.gitbook.io/mixin-tutorial/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ddozzi.gitbook.io/mixin-tutorial/how-to-make-a-mixin-client/5-or-injections.md).

# 5 | Injections

### What is Inject?

The `@Inject` annotation injects code into a specified target method.

Parameters, in order:

* All target method parameters
* `CallbackInfo` (for `void` return type on target) or `CallbackInfoReturnable<R>`

Return type: `void`

Consider the following code:&#x20;

```java
 public void target() {
      Dummy.getInstance().dummy("foo");
 }
```

If we were to inject into it, we would use something like this:

```java
@Inject(method = "target()V", at = @At(value = "INVOKE", target = "Lnet/example/Dummy;dummy(Ljava/lang/String;)V"))
private void mixin(CallbackInfo ci) {
    injectedCode();
}
```

It might be a little hard to understand at first, so lets break it down.

```java
@Inject(
    method = "target()V",
    at = @At(value = "INVOKE", target = "Lnet/example/Dummy;dummy(Ljava/lang/String;)V")
)
```

The first parameter is `method` , it tells the mixin what method we're going to modify

Now for the `@At` statement:

* `value = "INVOKE"`&#x20;
  * This tells the mixin that we're looking for the invocation of the method.
* `target = "Lnet/example/Dummy;dummy()V")`
  * This tells the mixin where the method we're modifying is located

Now, lets break down the syntax of `"Lnet/example/Dummy;dummy()V"`&#x20;

The `L` represents a class name, so `Lnet/example/Dummy;` is the path to the class where the method `dummy` , is located.

The `dummy` represents the method name inside the `Dummy` class.

Inside the brackets, we see another class `(Ljava/lang/String;)`, this is the path to the `String` class, as `String` is actually a class, just one in the standard library, which doesn't need to be imported, everything inside these brackets are the parameters for `dummy`

Just after the brackets there is a `V`, which simply means `void` specifying that the `println` function doesn't return any value

The table below shows the different characters with their different meanings.

| *FieldType* term | Type       | Interpretation                                                                    |
| ---------------- | ---------- | --------------------------------------------------------------------------------- |
| B                | byte       | signed byte                                                                       |
| C                | char       | Unicode character code point in the Basic Multilingual Plane, encoded with UTF-16 |
| D                | double     | double-precision floating-point value                                             |
| F                | float      | single-precision floating-point value                                             |
| I                | int        | integer                                                                           |
| J                | long       | long integer                                                                      |
| L                | ClassName; | reference an instance of class ClassName                                          |
| S                | short      | signed short                                                                      |
| Z                | boolean    | true or false                                                                     |
| \[               | reference  | one array dimension                                                               |

[Read more](https://docs.oracle.com/javase/specs/jvms/se14/html/jvms-4.html#jvms-4.3.2)

### Inject, cancelable

Consider the following code:

```java
 public void target() {
      Dummy.getInstance().dummy("foo");
 }
```

Example mixin:

```java
@Inject(
    method = "target()V", 
    at = @At(value = "INVOKE", 
    target = "Lnet/example/Dummy;dummy(Ljava/lang/String;)V"), 
    cancellable = true
)
private void mixin(CallbackInfo ci) {
    if (condition) {
        ci.cancel();
    }
}
```

Lets dissect this; First, we start off with defining our method, in this case, it's `target()` then we define the `@At` statement, which houses our `value` and our `target`.

Oh, but what's that? It's the `cancellable` statement!&#x20;

The `cancellable` statement, can only be used at `TAIL` or `RETURN` . It's job is to inject code into a target method, and if `ci.cancel` is called, it returns after the mixin is done executing.

So, if we were to apply our mixin to our `target()` function, we would have a result similar, if not exactly like this:

```java
 public void target() {
      Dummy.getInstance().dummy();
      {
          boolean canceled = false;
          if (condition) {
              canceled = true;
          }
          if (canceled) return;
     }
  }
```

{% hint style="success" %}
Congrats! Now you hold the power of injecting code to your heart's content!
{% endhint %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://ddozzi.gitbook.io/mixin-tutorial/how-to-make-a-mixin-client/5-or-injections.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
