标签: gen-class

clojure gen-class varargs构造函数

在:constructors map和后续-init定义中,我如何表示一个varargs构造函数(假设超类有多个构造函数,其中一个是varargs)?

clojure java-interop gen-class clojure-java-interop

8
推荐指数
1
解决办法
966
查看次数

Clojure使用gen-class的多个构造函数

如何在clojure中使用gen-class定义多个构造函数和状态?我没有看到使用单值映射执行此操作的方法:init,:state和:constructors.

clojure gen-class

6
推荐指数
1
解决办法
1479
查看次数

如何在Leiningen中的Clojure代码之后编译Java代码

在我的Leiningen项目中:

(defproject com.stackoverflow.clojure/tests "0.1.0-SNAPSHOT"
  :description "Tests of Clojure test-framework."
  :url "http://example.com/FIXME"
  :license {:name "Eclipse Public License"
            :url "http://www.eclipse.org/legal/epl-v10.html"}
  :dependencies [[org.clojure/clojure "1.6.0"]
                 [instaparse "1.3.4"]]
  :aot [com.stackoverflow.clojure.testGenClass]
  :source-paths      ["src/main/clojure"]
  :java-source-paths ["src/main/java"]
  :test-paths        ["src/test/clojure"]
  :java-test-paths   ["src/test/java"]
  )
Run Code Online (Sandbox Code Playgroud)

我正在用gen-class生成Java类:

(ns com.stackoverflow.clojure.testGenClass
  (:gen-class
     :name com.stackoverflow.clojure.TestGenClass
     :implements [com.stackoverflow.clojure.TestGenClassInterface]
     :prefix "java-"))

(def ^:private pre "START: ")

(defn java-addToString [this text post]
  (str pre text post))
Run Code Online (Sandbox Code Playgroud)

我想在Java中使用:

package com.stackoverflow.clojure;

public class TestGenClassTest {

    private TestGenClassTest() {
    }

