1

When using LuaJ, I was tring to call a java method that needs a Byte argument. But when I use luajava.newInstance("java.lang.Byte","2") It turned out to be a java.lang.Integer and caused an error.

java.lang.ClassCastException: class java.lang.Integer cannot be cast to class java.lang.Byte (java.lang.Integer and java.lang.Byte are in module java.base of loader 'bootstrap')

I can't modify the java method to do the cast. How can I get a Byte instance in lua?

Edit: The method is in minecraft sourcecode and I cant find the implementation. So I write a method to test.

java:

    public static String GetType(Object obj){
        return  obj.getClass().toString();
    }

lua:

print(MyJavaClass:GetType(luajava.newInstance("java.lang.Byte","2")))

And it is "class java.lang.Integer"

etwxr9
  • 11
  • 2
  • 3
    You'll have to show the code where the result of that `newInstance()` call is used. Can't tell if that method returns the wrong type, or your code doing something to cause integer type promotion, which is easy to do. – erickson Aug 11 '21 at 15:38
  • 1
    I edited the post and added a test result. – etwxr9 Aug 11 '21 at 15:59
  • I can't answer because the question is currently closed, but looking at [the source,](https://github.com/luaj/luaj/blob/daf3da94e3cdba0ac6a289148d7e38bd53d3fe64/src/jse/org/luaj/vm2/lib/jse/CoerceJavaToLua.java#L148) I see that `Byte` is always coerced to `LuaInteger`. It doesn't look like this Lua runtime supports an 8-bit `Byte`. The context here is still not clear; it sounds like you are invoking Java methods from Lua. Is that right? If Lua doesn't support byte, maybe you could create `byte[]` and pass zeroth element, or use Java reflection (script via Lua) to instantiate `Byte`. – erickson Aug 11 '21 at 16:52
  • Yes I am using lua to invoke java method and I had said it is LuaJ in the title. And I did another test. I created a Byte object in java and passed it to lua and it became lua number instead of userdata. – etwxr9 Aug 12 '21 at 01:36
  • And I dont know how to create byte[] in lua. And cant not instantiate Byte because I cant get the constructor of Byte using Class.getConstructor(String.class). Lua dont support that ".class" – etwxr9 Aug 12 '21 at 02:26
  • How about `"".getClass()`? I guess that might give a `LuaString`? – erickson Aug 12 '21 at 03:04
  • 1
    Solved it by modify luaj source code. Although this is not what I want. But seems it is the only way since luaj will always coerce Byte to Integer as long as i use lua. – etwxr9 Aug 12 '21 at 03:12
  • "" is a luastring so there is not getClass function, it will throw nil error. And type("") returns "string" – etwxr9 Aug 12 '21 at 03:35
  • It's too bad you had to modify the luaj code, but I don't see a way to make it work as-is. It's really an oversight of the luaj system; they should do better type coercion when invoking Java methods with `LuaValue` arguments. Are you using luaj because you are comfortable with Lua, or because using Java directly is not an option in your context? – erickson Aug 12 '21 at 15:24
  • i am developing a minecraft plugin that allows people to use scripting languages. And yes I'm more familiar with Lua due to experience using unity and some games modding. Although Lua is not so suitable for Java – etwxr9 Aug 13 '21 at 16:45

0 Answers0