Skip to content

Instantly share code, notes, and snippets.

@klauern klauern/inerop.clj
Created Feb 17, 2012

What would you like to do?
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 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

This comment has been minimized.

Copy link
Owner Author

commented Feb 17, 2012

To add to it, I toyed a bit with the (quote) form to see if I could create a def for the factory, but I'm pretty sure I'm lost on this front, too:

=> (def mref 'com.service.mect.MECTPortFactory)
=> (mref/createMECTPort)
#<CompilerException java.lang.RuntimeException: No such namespace: mref, compiling:(NO_SOURCE_PATH:1)>
=> (com.service.mect.ucap/mref (createMECTPort))
#<CompilerException java.lang.RuntimeException: Unable to resolve symbol: createMECTPort in this context, compiling:(NO_SOURCE_PATH:1)>
=> (com.service.mect.ucap/mref (.createMECTPort))
#<CompilerException java.lang.IllegalArgumentException: Malformed member expression, expecting (.member target ...), compiling:(NO_SOURCE_PATH:1)>
=> (com.service.mect.ucap/mref createMECTPort)
#<CompilerException java.lang.RuntimeException: Unable to resolve symbol: createMECTPort in this context, compiling:(NO_SOURCE_PATH:1)>
=> (com.service.mect.ucap/mref/createMECTPort)
#<CompilerException java.lang.RuntimeException: No such namespace: com.service.mect.ucap/mref, compiling:(NO_SOURCE_PATH:1)>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.