1
我曾尝试使用gen-and-load-class
从clojure.core
,然后使用自定义类加载器调用defineClass
与生成的字节码,但是当我打电话我如何用Clojure在运行时定义一个Java类,并创建实例
(foo.bar.MyClass.)
我“M越来越
CompilerExceptionjava.lang.NoClassDefFoundError: Could not initialize class foo.bar.MyClass
UPDATE:
所以我用DEFTYPE通过@Elogent的建议:
(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)))))
AOT后,当我做javap Foo.class
我:
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!
你试图解决什么问题?换句话说,你需要这个你定义的Java类的目的是什么? –
我正在构建原型,允许构建简洁的功能和缓存不经意的数据结构。为此,我需要构建等效的C strict,模型数据结构作为数组,而不使用引用等。 – Lambder
为什么?在这种情况下['defrecord'](http://clojuredocs.org/clojure.core/defrecord)或['deftype'](http://clojuredocs.org/clojure.core/deftype)有什么问题? –