2/17/2012 - 11:23 AM

learning java interop

learning java interop

; So, I am new to Clojure, and I spent a bit of time reading the Java Interop page (so obviously not enough
; in retrospect), wanting to create and call some methods on instances.  I have two here, and they are glorified 
; wrappers around a SOAP interface.  One is called MECT, which is the port that makes the call, and the other is
; called QueryHeartbeat, which is a small helper class I use to make a heartbeat call against this
; SOAP service

; some hiccups in the beginning, trying to import the classes:
=> (import [com.service.mect.MECTPortFactory :as MECT]) ; whoops
#<ClassNotFoundException java.lang.ClassNotFoundException: com.service.mect.MECTPortFactory.:as>
=> (import 'com.service.mect.MECTPortFactory)  ; ok
=> (import com.service.mect.MECTPortFactory) ; still okay
=> (import (com.service.mect.MECTPortFactory)) ; guess it doens't care how I do this?
=> (import [com.service.mect.MECTPortFactory]) ; hm...
; confusing to me on two fronts:
; I can practically throw anything I want in there and it will work, but sometimes it returns nil,
; sometimes returns the class name.  What's the difference? 

; this is also not in the Java Interop page at all (I don't even see a reference to 'import' on the
; page.  I'm assuming you have to get this far to consider wanting to interop with Java, but
; not knowing how to bring a class in resulted in me looking on www.clojuredocs.org for a way to 
; do it, then I came back to the interop page.

; First mistake in my learning: def can not be used as an alias definer for the ugliness of Java's
; classes:
(def mect (com.service.mect.MECTPortFactory)) ; this is a class that provides a static method to
; create the MECT object.

; instead, I just create the instance myself:
(def mect (com.service.mect.MECTPortFactory/createMECTPort)) 

; second learning piece: I'm still confused about when I should evaluate the call versus just
; reference it:
(def heartbeat com.service.mect.QueryHeartBeat.)  ; whoops, nope that's not right
(def heartbeat (com.service.mect.QueryHeartBeat.)) ; that's better

; third, the dot special form seems like several aliases for the same calls, and putting a space
; in there is the same as not:
(def alive (.queryHeartbeat heartbeat mect)) ; same as
(def alive (. heartbeat queryHeartbeat mect)) ; same as
(def alive (. heartbeat (queryHeartbeat mect))) ; same as

; going back against point #2, why are the second and third of these the same?
(def mect (com.service.mect.MECTPortFactory/createMECTPort)) ; same as
(def mect (. com.service.mect.MECTPortFactory createMECTPort)) ; same as
(def mect (. com.service.mect.MECTPortFactory (createMECTPort)) ; we evaluate the method call here,
  ; so what were we doing in the 2nd of the three?

; Now, I /think/ I have a better handle on the Java Interop, but I stumbled like hell to get there,
; obviously because I made alot of assumptions about behavior that just weren't there,
; and looking at the examples gave me several options for the same method, and it seems there's
; a gray area between what you (evaluate) in the parens and what you .reference in them.
; That's okay, but I guess I just don't see the consistency in how this relates to the rest of
; the Clojure world.

; to sum up: I take responsibility for not really reading and understanding the Java Interop page,
; but I have yet to see where all of this is supposed to make sense if you were working primarily
; in Clojure to start and wanting to start playing with the Java libraries out there.

; I also don't see why there has to be so many ways to do this, especially when you have to factor
; in Java's memory model about static and instance fields/methods. It seems like a mishmash of
; various ways to make the same call and work consistently across static fields/methods and instance
; fields/methods

; This doesn't shy me away from Clojure, but I'm going to have to double-down on my reading of
; some of the tutorials I have handy, as I'm obviously not getting something and need
; to clear my head of my assumptions