Reflection

local Reflection = require("Starlit/utils/Reflection")

The reflection module provides functions for inspecting and modifying classes and objects, both from Lua and from Java.

Functions

Reflection.getField(object: any, name: string) any

Returns the value of an object’s field by name. This can retrieve field values even from objects of unexposed classes. It also works for exposed classes however it is less performant than the regular syntax.

Parameters:
  • object (any) – The object.

  • name (string) – The name of the field.

Returns:

value (any) – The value of the field.

Reflection.getClassFieldNames(class: string) string[] | nil

Returns a table of the names of every field belonging to that class. :param string class: Name of the class. :return string[] | nil fields: Field names.

Reflection.hasLocal(callframeOffset: number, name: string) hasLocalVariable: boolean

Returns whether the specified callframe has a local variable by that name.

Parameters:
  • callframeOffset (integer) – How many callframes downwards to search for the local.

  • name (string) – The name of the local variable.

Returns:

hasLocalVariable (boolean) – Whether the callframe had a local variable by that name.

Reflection.getLocalValue(callframeOffset: integer, name: string) any: value

Returns the value of a local variable by its name.

Parameters:
  • callframeOffset (integer) – How many callframes downwards to search for the local.

  • name (string) – The name of the local variable.

Returns:

value (any) – The value of the local variable, or nil if there was no such local. A non-existent local is indistinguishable from a local containing the value nil.

Reflection.getLocalName(callframeOffset: integer, value: any) name: string | nil

Returns the name of a local variable by its value.

Parameters:
  • callframeOffset (integer) – How many callframes downwards to search for the local.

  • value (any) – The value of the local to search for.

Returns:

name (string | nil) – The name of the first local variable containing the value, or nil if no local variable containing the value could be found.

Reflection.getLocals(callframeOffset: integer) locals: table<string, any>

Returns a table containing all of the local variables in a callframe.

Parameters:

callframeOffset (integer) – How many callframes downwards to get locals from.

Returns:

locals (table<string, any>) – The local variables in the callframe.

Reflection.getClassName(object: any) string

Returns the name of the java class or lua type that object is an instance of

Parameters:

object (any) – The object.

Returns:

name (string) – Name of the object’s class.

Reflection.registerClassName(metatable: metatable, name: string)

Registers a class’s name for getClassName.

Parameters:
  • metatable (metatable) – Metatable of the class.

  • name (string) – Name of the class.

Glossary

callframe

Callframes are the context of a function call. When a function is called a new callframe is created and placed on top of the stack. Remember that all Lua code execution is internally the same as a function call, so a file running for the first time is also a callframe, as well as code ran in the console.

Callframes should not be confused with scopes. A callframe will be able to see locals from all scopes visible to the currently executing line of code in that callframe.

For these functions, offset 0 refers to the current function. Offset 1 refers to the function calling the current function.

Note

In some limited circumstances, locals may be optimised out and will not actually exist at runtime, even if they are defined in the source file.