Thursday, 3 May 2018

Access non-static enum in Clojure

Let's say there is a Java class with a non-static enum field as below.
package com.example;

public class Encrypter {

    public enum KeyPlacement {
        PEER,
        INLINE
    }

    private KeyPlacement keyPlacement;

    public KeyPlacement getKeyPlacement() {
        return this.keyPlacement;
    }

    public void setKeyPlacement(final KeyPlacement newKeyPlacement) {
        this.keyPlacement = newKeyPlacement;
    }
}

Setting keyPlacement value from Clojure can be done as follows.
(ns core
  (:import [com.example Encrypter Encrypter$KeyPlacement]))

(doto (Encrypter.)
  (.setKeyPlacement Encrypter$KeyPlacement/INLINE))


When compiling the above source, inner classes, enums etc. gets generated with $ appended as Encrypter$KeyPlacement. So we can import those and access them from Clojure.

No comments:

Post a Comment