Module : java

Module overview

This module provides the API for Java interoperability in Ballerina. It includes a set of Ballerina annotations with which Java constructors, methods and fields can provide implementations to Ballerina functions with external function bodies.

Records

ArrayType ArrayType represents a Java array type. It is used to specify parameter types in `Constructor` and `Method` annotations.
ConstructorData The `ConstructorData` type describes a Java constructor. If the `paramTypes` field is not specified, then parameter types are inferred from the corresponding Ballerina function.
FieldData The `FieldData` type describes a Java field.
MethodData The `MethodData` type describes a Java method. If the `paramTypes` field is not specified, then parameter types are inferred from the corresponding Ballerina function.

Functions

createNull

Returns a handle that refers to Java null.

fromString

Returns a handle that refers to the Java string representation of the Ballerina string.

getArrayElement

Returns a handle that refers to the element at the specified index in the given Java array.

This function panics with a JavaNullReferenceError error if the handle refers to Java null.

getArrayLength

Returns the length of the given Java array.

This function panics with a JavaNullReferenceError error if the handle refers to Java null.

isNull

Returns true if this handle refers to Java null.

setArrayElement

Replaces the element at the specified index in the given Java array with the specified element.

This function panics with a JavaNullReferenceError error if the handle refers to Java null.

toString

Returns a Ballerina string representation of the Java object referred by the handle.

If the handle refers to Java null, then this function returns a nil value.

Annotations

Constructor

The Constructor annotation describes a Java constructor that provides an implementation of a Ballerina function whose body is marked as external. If the Ballerina function body is marked as external, it means that the implementation of the function is not provided in the Ballerina source module.

The following code snippet shows an example usage of this annotation. Here, the newJavaLinkedList Ballerina function's implementation is provided by the default constructor of the java.util.LinkedList class.

 import ballerinax/java;

 function newJavaLinkedList() returns handle = @java:Constructor {
      class: "java.util.LinkedList"
 } external;
FieldGet

The FieldGet annotation describes a Java Field access that provides an implementation of a Ballerina function whose body is marked as external.

FieldSet

The FieldSet annotation describes a Java Field mutate that provides an implementation of a Ballerina function whose body is marked as external.

Method

The Method annotation describes a Java method that provides an implementation of a Ballerina function whose body is marked as external. If the Ballerina function body is marked as external, it means that the implementation of the function is not provided in the Ballerina source module.

The following code snippet shows an example usage of this annotation. Here, the getUUID Ballerina function's implementation is provided by the java.util.UUID.randomUUID static method.

 import ballerinax/java;

 function getUUID() returns handle = @java:Method {
     name: "randomUUID",
     class: "java.util.UUID"
 } external;

The name field is optional. If it is not provided, the name of the Java method is inferred from the Ballerina function.