jballerina.java
ballerina/jballerina.java Ballerina library
Module overview
This module provides library operations required for Java interoperability in Ballerina. It includes a set of Ballerina annotations with which Java constructors, methods, and fields can provide implementations of Ballerina functions with external function bodies.
For information on the operations, which you can perform with the Java module, see the following learn page.
Functions
cast
Performs a Java cast operation on a value.
This casts a value describing a JObject
to a type describing a JObject
based on Java assignability,
returns an error if the cast cannot be done.
FileInputStream|error obj = java:cast(inputStream);
Parameters
- value JObject - A value describing a
JObject
which is to be casted
- t typedesc<JObject> (default <>) - A type describing a
JObject
to which thevalue
is to be casted
Return Type
- t|error - A value belonging to parameter
t
or an error if this cast cannot be done
createNull
function createNull() returns handle
Returns a handle
, which refers to Java null.
handle nullHandle = java:createNull();
Return Type
- handle - The
handle
, which refers to Java null
fromString
function fromString(string value) returns handle
Returns a handle
, which refers to the Java string representation of the Ballerina string
.
handle header = java:fromString("Content-Type");
Parameters
- value string - The Ballerina
string
with which thehandle
is created
Return Type
- handle - The
handle
, which refers to the Java String representation of the Ballerinastring
getClass
Returns a handle
, which refers to the Java Class object associated with the class or interface with the given
string name.
handle|error intClass = java:getClass("int");
This function performs a Java class.forName(name)
except for the following cases:
Name | Output |
---|---|
boolean | Java Class instance representing the primitive type boolean |
byte | Java Class instance representing the primitive type byte |
char | Java Class instance representing the primitive type char |
short | Java Class instance representing the primitive type short |
int | Java Class instance representing the primitive type int |
long | Java Class instance representing the primitive type long |
float | Java Class instance representing the primitive type float |
double | Java Class instance representing the primitive type double |
Parameters
- name string - The name of the Java class
Return Type
- handle|error - The Java Class object for the class with the given name
isNull
function isNull(handle value) returns boolean
Returns true
if this handle refers to Java null.
boolean status = java:isNull(value);
Parameters
- value handle - The
handle
of which the referred value is to be tested with Java null
Return Type
- boolean -
true
if this handle refers to Java null
toString
function toString(handle value) returns string?
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.
string? version = java:toString(versionProperty);
Parameters
- value handle - The
handle
of which the referred value is to be converted to a Ballerinastring
Return Type
- string? - The Ballerina
string
representation of the Java object referred by thehandle
or else returns()
if thehandle
refers to Java null
Classes
jballerina.java: SpanImpl
Implementation for the regexp.Span
.
substring
function substring() returns string
Fields
- startIndex int - Callable name
- endIndex int - Module name
jballerina.java: StackFrameImpl
Implementation for the runtime.StackFrame
.
toString
function toString() returns string
Returns a string representing for the StackFrame
.
Return Type
- string - A stack frame as string
Fields
- callableName string - Callable name
- moduleName string? - Module name
- fileName string - File name
- lineNumber int - Line number
Annotations
jballerina.java: Binding
Describes the Java class representing a Ballerina binding.
@java:Binding { 'class: "java.io.File" }
jballerina.java: Constructor
Describes a Java constructor, which provides an implementation of a Ballerina function of which the 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.
function newJavaLinkedList() returns handle = @java:Constructor { 'class: "java.util.LinkedList" } external;
jballerina.java: FieldGet
Describes a Java Field access, which provides an implementation of a Ballerina function of which the body is marked as
external
.
function getError() returns handle = @java:FieldGet { name:"err", 'class:"java/lang/System" } external;
jballerina.java: FieldSet
Describes a Java Field mutate, which provides an implementation of a Ballerina function of which the body is marked as
external
.
function setContractId(handle contractId) = @java:FieldSet { name:"contractId", 'class:"org/lang/impl/JavaFieldAccessMutate" } external;
jballerina.java: Method
Describes a Java method, which provides an implementation of a Ballerina function of which the 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.
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.
Records
jballerina.java: ArrayType
Represents a Java array type. It is used to specify the parameter types in the java:Constructor
and java:Method
annotations.
Fields
- 'class Class -
- dimensions byte - Dimensions of the array type
jballerina.java: ConstructorData
Describes a Java constructor. If the paramTypes
field is not specified, then the parameter types are inferred from
the corresponding Ballerina function.
Fields
- 'class Class -
jballerina.java: FieldData
Describes a Java field.
Fields
- name Identifier - An optional field, which describes the name of the Java field. If this field is not provided, then the name is inferred from the Ballerina function name
- 'class Class -
jballerina.java: MethodData
Describes a Java method. If the paramTypes
field is not specified, then the parameter types are inferred from the
corresponding Ballerina function.
Fields
- name Identifier? - An optional field, which describes the name of the Java method. If this field is not provided, then the name is inferred from the Ballerina function name
- 'class Class -
jballerina.java: ObjectData
Describes a Java class that corresponds to a Ballerina object.
Fields
- 'class Class -
Errors
Object types
jballerina.java: JObject
The Ballerina abstract object which is to be extended by Ballerina objects representing Ballerina bindings for Java classes.
Fields
- jObj handle - The
handle
reference to the corresponding Java object.