Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

generate java beans with clojure

Is there a way to easily generate java beans given a vector in clojure? For example given a vector like this:

[
    String :key1
    Integer :key2
]

I'd like it to generate code like this:

public class NotSureWhatTheTypeWouldBeHere {
    private String key1;
    private Integer key2;

    public NotSureWhatTheTypeWouldBeHere() {}
    public NotSureWhatTheTypeWouldBeHere(String key1, Integer key2) {
        this.key1 = key1;
        this.key2 = key2;
    }

    public void setKey1(String key1) {
        this.key1 = key1;
    }
    public String getKey1() {
        return this.key1;
    }
    public void setKey2(Integer key2) {
        this.key2 = key2;
    }
    public String getKey2() {
        return this.key2;
    }

    // and equals,hashCode, toString, etc.
}

For context, I'd like to write an app that is written in java but calls into a library written in clojure. So that means the return values should be java beans (I know they don't have to be, but I'd like them to be). One way would be to define the model in java and then use clojure's normal java interop to populate the model in the clojure code, but I like the idea of a concise clojure vector(or map) expanding out to a (verbose) java bean.

like image 533
Kevin Avatar asked Mar 17 '12 19:03

Kevin


1 Answers

I don't think your Java code would play nicely with auto generated Java bean compliant classes. You need to have at least an interface on the Java side to make any sense of what's Clojure is going to return. Without that, you will have to revert to:

Object result = callClojureLib(params);

Then, no matter if the actual result implements the Java bean contract, your Java code will have to do all sorts of reflection wizardry to be able to even call a setter, as you're missing the class specification.

Another approach to the problem would be to use the java.util.Map interface as the contract between Java and Clojure worlds. This way, you could just use plain Clojure maps as transfer objects, as they are assignable to java.util.Map:

user=> (isa? (class {}) java.util.Map)
true
like image 81
skuro Avatar answered Sep 30 '22 04:09

skuro