--
-- lua调用java实例测试
--
-- author:Lonvoy
-- createDate:20151009
--
-- newInstance 通过class2Name,获取实例对象
local ljTest = luajava.newInstance("LuaJavaTestByLonvoy");
-- bindClass 可以使lua访问class的静态成员
local ljTestStatic = luajava.bindClass("LuaJavaTestByLonvoy");
-- new 通过class对象返回对应类的实例
local ljTestNew = luajava.new(ljTestStatic);
print("测试获取hello方法");
print(ljTest:hello());
print("测试获取testString方法");
print(ljTest:testString("Lonvoy"));
print("测试获取testMap方法");
local jMap = ljTest:testMap();
print("testMap返回对象:");
print(jMap);
luaJavaUtil = require("luaJavaUtil");
testTable = luaJavaUtil.convert_map(jMap);
for k, v in pairs(testTable) do
print(k, v);
end
对应Java测试类,如下:
/**
* lua调用java测试类
* @author Lonvoy
* @createDate 2015年10月8日
*
*/
public class LuaJavaTestByLonvoy {
/**
* 欢迎
* @return
*/
public String hello(){
return "Hello Lonvoy!";
}
/**
* 私有方法测试
* @return
*/
@SuppressWarnings("unused")
private String testPrivate(){
return "this method is private, you can call it!";
}
/**
* 测试 返回String类型
* @param arg
* @return
*/
public String testString(String arg){
return "your input param is :".concat(arg);
}
/**
* 测试返回 Map 类型
* @return
*/
public Map testMap(){
Map map = new HashMap();
map.put("A", "value type is String");
map.put("B", 10);
map.put("C", false);
map.put("D", 6.9);
map.put("E", new String [] {"11","22","33"});
Set s = map.
return map;
}
/**
* 测试返回List类型
* @return
*/
public List testList(){
List list = new ArrayList();
list.add("first");
list.add("second");
list.add("third");
list.add("fourth");
return list;
}
}