Implementing Interfaces and calling Java Constants in Clojure (Newbie)

533 views Asked by At

I am trying to write a wrapper for the google adwords api in Clojure but struggle with constants and Interfaces. The java code looks like this :

CampaignServiceInterface campaignService =
    user.getService(AdWordsService.V201109.CAMPAIGN_SERVICE);

Usually you can call constants in Clojure with e.g. (Math/PI) but when I write:

(def user (AdWordsUser. ))
(.getService user (AdWordsService/V201109/CAMPAIGN_SERVICE))

I just get "no such namespace". Also I am a bit clueless on how to implement the interface correct. I think I should use "reify" but I get stuck.

Link to Interface: http://google-api-adwords-java.googlecode.com/svn-history/r234/trunk/docs/com/google/api/adwords/v201003/cm/CampaignServiceInterface.html

(defn campaign-service [ ]
(reify 
  com.google.adwords.api.v201109.cm.CampaignServiceInterface
  (get [this] ??))))
2

There are 2 answers

0
Joost Diepenmaat On

If I read it correctly, AdWordsService.V201109.CAMPAIGN_SERVICE is a static constant of an inner class of class AdWordsService.

To access inner classes you need to use java's internal name mangling scheme **; separate the inner class from its outer class with a $ sign:

AdWordsService$V201109/CAMPAIGN_SERVICE

** the JVM doesn't actually have a notion of inner classes, so java "fakes" it by creating a standalone class AdWordsService$V201109

1
Mikita Belahlazau On

1.About accessing constants. Did you import AdWordsService? If not you either can access AdWordsService with fully qualified name: some.package.name.AdWordsService/V201109/CAMPAIGN_SERVICE, or import it via import macro.

2.Check examples here: http://clojuredocs.org/clojure_core/clojure.core/reify

(defn campaign-service [ ]
(reify   
  com.google.adwords.api.v201109.cm.CampaignServiceInterface
  (get [_ selector] (some-function selector))
  (mutate [_ operations] (some-function-2 operations))))