Download - Ceylon/Java interop by Tako Schotanus
Java InteropTako Schotanus, Red Hat
Geecon: An Introduction to Ceylon
Combined sources
public class JavaClass {
int count(String name) { ... }
}
shared void run() {
value jc = JavaClass();
print( jc.count("thing") );
}
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
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
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()
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
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) { ... }
}
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) { ... }
}
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
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());
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 { ... }
Geecon: An Introduction to Ceylon
Arrays
• BooleanArray --> boolean[ ]
• IntArray --> int[ ]
• ByteArray, CharArray, etc
• ShortArray(...).array --> Array<j.l.Short>
• Array<T>
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
Geecon: An Introduction to Ceylon
ceylon.interop.java
• Part of the SDK
• Useful functions like– javaClassFromInstance()
– javaString()
– JavaIterable / JavaIterator– CeylonIterable / CeylonIterator
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?
Geecon: An Introduction to Ceylon