Andreas Rozek      

LuaJava_03 - accessing fields and methods of Java objects

One of the two basic functionalities provided by LuaJava is the capability to bind Java classes, instantiate Java objects and to access their "fields" or invoke their methods, respectively. LuaJava_03 tests this functionality.

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

LuaJava_03

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

  javac LuaJava_03.java

and does not even 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_03.lua

the script produces the following output:

LuaJava_03 - first experiments with LuaJava 1.0b3 and Lua 5.0

JavaClass:
  type(...) = "userdata"
  value     = "class LuaJava_03" (tostring() yields "userdata: 006D83F0")

  class constants (read access):
    BooleanClassConstant = true
    ByteClassConstant    = 123
    ShortClassConstant   = 12345
    IntClassConstant     = 1234567
    LongClassConstant    = 123456789
    FloatClassConstant   = 1.2000000243434e+034
    DoubleClassConstant  = 1.2345e+071
    CharClassConstant    = userdata: 006DBA00
    StringClassConstant  = StringClassConstant

  class variables (read access):
    BooleanClassVariable = true
    ByteClassVariable    = 123
    ShortClassVariable   = 12345
    IntClassVariable     = 1234567
    LongClassVariable    = 123456789
    FloatClassVariable   = 1.2000000243434e+034
    DoubleClassVariable  = 1.2345e+071
    CharClassVariable    = userdata: 006D89A0
    StringClassVariable  = StringClassVariable

  class variables (write access):
    impossible, throws "attempt to index local `JavaClass' (a userdata value)"

  class methods (invocation):
    BooleanClassMethod(true)   -> true
    ByteClassMethod(123)       -> 123
    ShortClassMethod(12345)    -> 12345
    IntClassMethod(1234567)    -> 1234567
    LongClassMethod(123456789) -> 123456789
    FloatClassMethod(123.45)   -> 123.44999694824
    DoubleClassMethod(123.45)  -> 123.45
    CharClassMethod('a')       -> impossible, throws "Invalid method call. No such
                                  method."
    StringClassMethod('Test')  -> Test

JavaInstance:
  type(...) = "userdata"
  value     = "LuaJava_03 instance" (tostring() yields "userdata: 006DBB30")

  instance variables (read access):
    BooleanVariable = true
    ByteVariable    = 123
    ShortVariable   = 12345
    IntVariable     = 1234567
    LongVariable    = 123456789
    FloatVariable   = 1.2000000243434e+034
    DoubleVariable  = 1.2345e+071
    CharVariable    = userdata: 006C5700
    StringVariable  = (just for testing)

  instance variables (write access):
    impossible, throws "attempt to index local `JavaInstance' (a userdata value)"

  instance methods (invocation):
    BooleanMethod(true)   -> true
    ByteMethod(123)       -> 123
    ShortMethod(12345)    -> 12345
    IntMethod(1234567)    -> 1234567
    LongMethod(123456789) -> 123456789
    FloatMethod(123.45)   -> 123.44999694824
    DoubleMethod(123.45)  -> 123.45
    CharMethod('a')       -> impossible, throws "Invalid method call. No such
                             method."
    StringMethod('Test')  -> Test

(Two lines have been wrapped manually in order to fit onto this web page)

From the source code (of the Lua script) and its output the following lessons are learned:

  • it is possible to read the contents of constant or variable class or instance fields - but it is impossible to write the (variable) fields of neither a Java class nor a Java instance;

  • Java 32-bit floating point numbers seem to be supported within the range (and with the precision) given by their IEEE 32-bit encoding;

  • Java characters are not supported: within Lua, a Java character "looks" like a "userdata" value, and when trying to invoke a method which expects a character argument, this method is not found from within Lua;

  • Java objects (i.e., not primitive values) must not be printed directly (from within Lua) - you will have to use the Java method "String.valueOf(...)" first in order to convert an arbitrary Java object into a (Java) string which may then be printed from within Lua;

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_03_en.html   (last Modification: 25.11.2004)