LuaJava:一点一点写Example 1

 

Lua Reference

One of the goals of LuaJava is to allow the programmer to manipulate Java objects in the same way as it manipulates native (Lua) objects.

LuaJava creates a library in Lua called luajava. This library offers 5 functions:

 

LuaJava的目标之一就是允许程序猿使用java语法来操作java对象.LuaJava在库里面提供5种方法来操作java对象.

1 -newInstance(className, ...)

 

This function creates a new Java object, and returns a Lua object that is a reference to the actual Java object. You can access this object with the regular syntax used to access object oriented functions in Lua objects.

 

The first parameter is the name of the class to be instantiated. The other parameters are passed to the Java Class constructor.

newInstance()函数可以创建一个新java对象,同时返回一个真正的java对象的引用.这样就可以在Lua语言中使用语法糖通过面向对象的方式访问该对象.

 

例子:

hello.lua 文件

 --创建一个java对象
 local javaObject=luajava.newInstance("org.keplerproject.luajava.JavaObject")
 --调用java对象的方法
 javaObject:setName("JavaObject")
 --调用javaObject的toString()方法
 print(javaObject:toString())

 

在Lua语言中注册java类最重要为全类名.


JavaObject.java文件

package org.keplerproject.luajava;
public class JavaObject
{
    private String name;
	@Override
	public String toString() {
		return "JavaObject [name=" + name + "]";
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
}

 

main 方法:

	     //加载Lua 虚拟机
	     LuaState L = LuaStateFactory.newLuaState();
	    //加载Lua 自身类库
	     L.openLibs();
	     //加载hello.lua 模块
	     L.LdoFile("hello.lua");
	    L.close();


打印结果:JavaObject [name=JavaObject]

 

2-bindClass(className)

This function retrieves a Java class corresponding to className. The returned object can be used to access static fields and methods of the corresponding class.

bindClass() 接受一个class类. 然后返回一个对象可以访问该对象的静态知道和该类的方法.

 

hello.lua 文件:

sys = luajava.bindClass("java.lang.System")
print ( sys:currentTimeMillis() )

local bindObject =luajava.bindClass("org.keplerproject.luajava.JavaObject")
bindObject:showClassName()


JavaObject.java文件

package org.keplerproject.luajava;
public class JavaObject
{
    private String name;
    private static final int sid =1;
    private final int id =2;
    
	@Override
	public String toString() {
		return "JavaObject [name=" + name + "]";
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	public static void showClassName()
	{
		System.out.println(" org.keplerproject.luajava.JavaObject");
	}
}


打印结果:

 org.keplerproject.luajava.JavaObject
1445586382590

3-new(javaClass)

This function receives a java.lang.Class and returns a new instance of this class.

new works just like newInstance, but the first argument is an instance of the class.

Example:

str = luajava.bindClass("java.lang.String")
strInstance = luajava.new(str)

 

4 -createProxy(interfaceNames, luaObject)

We can also, instead of creating a Java object to be manipulated by Lua, create a Lua object that will be manipulated by Java.

We can do that in LuaJava by creating a proxy to that object. This is done by the createProxy function.

The function createProxy returns a java Object reference that can be used as an implementation of the given interface.

createProxy receives a string that contain the names of the interfaces to be implemented, separated by a comma(,), and a lua object that is the interface implementation.

 

hello.lua Lua文件

luaPrint={
    print=function(str)
          print('Printing from lua :'..str)
    end  
 
}


Printable.java文件:

package org.keplerproject.luajava;
//该接口定义print()方法
public interface Printable
{
	public void print(String str);
}


ObjPrint.java文件:

public class ObjPrint implements Printable
{
	@Override
	public void print(String str) {
		System.out.println("Printing from Java1..."+str);
	}
}


main文件:

	   //加载Lua 虚拟机
	   LuaState L = LuaStateFactory.newLuaState();
	   //加载Lua 自身类库
	    L.openLibs();
	    //加载hello.lua 模块
	    L.LdoFile("hello.lua");
	    
	    
	    Printable p = new ObjPrint();
	    p.print("TESTE 1");
	    //获取Lua中的全局变量
		LuaObject o = L.getLuaObject("luaPrint");
		//Lua对象创建PrintTable的代理
	    p = (Printable) o.createProxy("org.keplerproject.luajava.Printable");
	    //通过lua来实现
	    p.print("Teste 2");
	    L.close();


打印结果:

Printing from Java1...TESTE 1
Printing from lua :Teste 2

 

5-loadLib(className, methodName)

loadLib is a function that has a use similar to Lua's loadlib function. The purpose of this function is to allow users to write libraries in Java and then load them into Lua.

What loadLib does is call a static function in a given class and execute a given method, which should receive LuaState as parameter. If this function returns a integer, LuaJava takes it as the number of parameters returned by the the function, otherwise nothing is returned.

 

 


 注意::这些函数都被注册在LuaJava全局变量中.

JNIEXPORT void JNICALL Java_org_keplerproject_luajava_LuaState_luajava_1open
  ( JNIEnv * env , jobject jobj , jobject cptr , jint stateId )
{
  lua_State* L;

  jclass tempClass;
  //获取LuaVM虚拟机
  L = getStateFromCPtr( env , cptr );
  //在Lua中注册虚拟机索引
  lua_pushstring( L , LUAJAVASTATEINDEX );
  lua_pushnumber( L , (lua_Number)stateId );
  lua_settable( L , LUA_REGISTRYINDEX );

  //创建LuaJava全局变量Table,以下函数都注册在LuaJavaTable中
  lua_newtable( L );

  lua_setglobal( L , "luajava" );

  lua_getglobal( L , "luajava" );
  
  set_info( L);
  //注册bindClass 函数
  lua_pushstring( L , "bindClass" );
  lua_pushcfunction( L , &javaBindClass );
  lua_settable( L , -3 );
  //注册new函数
  lua_pushstring( L , "new" );
  lua_pushcfunction( L , &javaNew );
  lua_settable( L , -3 );
  //注册newInstance 函数
  lua_pushstring( L , "newInstance" );
  lua_pushcfunction( L , &javaNewInstance );
  lua_settable( L , -3 );

  lua_pushstring( L , "loadLib" );
  lua_pushcfunction( L , &javaLoadLib );
  lua_settable( L , -3 );

  lua_pushstring( L , "createProxy" );
  lua_pushcfunction( L , &createProxy );
  lua_settable( L , -3 );

  lua_pop( L , 1 );
}


 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值