You can also use the equivalent syntax:
obj.test(obj)
It's the same as
obj:test()
You should be able to get at any filed or method of a Java class or instance, but in certain cases the extra argument is required, and the colon syntax is a convenient shortcut.
To illustrate, construct a class with various static and instance fields and methods such as this:
public static class MyClass {
public static String variable = "variable-value";
public String field = "field-value";
public static String func() {
return "function-result";
}
public String method() {
return "method-result";
}
If you coerce an instance of that class as in your example, here is syntax to access each:
Globals globals = JsePlatform.standardGlobals();
// Load an instance into globals
LuaValue instance = CoerceJavaToLua.coerce(new MyClass());
globals.set("obj", instance);
LuaValue chunk = globals.load(
"print( obj );" +
"print( obj.variable );" +
"print( obj.field );" +
"print( obj.func );" +
"print( obj.method );" +
"print( obj:method() );" + // same as 'obj.method(obj)'
"print( obj.method(obj) );");
chunk.call();
which for me produced
Example$MyClass@4554617c
variable-value
field-value
function: JavaMethod
function: JavaMethod
method-result
method-result
You may also want to just coerce the class instead. Everything can still be accessed except the field, which doesn't exist for a class:
// Load a class into globals, 'field' cannot be accessed
LuaValue cls = CoerceJavaToLua.coerce(MyClass.class);
globals.set("cls", cls);
chunk = globals.load(
"print( cls );" +
"print( cls.variable );" +
"print( cls.func );" +
"print( cls:func() );" + // same as 'cls.method(cls)'
"print( cls.func(cls) );" +
"print( cls.method );" +
"print( cls.method(obj) );");
chunk.call();
The output is:
class Example$MyClass
variable-value
function: JavaMethod
function-result
function-result
function: JavaMethod
method-result
Within lua, an instance can be constructed from a Java class via 'new', then it behaves like other Java instances:
// Construct an instance from a class using 'new'
chunk = globals.load(
"print( cls.new );" +
"print( cls.new() );" +
"print( cls.new().field );"); // etc.
chunk.call();
and the output is
function: JavaConstructor
Example$MyClass@4b67cf4d
field-value