LuaJ - 调用 Java 方法

我有一个 Java 类,其中有一个名为 test 的方法:

public class MyClass() {
    public String test() {
        //Do Something
    }
}

然后,我想在我的 Lua 脚本中调用 test 方法。为了做到这一点,我做了以下操作:

Globals globals = JsePlatform.standartGlobals();

LuaValue test = CoerceJavaToLua.coerce(new MyClass());
globals.set("obj", test);
LuaValue chunk = globals.load("obj.test()");
chunk.call();

当调用 Lua 脚本时,我收到了一个错误的参数错误。

只有在我使用"obj:test()"的时候才有效。

就像我应该将对象作为第一个参数传递一样。

有没有办法使"obj.test()"有效?

点赞
用户3586583
用户3586583

我找不到任何绑定类的静态函数并在没有类实例的情况下进行调用的示例。 即使使用静态函数,到处都有示例传递对象的实例。 因此,我无法100%确定这不可能做到(不编辑luaj)。

所有库和示例中的静态函数实际上都是通过创建虚构的类来实现的。例如,可以在此处查看[http://luaj.cvs.sourceforge.net/viewvc/luaj/luaj-vm/examples/jse/hyperbolic.java?view=markup](http://luaj.cvs.sourceforge.net/viewvc/luaj/luaj-vm/examples/jse/hyperbolic.java?view=markup)。

仔细查看luaj源代码表明,luaj不区分类中的静态和非静态函数,这意味着所有函数都被处理为非静态函数。有关更多信息,请参见JavaClass.java getMethod函数。

以下是一个相当简单的示例,您可以通过它来完成所需的操作,但不幸的是,它要求没有静态方法。

package luaj;

import org.luaj.vm2.*;
import org.luaj.vm2.lib.*;
import org.luaj.vm2.lib.jse.*;

public class luaj {

    static final public class MyClass {

        public static int asd = 5;

        static public class Test extends ZeroArgFunction {

            @Override
            public LuaValue call() {
                System.out.println("Worked");
                return NIL;
            }
        }
    }

    public static void main(String[] args) {
        Globals globals = JsePlatform.standardGlobals();
        LuaValue test = CoerceJavaToLua.coerce(new MyClass());
        globals.set("obj", test);
        LuaTable t = new LuaTable();
        t.set("test", new MyClass.Test());
        t.set("__index", t);
        test.setmetatable(t);
        LuaValue chunk = globals.load("print('Testing', obj.asd) obj.test()");
        chunk.call();
    }
}

类似的方法可以通过将“ obj.test”设置为包装真实“ obj.test”的函数并将一个新的“ MyClass”实例传递给它来完成。从而“隐藏”了传递实例的方式。

2016-01-08 13:39:29
用户5761021
用户5761021

我不太用 Lua,但你说的做法是可行的。

LuaValue chunk = globals.load("obj:test()");

请注意冒号,这是方法应该被调用的方式。

正如我之前提到的那样,虽然我不太懂,但看起来你要这样做才可以。

2016-01-08 13:46:55
用户2556943
用户2556943

你也可以使用等效的语法:

obj.test(obj)

与以下语法相同:

obj:test()

您应该能够访问Java类或实例的任何字段或方法,但在某些情况下需要额外的参数,而冒号语法是一种方便的快捷方式。

为了说明这一点,构造一个具有各种静态和实例字段和方法的类,如下所示:

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";
    }

如果像您的示例中一样强制使用该类的实例,则可以使用以下语法访问每个实例:

Globals globals = JsePlatform.standardGlobals();

// 将实例加载到全局对象中
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() );" + // 与 'obj.method (obj)' 相同
        "print( obj.method(obj) );");
chunk.call();

对我来说,这个例子会产生以下输出:

Example$MyClass@4554617c
variable-value
field-value
function: JavaMethod
function: JavaMethod
method-result
method-result

您也可以只是强制使用该类。除了字段外,仍然可以访问所有内容,因为对于类而言,该字段不存在:

// 将类加载到全局对象中,无法访问 'field'
LuaValue cls = CoerceJavaToLua.coerce(MyClass.class);
globals.set("cls", cls);
chunk = globals.load(
        "print( cls );" +
        "print( cls.variable );" +
        "print( cls.func );" +
        "print( cls:func() );" + // 与 'cls.method (cls)' 相同
        "print( cls.func(cls) );" +
        "print( cls.method );" +
        "print( cls.method(obj) );");
chunk.call();

输出如下:

class Example$MyClass
variable-value
function: JavaMethod
function-result
function-result
function: JavaMethod
method-result

在lua中,可以通过 'new'从Java类构造实例,然后它就如同其他Java实例一样:

// 使用 'new' 从类构造实例
chunk = globals.load(
        "print( cls.new );" +
        "print( cls.new() );" +
        "print( cls.new().field );"); // etc.
chunk.call();

输出为:

function: JavaConstructor
Example$MyClass@4b67cf4d
field-value
2016-01-23 06:57:29