cn java_instance
and cn java_extends
. The
former is used to designate instances of exactly the given class
cn
, while the latter is used to designate instances of either the
class cn
or any of its subclasses. In both cases, the type
parameter cn
is used to specify the class, and uses the
fully-qualified name of the class where dots are replaced with single
quotes. As a consequence, the type of Java strings is thus written
java'lang'String java_instance
.
let i = call "java.lang.Integer.parseInt(java.lang.String):int" s
However, it is possible to use simple names rather than qualified
names for classes if their packages have been opened. Initially, only
the java.lang
package is opened. A package can be opened though a
modified open Package'packname
directive; for example, the
javax.awt
package can be opened by writing:
open Package'java'awt
It is also possible to take advantage of opened packages in types, by
replacing the package name with an underscore. The type of Java
strings can thus be written _'String java_instance
.
let i = call "Integer.parseInt(_)" s
Furthermore, it is possible to use dashes to denote any number of
parameters, leading to the lightest code to rotate an integer value:
let j = call "Integer.rotateLeft(-)" i nbits
The compiler will issue an error if there is an ambiguity.
cn java_instance
and cn java_extends
types that are
used to map Java reference types, the compiler maps Java primitive
type to newly-introduced OCaml types that are synonym of OCaml
predefined types. The complete mapping is given by the following
table.
Java type | OCaml type | Synonym |
---|---|---|
boolean |
java_boolean |
bool |
byte |
java_byte |
int |
char |
java_char |
int |
double |
java_double |
float |
float |
java_float |
float |
int |
java_int |
int32 |
long |
java_long |
int64 |
short |
java_short |
int |
void |
java_void |
unit |
Java type | OCaml type | OCaml module |
---|---|---|
boolean[] |
java_boolean java_boolean_array |
JavaBooleanArray |
byte[] |
java_byte java_byte_array |
JavaByteArray |
char[] |
java_char java_char_array |
JavaCharArray |
double[] |
java_double java_double_array |
JavaDoubleArray |
float[] |
java_float java_float_array |
JavaFloatArray |
int[] |
java_int java_int_array |
JavaIntArray |
long[] |
java_long java_long_array |
JavaLongArray |
short[] |
java_short java_short_array |
JavaShortArray |
reference[] |
'a java_reference_array |
JavaReferenceArray |
JavaArray
module.
"C.m(T[])"
is used, then the arguments are passed through a
Java array;"C.m(T...)"
is used, then the arguments are passed through an
OCaml literal array. let l1 =
Java.call "Arrays.asList(Object[])"
(Java.make_array "Object[]" 5l)
let l2 =
Java.call "Arrays.asList(Object...)"
[| Java.null ; Java.make "Object()" () |]
exception Java_exception of java'lang'Exception java_instance
exception Java_error of java'lang'Error java_instance
The former is used for exceptions whose instances inherit from
java.lang.Exception (i.e. checked exceptions), while the
latter is used for exceptions whose instances inherit from
java.lang.Error (i.e. unchecked exceptions).make desc param1 ... paramn
calls the constructor whose descriptor
is desc
with parameters param1 ... paramn
, and returns the
created instance.
desc
is composed of the following elements:(
);)
). let inst = make "java.lang.Object()" ()
Java_exception
if the constructor throws an exceptionJava_error
if the constructor throws an errormake_array desc dim1 ... dimn
builds and returns an array, whose
number of dimensions and type of elements are determined by desc
.
Each element is initialized to the default value for the type (that
is false
for booleans, zero for other primitive types, and null
for reference types).
desc
is composed of the following elements:[]
characters. let arr = make_array "byte[][]" 2l 3l
Java_exception
if a dimension is negativemake_array_dims desc dim1 ... dimn
is similar to Java.make_array
,
except that the array descriptor is made of two kinds of dimension
specifiers, allowing to initialize only the first dimensions of the
array.
desc
is composed of the following elements:[_]
that indicates that the dimension will be allocated;[]
that indicates that the dimension will not be allocated. let arr = make_array_dims "byte[_][]" 2l
Java_exception
if a dimension is negativecall desc param1 ... paramn
calls and returns the result of method
desc
called with parameters param1 ... paramn
, where param1
is
the instance to call method upon if the method is not static.
desc
is composed of the following elements:.
);(
);)
);:
) followed by a type.s1
and s2
:
call "java.lang.String.compareTo(java.lang.String):int" s1 s2
Java_exception
if the method throws an exceptionJava_error
if the method throws an errorJava.call
, but ignores the result if any.Java.call
, returns the instance the method was called upon.get desc obj
retrieves the value of field desc
for instance
obj
. The obj
value should be replaced by ()
if desc
designates a static field.
desc
is composed of the following elements:.
);:
) followed by a type. let max_int = get "java.lang.Integer.MAX_VALUE:int" ()
Java_exception
if obj
is null
set desc obj x
changes the value of field desc
for
instance obj
to x
. The obj
value should not be provided if
desc
designates a static field.
desc
is composed of the following elements:.
);:
) followed by a type.dim
to
zero:
let () = set "java.awt.Dimension.height:int" dim 0l
Java_exception
if obj
is null
iter desc f it
applies f
to every element returned by it
, after
casting them to desc
.
desc
is either a class name, or an array descriptor.Java_exception
if it
is null
fold desc f z it
computes (f ... ((f z it0) it1) ... itn)
where
iti
values are successively returned by it
, and casted to desc
before they are passed to f
.
desc
is either a class name, or an array descriptor.Java_exception
if it
is null
null
value.is_null x
returns true
iff x
is equal to null
.is_not_null x
returns false
iff x
is equal to null
.equal x y
returns true
if x
and y
designate the very same
reference.not_equal x y
returns false
if x
and y
designate the very
same reference.instanceof desc x
returns true
if x
is an instance of desc
.
desc
is either a class name, or an array descriptor.cast desc x
casts x
, so that it can be used as an instance of
desc
.
desc
is either a class name, or an array descriptor.Java_exception
if the cast failsget_class desc
returns the instance of java.lang.Class
representing the passed type descriptor.
desc
can designate any Java type (primitive, array, reference).throw x
raises the instance x
, that will be wrapped into either
a Java_exception
, or a Java_error
on the OCaml side.Java_exception
if x
is an instance of
java.lang.ExceptionJava_error
if x
is an instance of java.lang.Errorsynchronized obj (fun () -> ...)
is equivalent to the Java code
synchronized (obj) { ... }
.Java_exception
if the obj
is null
proxy_loader desc cl impl
returns an instance that implements the
interfaces specified by desc
, using the methods provided by impl
.
The class is defined in the class loader cl
.
desc
is basically a comma-separated list of interface names.
proxy "java.lang.Runnable" (object
method run = ...
end)
When only one interface is provided, the instance returned has this
type, otherwise it has type java.lang.Object.
.methodName
notation in
desc
. As of Java 1.7, only three methods can be overridden:toString
;equals
;hashCode
.toString
can be built using the following code:
proxy_loader "java.lang.Runnable, .toString" loader (object
method run = ...
method toString = ...
end)
Java.proxy_loader
, but uses the system class loader.Java.proxy_loader
, but uses the class loader that was used to
load the OCaml-Java runtime.Java.proxy_system
.wrap x
wraps the reference x
into an option type:Some x
if x
is not null
;None
if x
is null
.unwrap x
unwraps the option x
into a bare reference:Some x
is mapped to x
;None
is mapped to null
.