These sections document older and less convenient ways to call Java methods, access Java fields, and use Java arrays.
The following lower-level primitives require you to specify
the parameter and return types explicitly.
You should probably use the functions invoke
and invoke-static
(see Method operations) instead.
Syntax: primitive-constructor
class
(argtype
...
)
Returns a new anonymous procedure, which when called will create a new object of the specified class, and will then call the constructor matching the specified argument types.
Syntax: primitive-virtual-method
class
method
rtype
(argtype
...
)
Returns a new anonymous procedure, which when called will invoke the instance method whose name is the string
method
in the class whose name isclass
.
Syntax: primitive-static-method
class
method
rtype
(argtype
...
)
Returns a new anonymous procedure, which when called will invoke the static method whose name is the string
method
in the class whose name isclass
.
Syntax: primitive-interface-method
interface
method
rtype
(argtype
...
)
Returns a new anonymous procedure, which when called will invoke the matching method from the interface whose name is
interface
.
The macros return procedure values, just like lambda
.
If the macros are used directly as the procedure of a procedure call,
then kawa can inline the correct bytecodes to call the specified methods.
(Note also that neither macro
checks that there really is a method that matches the specification.)
Otherwise, the Java reflection facility is used.
The following macros evaluate to procedures that can be used to access or change the fields of objects or static fields. The compiler can inline each to a single bytecode instruction (not counting type conversion).
These macros are deprecated.
The fields
and static-field
functions
(see Field operations) are easier to use, more powerful, and
just as efficient. However, the high-level functions currently do
not provide access to non-public fields.
Syntax: primitive-get-field
class
fname
ftype
Use this to access a field named
fname
having typetype
in classclass
. Evaluates to a new one-argument procedure, whose argument is a reference to an object of the specifiedclass
. Calling that procedure returns the value of the specified field.
Syntax: primitive-set-field
class
fname
ftype
Use this to change a field named
fname
having typetype
in classclass
. Evaluates to a new two-argument procedure, whose first argument is a reference to an object of the specifiedclass
, and the second argument is the new value. Calling that procedure sets the field to the specified value. (This macro’s name does not end in a ‘!
’, because it does not actually set the field. Rather, it returns a function for setting the field.)
Syntax: primitive-get-static
class
fname
ftype
Like
primitive-get-field
, but used to access static fields. Returns a zero-argument function, which when called returns the value of the static field.
Syntax: primitive-set-static
class
fname
ftype
Like
primitive-set-field
, but used to modify static fields. Returns a one-argument function, which when called sets the value of the static field to the argument.
The following macros evaluate to procedures that can be used to manipulate primitive Java array objects. The compiler can inline each to a single bytecode instruction (not counting type conversion).
Syntax: primitive-array-new
element-type
Evaluates to a one-argument procedure. Applying the resulting procedure to an integer count allocates a new Java array of the specified length, and whose elements have type
element-type
.
Syntax: primitive-array-set
element-type
Evaluates to a three-argument procedure. The first argument of the resulting procedure must be an array whose elements have type
element-type
; the second argument is an index; and the third argument is a value (coercible toelement-type
) which replaces the value specified by the index in the given array.
Syntax: primitive-array-get
element-type
Evaluates to a two-argument procedure. The first argument of the resulting procedure must be an array whose elements have type
element-type
; the second argument is an index. Applying the procedure returns the element at the specified index.
Syntax: primitive-array-length
element-type
Evaluates to a one-argument procedure. The argument of the resulting procedure must be an array whose elements have type
element-type
. Applying the procedure returns the length of the array. (Alternatively, you can use(field
.)array
'length)