从Luaj继承Java类

时间:2014-10-25 15:15:27

标签: java lua luaj

在Luaj中,是否有可能让Lua类扩展Java类?我尝试在绑定类上使用getmetatable(),但显然它会返回nil

这里,Wizard是绑定到Lua的Java类,SetupWizard是我想要从Wizard继承的Lua类。

function SetupWizard:new()
    local self = setmetatable({}, getmetatable(Wizard))
    return self
end

__index分配给Wizard的值也不起作用。

SetupWizard定义:

SetupWizard = {
    host = nil,
    user = nil,
    password = nil,
    database = nil,
    mySqlTables = {
        users = nil,
    },
}
SetupWizard.__index = Wizard

... SetupWizard methods here

2 个答案:

答案 0 :(得分:0)

LuaJ在如何处理诸如从java导入类之类的事情方面相当灵活。 你还没有说明你是如何做到这一点所以我假设你已经通过创建一个library of Java Functions作为构建你可以要求的模块的桥梁来解决它(与require"moduleName"}喜欢这样:

import org.luaj.vm2.LuaValue;
import org.luaj.vm2.lib.OneArgFunction;
import org.luaj.vm2.lib.TwoArgFunction;


/*
 * When require"module name" is called in luaJ it also searches for java classes with a classname that matches
 * the given module name that inherit from LuaFunction and have a default constructor, it then
 * calls the method call(LuaValue moduleName, LuaValue globalEnviroment) passing the module name string and
 * global environment as LuaValues and returning the return value of this call as it's own. 
 * 
 */

public class Example extends TwoArgFunction{

    @Override
    public LuaValue call(LuaValue modname, LuaValue globalEnv) {
        // Creates a table to place all our functions in
        LuaValue table = LuaValue.tableOf();

        // Sets the value 'name' in our table to example. This is used in the example function
        table.set("name", "example");
        // Sets the value 'exampleMethod' in our table to the class of type OneArgFunction
        // we created below
        table.set("exampleMethod", exampleMethod);

        //Finally returns our table. This value is then returned by require.
        return table;
    }


    /* Creates a function that takes one arg, self.

      This emulates the creation of a method in lua and can be called,
      when added to a table, as table:exampleMethod().
      This is possible as in Lua the colon in functions is
      Syntactic suger for object.function(object) 

    */
    OneArgFunction exampleMethod = new OneArgFunction() {

        @Override
        public LuaValue call(LuaValue self) {

            if(self.istable()) {
                return self.get("name");
            }

            return NIL;
        }
    };

}

然后可以在Lua代码中使用它,如下所示:

--Imports the class Wrapper we created
example = require"Example"
--Prints the return value of calling exampleMethod
print("The name of the class is: "..example:exampleMethod())


--And then used as an index like so
example2 = setmetatable({},{__index=example})

example2.name = "example2"

print("The name of the class is: "..example2:exampleMethod())

正如我在答案的顶部所说,LuaJ在如何处理这些事情方面是灵活的,这只是我这样做的方式。

由于我的声誉,我无法发表评论,询问您是如何进行导入的 LuaJ中的Java类,可以在对这个答案的评论中自由澄清。

答案 1 :(得分:0)

您的基于Java的LuaJ模块需要返回一个表,并使用

local SetupWizard = require("SetupWizard")
local Wizard = {}
setmetatable(Wizard, {__index=SetupWizard})
--optionally create single metatable for all Wizard instances: local wizardMetaTable = {_index=Wizard}
function Wizard:new()
  local T = {}
  setmetatable(T, {__index=Wizard})
  -- setmetatable(T, wizardMetaTable)
  -- this increases performance by using one metatable for all wizards.
  return T
end