> ## Documentation Index
> Fetch the complete documentation index at: https://docs.selvut.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Java & Reflect

> Direct Java interop helpers.

Prefer high-level APIs first. Direct Java calls are advanced and can be version-sensitive.

## Java

| Function                          | Description                                              |
| --------------------------------- | -------------------------------------------------------- |
| `Java.mc()`                       | Raw Minecraft client instance                            |
| `Java.type(className)`            | Load a Java class by binary name                         |
| `Java.array(componentType, size)` | Create a Java array for raw API calls                    |
| `Java.arrayLength(array)`         | Length of a Java array returned by a raw API             |
| `Java.arrayGet(array, index)`     | Read an item from a Java array using zero-based indexing |

## Reflect

`Reflect` works with raw Java objects and class objects. It is useful for external libraries, Minecraft objects that are not yet wrapped by the script API, and small compatibility scripts.

For Minecraft classes, fields, and methods, use Mojang names. Selvut maps them to the runtime names automatically for `Java.type(...)`, `Reflect.classByName(...)`, field lookups, and method invocation.

| Function                                          | Description                                  |
| ------------------------------------------------- | -------------------------------------------- |
| `Reflect.classByName(name)`                       | Load a Java class by binary name             |
| `Reflect.newInstanceByName(name, ...args)`        | Instantiate by class name                    |
| `Reflect.newInstance(type, ...args)`              | Instantiate by class object                  |
| `Reflect.newInstanceByObject(object, ...args)`    | Instantiate another object of the same class |
| `Reflect.getField(target, fieldName)`             | Read a field from an object                  |
| `Reflect.getStaticField(type, fieldName)`         | Read a static field from a class             |
| `Reflect.invokeMethod(target, name, ...args)`     | Invoke instance method                       |
| `Reflect.invokeStaticMethod(type, name, ...args)` | Invoke static method                         |
| `Reflect.setInt(target, fieldName, value)`        | Set an int field                             |

```lua theme={null}
local list = Reflect.newInstanceByName("java.util.ArrayList")
Reflect.invokeMethod(list, "add", "a")
Reflect.invokeMethod(list, "add", "b")
Chat.log("size: " .. tostring(Reflect.invokeMethod(list, "size")))
```

`Reflect` returns primitive values directly. Object and class results stay raw so they can be passed back into other `Reflect` calls.

Raw calls are not automatically marshalled to the client thread. Raw Minecraft objects returned by entity/item/packet helpers are intentionally outside the stable scripting API.
