i have a question regarding implementing java public interfaces in clojure
here is an example of my interface
`
public interface SomePublicInterface {
void aMethod(Enum arg1, String... arg2),
void aMethod (Enum arg1, AClass arg2, String... arg3)
}
Here is how i’m implementing it:
(defrecord SomeRecord [field1 name]
SomePublicInterface
(aMethod [this enum msg] (str "ENUM is " enum)))
(defn some-record
[name]
(->SomeRecord (.getTheThing (TheThingFactory/getInstance) "NamedThing") name))
(def test (some-record "testing-some-record-method"))
i am not able to understand why this isn’t working.
(aMethod test "ENUM" "MY TEST") ;;doesn't work
Unable to resolve symbol: aMethod in this context
(.aMethod test "ENUM" "MY TEST")
ClassCastException Cannot cast java.lang.String to java.lang.Enum java.lang.Class.cast (Class.java:3369)
it defaults to the types of the actual java Interace.
it doesn’t call the aMethod
i implemented.
I would like to call aMethod
on my record (type) with strings and i want it to print “ENUM is: MY TEST”
how do you know it doesn't call the method you implemented?
if I had to guess, from the exception, the interface you implemented requires it to return an Enum and you are returning a string (which doesn't match the example interface, but given the exception that is my guess)
to be honest i don’t know much about java.
i have to use a class that’s implementing the Interface SomePublicInterface
I just thought it would return back a string because that’s what i’m asking for.
the return type on aMethod
in the interface is void
I’m importing the Interface like this:
(import '[this.is.my.package SomePublicInterface])
@dhruv1 aMethod
is expecting an Enum
and an array of String
(the varargs part) or an Enum
, an AClass
(whatever that is), and an array of String
. Your implementation must match the signatures -- otherwise it isn't implementing the interface.
A string is not an Enum, but if you have a specific enumeration class, you can construct an Enum type from that class and the name used for one of the enumeration class members via (Enum/valueOf TheClass "name")
.
hey there. I am using a proxy to create an ENUM
(defn ->enum
[^String enum]
(proxy [Enum] [enum 1]))
how do i match the signature in my implementation?
In the code above you were trying to pass a string to the Enum argument and, quite correctly, got the exception "ClassCastException Cannot cast java.lang.String to java.lang.Enum"
right, so i was able to get past that by using a proxy and (into-array java.lang.String ["MY TEST"]
i am still at a loss here because of my lack of knowledge in Java and somewhat clojure
Were you then able to call (.aMethod test (->enum "ENUM") (into-array String ["MY TEST"]))
?
actually no i wasn’t
That Java interface looks like you made up the names rather than show us what the real code is -- are you working with a publicly available Java library? Or are you writing Java code yourself and compiling it and bringing it into your Clojure code?
at work we have a java library that i’m trying to use in clojure. it’s not public