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.

getClass

Returns a handle that refers to the Java Class object associated with the class or interface with the given string name.

This function performs a Java class.forName(name) except for following cases:

name output boolean the Java Class instance representing the Java primitive type boolean byte the Java Class instance representing the Java primitive type byte char the Java Class instance representing the Java primitive type char short the Java Class instance representing the Java primitive type short int the Java Class instance representing the Java primitive type int long the Java Class instance representing the Java primitive type long float the Java Class instance representing the Java primitive type float double the Java Class instance representing the Java primitive type double

isNull

Returns true if this 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.