B4J Question how can i get class id to share with a java library ?

Waldemar Lima

Well-Known Member
Licensed User
Longtime User
hello everyone !!
i am creating a LuaJ library , and i need to get a B4J class module id to register class to Functions Table on lua ...

this is a Java Function to register new class to lua functions table >
Java:
public void RegisterClassToLua(Class c)  {
       
       
        try {
            // on this line i need get B4J class to register class on function tables
            LuaValue instance = CoerceJavaToLua.coerce(c.newInstance());
            globals.set("obj", instance);
        } catch (InstantiationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
       
        // Use the convenience function on Globals to load a chunk.
        //LuaValue chunk = globals.load(script);

       
    }

when i try compile i get this error >
Screenshot_5.png
 

stevel05

Expert
Licensed User
Longtime User
It looks like the constructor needs a Class Object rather than an instance of the class.

Try something like this:

B4X:
Dim Jo as JavaObject = test
Dim Class As Object = Jo.RunMethod("getClass",Null)

Then pass the Class Object.
 
Last edited:
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
Not tried but you could probably change
B4X:
public void RegisterClassToLua(Class c)  {
       
       
        try {
            // on this line i need get B4J class to register class on function tables
            LuaValue instance = CoerceJavaToLua.coerce(c.newInstance());
...
to
B4X:
public void RegisterClassToLua(Object c)  {
       
       
        try {
            // on this line i need get B4J class to register class on function tables
            LuaValue instance = CoerceJavaToLua.coerce(c);
...
Providing you initialise the class in the b4j program.
eg
B4X:
dim test as SomeClassOrOther
test.initialize
'then pass test to the RegisterClassToLua
...
 
Upvote 0

Waldemar Lima

Well-Known Member
Licensed User
Longtime User
this works Daestrum but anyway lua dont recognition test class functions

code of test class >
test:
Sub Class_Globals
    Private fx As JFX
    Public example = "helloWorld";
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize

End Sub

public Sub helloWorld
    
    return "HelloWorld friends !"
    
End Sub

this screenshot shows that the function is returning null
b4j.png

this is full source code in JAVA

luaj.java:
package anywheresoftware.b4j.luaj;

import org.luaj.vm2.*;
import org.luaj.vm2.lib.ZeroArgFunction;
import org.luaj.vm2.lib.jse.CoerceJavaToLua;
import org.luaj.vm2.lib.jse.JsePlatform;

import anywheresoftware.b4a.BA.ShortName;
import anywheresoftware.b4a.debug.*;
import java.io.*;
import    java.nio.ByteBuffer;
import java.util.Collections;

@ShortName ("LuaJ")
public class Luaj {
    
    Globals globals = JsePlatform.standardGlobals();

    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";
        }
    }
    
    public String LoadLua(String script){

        
        // create an environment to run in
        //Globals globals = JsePlatform.standardGlobals();
        
        LuaValue instance = CoerceJavaToLua.coerce(new MyClass());
        globals.set("obj", instance);
        
        // Use the convenience function on Globals to load a chunk.
        LuaValue chunk = globals.load(script);
        
        // Use any of the "call()" or "invoke()" functions directly on the chunk.
        chunk.call();
        
        return "Sucess";
    }
    
    public void RegisterClassToLua(Object c,String className) throws IllegalAccessException  {
        
        LuaValue instance = CoerceJavaToLua.coerce(c);
        globals.set(className, instance);
        // Use the convenience function on Globals to load a chunk.
        //LuaValue chunk = globals.load(script);
        
    }
    
    
}
 

Attachments

  • LuaJ-source-and-compiled_libs.zip
    7.9 KB · Views: 244
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
Don't forget all subs names have a leading underscore and are lowercase

B4X:
' in class test
Sub HelloWorld
...
End Sub

You would access this method in java as "test._helloworld"
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
Have a look through the attached file, it's calling a method in a class in b4j.
There's probably better ways to do it, but my brain is tired but it should give you some pointers.
 

Attachments

  • inline lua.zip
    2.1 KB · Views: 246
Upvote 0

Waldemar Lima

Well-Known Member
Licensed User
Longtime User
Have a look through the attached file, it's calling a method in a class in b4j.
There's probably better ways to do it, but my brain is tired but it should give you some pointers.
this works fine !!
thanks .

but on this function , I will need to call all functions at: chunk.call ("myfunction ()"); ?? >
B4X:
 public void RegisterClassToLua(Object c,String className,String script) throws IllegalAccessException  {
        
        LuaValue instance = CoerceJavaToLua.coerce(c);
        globals.set(className, instance);
        // Use the convenience function on Globals to load a chunk.
        LuaValue chunk = globals.load(script);
        chunk.call("test()");
    }
 
Upvote 0
Top