Andreas Rozek      

LuaJava_08 - will Java types be preserved?

In a Java environment, Lua scripts may have to store Java objects for a while in order to pass them back to Java when necessary. LuaJava_08 checks if type (and content) of such Java objects is preserved within Lua.

Please, also consider my "Hints for Reading" and the "List of Recent Changes"!
(Problems displaying this page? Ugly graphics? Please, click here)

LuaJava_08

The script itself is extremely simple and should not require any further explanation. The accompanying Java source may be compiled using
 

  javac LuaJava_08.java

and does not need the luajava package. The resulting class file should be copied to a place where it can be found by the Java class loader (e.g., into a directory which is automatically scanned by the Java extension mechanism).

After an invocation of the form
 

  java luna.LuaJava LuaJava_08.lua

the script produces the following output

LuaJava_08 - will Java types be preserved while stored within Lua?

  java.lang.Boolean(true) -> java.lang.Boolean(true)
  java.lang.String('(just a test)') -> java.lang.String((just a test))
  java.util.Hashtable() -> java.util.Hashtable({})
  java.io.File('Test.dat') -> java.io.File(Test.dat)


  Lua got an object from Java as "boolean"
  Java got java.lang.Boolean from Lua (equal to original object)

  Lua got a java.lang.Character from Java as "userdata"
  Java got java.lang.Character from Lua (identical with original object)

  Lua got an object from Java as "number"
  Java got java.lang.Integer from Lua not equal to original object

  Lua got an object from Java as "number"
  Java got java.lang.Integer from Lua not equal to original object

  Lua got an object from Java as "number"
  Java got java.lang.Integer from Lua (equal to original object)

  Lua got an object from Java as "number"
  Java got java.lang.Integer from Lua not equal to original object

  Lua got an object from Java as "number"
  Java got java.lang.Integer from Lua not equal to original object

  Lua got an object from Java as "number"
  Java got java.lang.Integer from Lua not equal to original object

  Lua got an object from Java as "string"
  Java got java.lang.String from Lua (equal to original object)

  Lua got a java.util.Hashtable from Java as "userdata"
  Java got java.util.Hashtable from Lua (identical with original object)

  Lua got a java.io.File from Java as "userdata"
  Java got java.io.File from Lua (identical with original object)

which is worth to be analyzed (look at the coloured lines):

  • whenever LuaJava is able to convert a Java object to a Lua type, it will do so;
  • since Lua numbers will be passed back to Java as "Integer" objects, the numeric value returned will often be completely different from the original;
  • Java objects which cannot be automatically converted to a Lua type will be left untouched (which is probably what one expected).

Hopefully, the "Integer" problem will soon be corrected.

Source Code

The source code of this script and its accompanying Java class is publically available:

Disclaimer

Please, also consider the author's Disclaimer!


http://www.Andreas-Rozek.de/LuaJava/Acquainting/LuaJava_08_en.html   (last Modification: 26.11.2004)