Java Interoperability in Ballerina
Irushi Liyanage
@IrushiL
About Ballerina Community Call
● Monthly meeting
● Community call structure
○ Feature explanation + demonstration
○ Open discussion
● Useful links
○ Agenda
○ Notify me form
○ Topic suggestion form
Agenda
● Update on Ballerina Swan Lake releases
● Java Interoperability in Ballerina
○ Calling Java code from Ballerina
○ Generating bindings using the Bindgen Tool
○ Packaging Java Libraries with Ballerina Programs
● Open discussion
Update on Ballerina Swan Lake
Ballerina Releases
2020
Dec
jBallerina
1.0
2019
Sep
2019
Dec
jBallerina
1.1
2020
Mar
jBallerina
1.2
jBallerina
1.2.11
2020 June
Swan Lake
Preview 1
Swan Lake
Preview 7
Ballerina Swan Lake Previews
● Swan Lake - Preview 8 (Mid December, 2020)
○ Introducing new service design
○ Improved HTTP, Websocket, gRPC and more services
○ New GraphQL Listener
● Swan Lake - Alpha (Mid January, 2021)
○ All major features will be ready
New Features in Swan Lake Alpha
● Language Features
○ New service support
○ Immunitibility support
○ Type system improvements
○ Improved error handling
○ New transaction support
○ Enhanced query expression support
○ Raw template support
○ Configurability support and more
New Features in Swan Lake Alpha
● Standard Libraries
○ New services such as HTTP, Websocket, and gRPC etc.
○ Support for GraphQL services
○ New IO, Log API, SQL modules and more
New Features in Swan Lake Alpha
● Ballerina Platform
○ Introduce Ballerina packages
○ New Code2Cloud extension
○ Ballerina Parser
○ Compiler APIs
○ Ballerina Shell (experimental) and more
Calling Java Code from Ballerina
Using Java Code in Ballerina
FFI in jBallerina
● External function annotations:
○ @java:Method
○ @java:Constructor
○ @java:FieldGet
○ @java:FieldSet
● Handle type - a reference to an externally-managed storage
Java Types to Ballerina Types
Java Type Ballerina Type
Any reference type including “null type” handle
boolean boolean
byte byte, int, float
short int, float
char int, float
int int, float
long int, float
float float
double float
Ballerina Types to Java Types
Ballerina type Java type
handle Any reference type
boolean boolean
byte byte, short, char, int, long, float, double
int byte, char, short, int, long
float byte, char, short, int, long, float, double
FFI in jBallerina
● Static method invocation
● Instance method invocation
● Constructor invocation
function xyz(handle a) returns handle = @java:Constructor {
class: "a.b.c.Foo",
paramTypes: ["java.lang.String"]
} external;
function abc(handle a, int b) returns handle = @java:Method {
name: "bar",
class: "a.b.c.Foo",
paramTypes: ["java.util.UUID", "int"]
} external;
function cdf(handle receiver, int a) returns handle = @java:Method
{
name: "poll",
class: "a.b.c.Foo",
paramTypes: ["int"]
} external;
FFI in jBallerina
● Instance field getter
● Instance field setter
● Static field getter
function getENTER() returns int = @java:FieldGet {
name: "ENTER",
class: "a.b.c.Foo"
} external;
function getId(handle receiver) returns int = @java:FieldGet
{
name: "id",
class: "a.b.c.Foo"
} external;
function setId(handle receiver, int arg) = @java:FieldSet {
name: "id",
class: "a.b.c.Foo"
} external;
Using the Bindgen Tool
Bindgen CLI Command
A CLI tool for generating Ballerina bridge code for Java APIs
ballerina bindgen [(-cp|--classpath) <classpath>...]
[(-mvn|--maven) <groupId>:<artifactId>:<version>]
[(-o|--output) <output>]
[--public]
(<class-name>...)
Java to Ballerina Mappings
● Java class -> Ballerina object
● Java instance method -> Ballerina object method
● Java instance field -> Ballerina object methods for a getter and a setter
● Java constructor -> Ballerina function
● Java static method -> Ballerina function
● Java static field -> Ballerina functions for a getter and a setter
Java to Ballerina Type Mappings
● java.lang.String object -> Ballerina string type
● java.lang.Throwable object -> Ballerina error type
Java Subtyping
Java Casting
Packaging Java Libraries with Ballerina Programs
Ballerina.toml
[platform]
target = "java8"
[[platform.libraries]]
path = "./javalibs/mysql-connector-java-<version>.jar"
modules = ["ordermgt"]
[[platform.libraries]]
modules = ["load_yaml"]
groupId = "org.yaml"
artifactId = "snakeyaml"
version = "1.25"
target/platform-libs
References
● https://ballerina.io/learn/calling-java-code-from-ballerina/
● https://ballerina.io/learn/by-example/invoke-java-methods.html
● https://ballerina.io/learn/api-docs/ballerina/java/
● https://docs.oracle.com/javase/8/docs/api/
● https://bitbucket.org/asomov/snakeyaml/wiki/Documentation
Open Discussion

