Top Banner
Java Interop Tako Schotanus, Red Hat
16

Ceylon/Java interop by Tako Schotanus

May 26, 2015

Download

Technology

UnFroMage

Discover how Ceylon and Java work together.
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Ceylon/Java interop by Tako Schotanus

Java InteropTako Schotanus, Red Hat

Page 2: Ceylon/Java interop by Tako Schotanus

Geecon: An Introduction to Ceylon

Combined sources

public class JavaClass {

int count(String name) { ... }

}

shared void run() {

value jc = JavaClass();

print( jc.count("thing") );

}

Page 3: Ceylon/Java interop by Tako Schotanus

Geecon: An Introduction to Ceylon

Legacy code

• Maven

module “helloworld” 1.0 {

import org.hibernate.core “4.2.1.Final”;

import com.example.myjar “3.0”;

}

ceylon compile --rep aether helloworld/1.0

• Import-jarceylon import-jar --jar lib/myjar-3.0 com.example.myjar/3.0

Page 4: Ceylon/Java interop by Tako Schotanus

Geecon: An Introduction to Ceylon

Primitive types

• Integer <-> long, int, short, byte

• Float <-> double, float

• Boolean <-> boolean

• Character --> int, char

• String <-> j.l.String

• No Byte type (might come in future?)

• Boxing with generics

Page 5: Ceylon/Java interop by Tako Schotanus

Geecon: An Introduction to Ceylon

Special name handling

print(\iCONSTANT);

value v = \Isome_class()

import com.example {

constant = CONSTANT,

SomeClass = some_class }

print(constant);

value v = SomeClass()

Page 6: Ceylon/Java interop by Tako Schotanus

Geecon: An Introduction to Ceylon

Null handling

public class JavaClass {

int foo(int param) { ... }

Integer bar(Integer param) { return null; }

}

Integer i1 = jc.foo(5);

Integer i2 = jc.foo(null); // Error

Integer? i3 = jc.bar(null);

Integer i4 = jc.bar(5); // Compiles Ok, runtime error

Page 7: Ceylon/Java interop by Tako Schotanus

Geecon: An Introduction to Ceylon

Properties

public class JavaClass {

int getFoo() { ... }

int getBar() { ... }

void setBar(int bar) { ... }

void setBaz(int baz) { ... }

}

shared class ClassClass() {

Integer foo;

variable Integer bar;

void setBaz(Integer baz) { ... }

}

Page 8: Ceylon/Java interop by Tako Schotanus

Geecon: An Introduction to Ceylon

Properties (contd.)

public class JavaClass {

int getFoo() { return 0; }

int getBar() { return b; }

void setBar(int bar) { ... }

void setBaz(int baz) { ... }

}

shared class ClassClass() {

Integer foo { return 0; }

Integer bar { return b; }

assign bar { ... }

void setBaz(Integer baz) { ... }

}

Page 9: Ceylon/Java interop by Tako Schotanus

Geecon: An Introduction to Ceylon

Overloading

public class JavaClass {

void foo(int param) { ... }

void foo(String param) { ... }

}

jc.foo(5);

jc.foo(“bar”);

Ceylon doesn't have overloading but does support it for Java where possible

Page 10: Ceylon/Java interop by Tako Schotanus

Geecon: An Introduction to Ceylon

Overloading (contd.)

public class JavaClass {

void foo(long param) { ... }

void foo(short param) { ... }

}

import java.lang { Short }

jc.foo(5);

jc.foo(Short(5).shortValue());

Page 11: Ceylon/Java interop by Tako Schotanus

Geecon: An Introduction to Ceylon

JDK Modules (JigSaw)

• JDK divided in JigSaw compatible modules

• Always available but must be explicitly imported

module “helloworld” 1.0 {

import java.base “7”;

import javax.xml “7”;

}

import java.lang { Short }

import javax.xml { ... }

Page 12: Ceylon/Java interop by Tako Schotanus

Geecon: An Introduction to Ceylon

Arrays

• BooleanArray --> boolean[ ]

• IntArray --> int[ ]

• ByteArray, CharArray, etc

• ShortArray(...).array --> Array<j.l.Short>

• Array<T>

Page 13: Ceylon/Java interop by Tako Schotanus

Geecon: An Introduction to Ceylon

Annotations

import javax.annotation { postConstruct }

postConstruct shared void reset() {

// Clean up after ourselves

this.count = 0;

this.userNumber = 0;

}

• Integration with existing frameworks that use annotations possible

Page 14: Ceylon/Java interop by Tako Schotanus

Geecon: An Introduction to Ceylon

ceylon.interop.java

• Part of the SDK

• Useful functions like– javaClassFromInstance()

– javaString()

– JavaIterable / JavaIterator– CeylonIterable / CeylonIterator

Page 15: Ceylon/Java interop by Tako Schotanus

Geecon: An Introduction to Ceylon

JavaScript interop

• All revolves around dynamic

dynamic value target;

dynamic {

target = jQuery(“#target”);

target.select(function() { ... });

}

• Typed interfaces in the future?

Page 16: Ceylon/Java interop by Tako Schotanus

Geecon: An Introduction to Ceylon