-
-
Notifications
You must be signed in to change notification settings - Fork 2
Module: Reflection
Important
Documentation has moved to the docs. This wiki will no longer be updated, and may be deleted in the future.
local Reflection = require("Starlit/utils/Reflection")The reflection module provides functions for inspecting and modifying classes and objects, both from Lua and from Java.
Reflection.getClassName(object: any) -> stringReturns the string name of the class of the passed object. If the class has been registered with Reflection.registerClassName, it will return that name. In the case of Java objects, this will be the short name of the class, without the names of any packages or enclosing classes. If it is a Lua object, it will attempt to read the class's name from the Type field, which is set by ISBaseObject. If there is no Type, it will return "table". If the passed object is not a java object or lua object, it will return the lua name of the type (e.g. "string", "number", etc)
Reflection.registerClassName(metatable: table, name: string)Registers the name of the class defined by the passed metatable as the passed name. This will then be returned by Reflection.getClassName() by objects using that metatable.
Reflection.getClassFieldNames(class: string) -> string[]?Returns a table of field names declared by the java class named by the class argument. Currently does not support unexposed classes.
Reflection.getField(object: any, name: string) -> anyReturns the value of the field name on the object object. This is less efficient than a field access using the object.name syntax, but works even on objects of classes that are not exposed.
Reflection.hasLocal(callframeOffset: integer, name: string) -> hasLocal: booleanReturns true if the callframe callframeOffset callframes down the stack has a local by the name name, otherwise returns false.
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.
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.
Reflection.getLocalValue(callframeOffset: integer, name: string) -> value: anyReturns the value of a local variable name from the callframe *callframeOffset callframes down the stack. Returns nil if there was no local by that name: use Reflection.hasLocal to distinguish this from a local with the value nil.
Reflection.getLocalName(callframeOffset: integer, value: any) -> name: string?Returns the string name of the first local variable found with the value value in the callframe *callframeOffset callframes down the stack. Returns nil if there is no local with that value.
Reflection.getLocals(callframeOffset: integer) -> locals: table<string, any>Returns a key-value table containing every local variable in the callframe *callframeOffset callframes down the stack.