    public static void main(String[] args) {
        TestGenClassInterface gc = new TestGenClass(); …
Run Code Online (Sandbox Code Playgroud)

java compilation clojure gen-class leiningen

6
推荐指数
1
解决办法
864
查看次数

将元数据附加到Clojure gen-class

是否可以将元数据附加到Clojure gen-class?

我正在尝试实现一个使用库的服务器,该库需要将Java注释添加到类中.

从Chas Emerick等人,即将出版的书"Programming Clojure"(第9.7.3节)中,向gen-class方法添加注释很容易,但没有提到添加类级别的注释.

annotations clojure gen-class

5
推荐指数
1
解决办法
3433
查看次数

使用gen-class的clojure宏不会创建注释

我正在尝试编写一个clojure宏,它将用于在编译时生成多个Java类.我发现当我在宏之外调用gen-class时,我可以为类添加注释.但是,当我尝试在宏中使用gen-class时,编译的类没有注释.

我把问题归结为这个例子:

(gen-class
  :name ^{Deprecated true} Test1
  :prefix Test1-
  :methods [[^{Deprecated true} getValue [] Integer]])

(defn Test1-getValue [] 42)

(defmacro create-test-class [name x]
  (let [prefix (str name "-")]
    `(do
      (gen-class
         :name ~(with-meta name {Deprecated true})
         :prefix ~(symbol prefix)
         :methods [[~(with-meta 'getValue {Deprecated true}) [] Integer]])
      (defn ~(symbol (str prefix "getValue")) [] ~x))))

(create-test-class Test2 56)
Run Code Online (Sandbox Code Playgroud)

当我编译这个文件时,它创建了一个Test1.class和Test2.class - 我用Eclipse检查它们,发现Test1既有类级别和方法级别@Deprecated注释,但是没有注释的Test2.class.当我使用macroexpand时,看起来我的Test2.class应该被注释:

user=> (set! *print-meta* true)
true
user=> (macroexpand '(create-test-class Test2 56))
(do (clojure.core/gen-class :name ^{java.lang.Deprecated true} Test2 :prefix Test2- :methods [[^{java.lang.Deprecated true} getValue …
Run Code Online (Sandbox Code Playgroud)

macros annotations clojure gen-class

5
推荐指数
1
解决办法
577
查看次数

gen类可以覆盖受保护的Java方法吗?

我正在尝试使用来自Clojure的Swing,我对此感到困惑gen-class,我无法从文档中判断出这是否可行 - paintComponent是一个受保护的方法JPanel,我可以覆盖它,但是当我试着调用暴露的超类的方法java.lang.IllegalArgumentException: No matching method found: parentPaintComponent for class project.PicturePanel.任何人都可以澄清为什么我似乎无法访问此方法?

(ns project.PicturePanel
  (:gen-class
    :extends javax.swing.JPanel
    :name project.PicturePanel
    :exposes-methods {paintComponent parentPaintComponent}))

(defn -paintComponent [this g]
  (println this)
  (println g)
  (.parentPaintComponent this g))
Run Code Online (Sandbox Code Playgroud)

clojure gen-class

5
推荐指数
1
解决办法
728
查看次数

如何使用clojure的Gen-class生成生成静态方法?

在我的Clojure代码中,我想生成一个包含静态方法(命名staticMethod)的类文件,稍后在Java程序的静态上下文中调用该方法.

我试过(Clojure):

(ns com.stackoverflow.clojure.testGenClass
  (:gen-class
     :name com.stackoverflow.clojure.TestGenClass
     :prefix "java-"
     :methods [
               [#^{:static true} staticMethod [String String] String]
              ]))

(def ^:private pre "START: ")

(defn #^{:static true} java-staticMethod [this text post]
  (str pre text post))
Run Code Online (Sandbox Code Playgroud)

和(Java):

package com.stackoverflow.clojure;

public class TestGenClassTest {

    private TestGenClassTest() {
    }

    public static void main(String[] args) {
        TestGenClass.staticMethod("Static call from Java!", " :END");
    }
}
Run Code Online (Sandbox Code Playgroud)

https://kotka.de/blog/2010/02/gen-class_how_it_works_and_how_to_use_it.html上我读到:

通过将元数据 - 通过#^ {:static true}添加到方法声明中,您还可以定义静态方法.

无论我把#^{:static true}Java编译器放在哪里,总是说:

无法从TestGenClass类型对静态方法staticMethod(String,String)进行静态引用

如何在Clojure中定义静态方法?会#^{:static true}^:static意思相同吗?这记录在哪里?

java static static-methods clojure gen-class

5
推荐指数
1
解决办法
1702
查看次数

Leiningen为什么不能总是使用我的:gen-class呢?

假设我创建了一个新的Leiningen项目(lein new app example)并添加了一些代码,example/src/example/core.clj其中使用了:gen-class:

(ns example.core
  (:gen-class :extends javafx.application.Application))

(defn -start [this stage]
  (.show stage))

(defn -main [& args]
  (javafx.application.Application/launch example.core args))
Run Code Online (Sandbox Code Playgroud)

如果我然后创建一个JAR(lein uberjar)并运行它,一切正常.但是,如果我试着直接运行我的应用程序(lein run),我会得到一个ClassNotFoundException.另外,如果我打开一个REPL(lein repl),我首先得到与以前相同的错误,但在运行此代码后:

(compile 'example.core)
Run Code Online (Sandbox Code Playgroud)

错误不再出现在lein run 或中 lein repl.

有人可以向我解释这里到底发生了什么,以及如何直接运行我的应用程序而无需从REPL手动编译我的代码?

编辑:在多了一点之后,我发现这个问题的解决方案是添加

:aot [example.core]
Run Code Online (Sandbox Code Playgroud)

project.clj.(感谢@Mars!)我仍然感到困惑,因为我之前尝试过简单地删除^:skip-aot,(根据文档)应该工作:

这将是AOT默认编译; 要禁用此功能,请将^:skip-aot元数据附加 到命名空间符号.

但事实并非如此.为什么?

另一个编辑(如果我将其中任何一个分成一个单独的问题,让我知道,我会这样做):我一直在玩连字符(lein new app my-example),并且发生了奇怪的事情.这不起作用:

(ns my-example.core
  (:gen-class :extends javafx.application.Application))
;; …
Run Code Online (Sandbox Code Playgroud)

clojure gen-class leiningen

5
推荐指数
1
解决办法
1567
查看次数

用于重载和重写方法的Clojure gen-class

我正试图在clojure中gen-class覆盖此类中的compare(WriteableComparable a, WriteableComparable b)方法.复杂性来自于此方法重载3次:

  • int compare(WritableComparable a, WritableComparable b)
  • int compare(Object a, Object b)
  • int compare(byte[] b1, int s1, int l1, byte[] b2, int s2, int l2)

到目前为止我的尝试看起来像这样:

(gen-class
 :name comparators.MyWriteableComparator
 :extends org.apache.hadoop.io.WritableComparator
 :exposes-methods {compare superCompare}
 :prefix "interop-")

(defn interop-compare
  ([this a b c d e f]
     (.superCompare this a b c d e f))
  ([this ^WritableComparable w1 ^WritableComparable w2]         
     (.compareTo (.getSymbol ^SymbolPair w1) 
                 (.getSymbol ^SymbolPair w2))))
Run Code Online (Sandbox Code Playgroud)

一切都编译,但是当我运行它时,我得到一个空指针异常,我怀疑这是因为我重写了错误的方法(即compare(Object a, Object b)代替了预期的方法compare(WritableComparable …

java interop clojure gen-class

5
推荐指数
1
解决办法
662
查看次数

在Clojure中扩展类的问题:ClassFormatError:重复的字段名称和签名

我正在尝试使用Clojure扩展JButton,但是当我尝试创建自己的构造函数时遇到了一个问题.每当我使用时,当我尝试实例化我的类时:constructors,:gen-class我会不断收到"ClassFormatError:Duplicate field name&signature"消息.

我正在遵循Clojure文档.难道我做错了什么?

例:

(ns test.gui.button
  (:gen-class
   :extends javax.swing.JButton
   :constructors {[] [String]}
   :init init))

(defn -init []
  [["Click Me"] nil])
Run Code Online (Sandbox Code Playgroud)

oop clojure gen-class

4
推荐指数
1
解决办法
1107
查看次数