I've got pretty simple java code:
public class MessageListenerExample extends ListenerAdapter
{
@Override
public void onMessageReceived(MessageReceivedEvent event)
{
// do something with event
}
}
However, I can't seem to understand how to turn that code into clojure code. The docs and articles are very confusing. I'd be happy to see more examples too. I'm also interested in using implements
.
You can use proxy
to extend an existing Java class and also implement interfaces. For example:
(import '[java.awt.event ActionListener ActionEvent KeyAdapter KeyEvent])
(def listener
(proxy
;; first vector contains superclass and interfaces that the created class should extend/implement
[KeyAdapter ActionListener]
;; second vector contains arguments to superclass constructor
[]
;; below are all overriden/implemented methods
(keyPressed [event]
(println "Key pressed" event))
(actionPerformed [action]
(println "Action performed" action))))
(.keyPressed listener nil)
;; => Key pressed nil
(.actionPerformed listener nil)
;; => Action performed nil
Depending on what you need to do, a few options:
(ns your.class.Name
:extends whatever.needs.to.be.Extended)
(defn -methodYouOverride ; mind the dash - it's important
[...] ...)
I wouldn't recommend going down this path unless absolutely necessary. Getting compilation right (including AOT compilation) is tricky. And in the end you still need to use Java interop to work with the objects of this class, so not sure it's worth the hassle, which brings me to:
Code it in Java and use Java interop to work with it.
If you actually need to create an instance of an object that implements a certain interface, then it's easier:
(reify
InterfaceYouImplement
(methodYouImplement [..] ..)
I use it around my code, it's really much nicer that coding in Java.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With