[Ballerina Community Call] Java Interoperability

  • 1.
    Java Interoperability inBallerina Irushi Liyanage @IrushiL
  • 2.
    About Ballerina CommunityCall ● Monthly meeting ● Community call structure ○ Feature explanation + demonstration ○ Open discussion ● Useful links ○ Agenda ○ Notify me form ○ Topic suggestion form
  • 3.
    Agenda ● Update onBallerina Swan Lake releases ● Java Interoperability in Ballerina ○ Calling Java code from Ballerina ○ Generating bindings using the Bindgen Tool ○ Packaging Java Libraries with Ballerina Programs ● Open discussion
  • 4.
  • 5.
  • 6.
    Ballerina Swan LakePreviews ● Swan Lake - Preview 8 (Mid December, 2020) ○ Introducing new service design ○ Improved HTTP, Websocket, gRPC and more services ○ New GraphQL Listener ● Swan Lake - Alpha (Mid January, 2021) ○ All major features will be ready
  • 7.
    New Features inSwan Lake Alpha ● Language Features ○ New service support ○ Immunitibility support ○ Type system improvements ○ Improved error handling ○ New transaction support ○ Enhanced query expression support ○ Raw template support ○ Configurability support and more
  • 8.
    New Features inSwan Lake Alpha ● Standard Libraries ○ New services such as HTTP, Websocket, and gRPC etc. ○ Support for GraphQL services ○ New IO, Log API, SQL modules and more
  • 9.
    New Features inSwan Lake Alpha ● Ballerina Platform ○ Introduce Ballerina packages ○ New Code2Cloud extension ○ Ballerina Parser ○ Compiler APIs ○ Ballerina Shell (experimental) and more
  • 10.
    Calling Java Codefrom Ballerina
  • 11.
    Using Java Codein Ballerina
  • 12.
    FFI in jBallerina ●External function annotations: ○ @java:Method ○ @java:Constructor ○ @java:FieldGet ○ @java:FieldSet ● Handle type - a reference to an externally-managed storage
  • 13.
    Java Types toBallerina Types Java Type Ballerina Type Any reference type including “null type” handle boolean boolean byte byte, int, float short int, float char int, float int int, float long int, float float float double float
  • 14.
    Ballerina Types toJava Types Ballerina type Java type handle Any reference type boolean boolean byte byte, short, char, int, long, float, double int byte, char, short, int, long float byte, char, short, int, long, float, double
  • 15.
    FFI in jBallerina ●Static method invocation ● Instance method invocation ● Constructor invocation function xyz(handle a) returns handle = @java:Constructor { class: "a.b.c.Foo", paramTypes: ["java.lang.String"] } external; function abc(handle a, int b) returns handle = @java:Method { name: "bar", class: "a.b.c.Foo", paramTypes: ["java.util.UUID", "int"] } external; function cdf(handle receiver, int a) returns handle = @java:Method { name: "poll", class: "a.b.c.Foo", paramTypes: ["int"] } external;
  • 16.
    FFI in jBallerina ●Instance field getter ● Instance field setter ● Static field getter function getENTER() returns int = @java:FieldGet { name: "ENTER", class: "a.b.c.Foo" } external; function getId(handle receiver) returns int = @java:FieldGet { name: "id", class: "a.b.c.Foo" } external; function setId(handle receiver, int arg) = @java:FieldSet { name: "id", class: "a.b.c.Foo" } external;
  • 17.
  • 18.
    Bindgen CLI Command ACLI tool for generating Ballerina bridge code for Java APIs ballerina bindgen [(-cp|--classpath) <classpath>...] [(-mvn|--maven) <groupId>:<artifactId>:<version>] [(-o|--output) <output>] [--public] (<class-name>...)
  • 19.
    Java to BallerinaMappings ● Java class -> Ballerina object ● Java instance method -> Ballerina object method ● Java instance field -> Ballerina object methods for a getter and a setter ● Java constructor -> Ballerina function ● Java static method -> Ballerina function ● Java static field -> Ballerina functions for a getter and a setter
  • 20.
    Java to BallerinaType Mappings ● java.lang.String object -> Ballerina string type ● java.lang.Throwable object -> Ballerina error type Java Subtyping Java Casting
  • 21.
    Packaging Java Librarieswith Ballerina Programs
  • 22.
    Ballerina.toml [platform] target = "java8" [[platform.libraries]] path= "./javalibs/mysql-connector-java-<version>.jar" modules = ["ordermgt"] [[platform.libraries]] modules = ["load_yaml"] groupId = "org.yaml" artifactId = "snakeyaml" version = "1.25" target/platform-libs
  • 23.
    References ● https://ballerina.io/learn/calling-java-code-from-ballerina/ ● https://ballerina.io/learn/by-example/invoke-java-methods.html ●https://ballerina.io/learn/api-docs/ballerina/java/ ● https://docs.oracle.com/javase/8/docs/api/ ● https://bitbucket.org/asomov/snakeyaml/wiki/Documentation
  • 24.