如何在运行时在clojure中定义一个java类并实例化它

时间:2015-11-03 17:10:15

标签: clojure gen-class

我尝试使用gen-and-load-class中的clojure.core,然后使用自定义类加载器使用生成的字节码调用defineClass,但是当我调用

(foo.bar.MyClass.)

我正在

CompilerExceptionjava.lang.NoClassDefFoundError: Could not initialize class foo.bar.MyClass

更新:

所以我按照@Elogent的建议使用了deftype:

(defprotocol Struct
  (getX [this path] "Get value")
  (setX [this ^long value path] "Get value"))
(deftype Foo 
  [
  ^{:tag long :unsynchronized-mutable true} a 
  ^{:tag long :unsynchronized-mutable true} b 
  ^{:tag long :unsynchronized-mutable true} c]
  Struct
  (getX
    [this [head & tail]]
    (let [field (condp = head
          'a a
          'b b
          'c c)]
      (if (empty? tail)
        field
        (getX field tail))))
  (setX
    [this value [head & tail]]

    (if (empty? tail)
        (condp = head
          (set! a (long value))
          (set! b (long value))
          (set! c (long value)))
        (condp = head
          (setX a value tail)
          (setX b value tail)
          (setX c value tail)))))

当我做javap Foo.class之后的AOT之后我得到了:

public final class struct.core.Foo implements struct.core.Struct,clojure.lang.IType {
  public static final clojure.lang.Var const__0;
  public static final java.lang.Object const__1;
  public static final clojure.lang.Var const__2;
  public static final java.lang.Object const__3;
  public static final clojure.lang.Var const__4;
  public static final clojure.lang.AFn const__5;
  public static final clojure.lang.AFn const__6;
  public static final clojure.lang.AFn const__7;
  public static final clojure.lang.Var const__8;
  public static final clojure.lang.Var const__9;
  public static final clojure.lang.Var const__10;
  public static final clojure.lang.Var const__11;
  public static final clojure.lang.Var const__12;
  long a;
  long b;
  long c;
  public static {};
  public struct.core.Foo(long, long, long);
  public static clojure.lang.IPersistentVector getBasis();
  public java.lang.Object setX(java.lang.Object, java.lang.Object);
  public java.lang.Object getX(java.lang.Object);
}

这正是我所需要的。谢谢@Elogent!

1 个答案:

答案 0 :(得分:1)

除非您绝对需要Java类的所有灵活性,否则我建议您使用deftype代替。正如here所解释的那样,deftype使您能够以惯用的方式访问较低级别的构造,例如原始类型和可变性。