| Andreas Rozek | |
|
|
|
LuaJava_03 - accessing fields and methods of Java objectsOne 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"! |
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:
|
Source CodeThe source code of this script and its accompanying Java class is publically available: |
DisclaimerPlease, also consider the author's Disclaimer! |
|
|
|
| http://www.Andreas-Rozek.de/LuaJava/Acquainting/LuaJava_03_en.html | (last Modification: 25.11.2004) |