Slide 1

Slide 1 text

Java Interoperability Friday, October 11, 13

Slide 2

Slide 2 text

Access Java in Clojure Friday, October 11, 13

Slide 3

Slide 3 text

Calling Methods Friday, October 11, 13

Slide 4

Slide 4 text

(.method object) Friday, October 11, 13

Slide 5

Slide 5 text

(.toUpperCase "abc") ;; => "ABC" Friday, October 11, 13

Slide 6

Slide 6 text

(.method object x y) Friday, October 11, 13

Slide 7

Slide 7 text

(.equals "a" "b") ;; => false Friday, October 11, 13

Slide 8

Slide 8 text

(.getName String) ;; => "java.lang.String" Friday, October 11, 13

Slide 9

Slide 9 text

(Class/static-var) (Class/method) (Class/method x) (Class/method x y) Friday, October 11, 13

Slide 10

Slide 10 text

(Math/PI) ;; => 3.141592653589793 (Thread/activeCount) Friday, October 11, 13

Slide 11

Slide 11 text

(.println (System/out) "hello") Friday, October 11, 13

Slide 12

Slide 12 text

(.toUpperCase "abc") (. "abc" toUpperCase) Friday, October 11, 13

Slide 13

Slide 13 text

(Thread/activeCount) (. Thread activeCount) Friday, October 11, 13

Slide 14

Slide 14 text

(.equals "a" "b") (. "a" equals "b") Friday, October 11, 13

Slide 15

Slide 15 text

(.println (System/out) "hello") (. (. System out) println "hello") (.. System out (println "hello")) Friday, October 11, 13

Slide 16

Slide 16 text

(.. Runtime getRuntime availableProcessors) Friday, October 11, 13

Slide 17

Slide 17 text

New Objects Friday, October 11, 13

Slide 18

Slide 18 text

(new Object) (Object.) (java.util.HashMap.) (Integer. 5) Friday, October 11, 13

Slide 19

Slide 19 text

(let [hashmap (java.util.HashMap.)] (.put hashmap "a" "b") (.get hashmap "a")) Friday, October 11, 13

Slide 20

Slide 20 text

(import [java.util HashMap ArrayList]) (HashMap.) (ArrayList.) Friday, October 11, 13

Slide 21

Slide 21 text

(let [hashmap (HashMap.)] (.put hashmap "a" "b") (.put hashmap "x" "y") hashmap) Friday, October 11, 13

Slide 22

Slide 22 text

(let [hashmap (HashMap.)] (.put hashmap "a" "b") (.put hashmap "x" "y") hashmap) (doto (HashMap.) (.put "a" "b") (.put "x" "y")) Friday, October 11, 13

Slide 23

Slide 23 text

(instance? HashMap (HashMap.)) Friday, October 11, 13

Slide 24

Slide 24 text

Implementing Interfaces Friday, October 11, 13

Slide 25

Slide 25 text

(.start (Thread. target)) Friday, October 11, 13

Slide 26

Slide 26 text

(reify Interface (method [this x y z] body)) Friday, October 11, 13

Slide 27

Slide 27 text

(reify InterfaceA (method-a [this x y z] body) InterfaceB (method-b [this m n] body)) Friday, October 11, 13

Slide 28

Slide 28 text

(.start (Thread. target)) Friday, October 11, 13

Slide 29

Slide 29 text

(let [target (fn [] ...) thread (Thread. target)] (.start thread)) Friday, October 11, 13

Slide 30

Slide 30 text

(let [target (reify Runnable (run [this _] (println "hello"))) thread (Thread. target)] (.start thread)) Friday, October 11, 13

Slide 31

Slide 31 text

Miscellanea (if time allows) Friday, October 11, 13

Slide 32

Slide 32 text

(make-array (Integer/TYPE) 5) (aset (to-array [1 2 3]) 1 5) (into-array [1 2 3]) Friday, October 11, 13

Slide 33

Slide 33 text

Access Clojure in Java Friday, October 11, 13

Slide 34

Slide 34 text

Creating Class Friday, October 11, 13

Slide 35

Slide 35 text

(gen-class :name my.namespace.MyClass) (compile 'my.namespace) (my.namespace.MyClass.) Friday, October 11, 13

Slide 36

Slide 36 text

(gen-class :name com.example.Hello :methods [[hello [String] String]]) (defn -hello [arg] (str "Hello, " arg)) (.hello (com.example.Hello.) "Jan") Friday, October 11, 13

Slide 37

Slide 37 text

(gen-class :name com.example.Hello :prefix Hello :methods [[hello [String] String]]) (defn Hello-hello [arg] (str "Hello, " arg)) (.hello (com.example.Hello.) "Jan") Friday, October 11, 13

Slide 38

Slide 38 text

(gen-class :name com.example.Hello :init init :constructors {[String] []}) (defn -init [arg] (println "Hello, " arg)) (com.example.Hello. "Jan") Friday, October 11, 13

Slide 39

Slide 39 text

(import [clojure.lang Seqable]) Friday, October 11, 13

Slide 40

Slide 40 text

(gen-class :name com.example.Hello :init init :state state :constructors {[Seqable] []}) (defn -init [seq] [nil seq]) (com.example.Hello. [:a :b :c]) Friday, October 11, 13

Slide 41

Slide 41 text

(gen-class :name com.example.Hello :init init :state state :constructors {[Seqable] []}) (defn -init [seq] [nil seq]) (com.example.Hello. [:a :b :c]) (defn -toString [this] (str (.state this))) Friday, October 11, 13

Slide 42

Slide 42 text

(gen-class :name com.example.Hello :implements [java.io.Serializable]) Friday, October 11, 13

Slide 43

Slide 43 text

(ns org.example.namespace (:gen-class)) (defn -main [arg] (println (.toUpperCase arg))) (compile 'org.example.namespace) java org.example.namespace hello! Friday, October 11, 13