English 中文(简体)
我怎样把 Java门的`静态'固定下来的固定点带入一个克隆名称空间?
原标题:How do I pull `static final` constants from a Java class into a Clojure namespace?

我正试图把一个具有拘束力的 Java图书馆包上。 Java图书馆的一个特殊班级界定了固定的固定定级固定不变的一组,例如:

class Foo {
    public static final int BAR = 0;
    public static final int SOME_CONSTANT = 1;
    ...
}

我曾认为,我也许能够检查这几类,并将这些固定装置带入我的克隆名称空间,而没有明文的<代码>def-- 每一条。

例如,它不是像这样明确连接:

(def foo-bar Foo/BAR)
(def foo-some-constant Foo/SOME_CONSTANT)

页: 1 Foo 类别和动态电线foo-barfoo-some-constant,载于我的服装名称空间,该模块装上。

我认为这样做有两个原因:

(a) 在<代码>上添加新的固定装置。 Foo。 换言之,如果 Java接口增加了新的不变,我就不必修改我的克隆包裹。

B)我可以保证,固定不变者会遵守一个更衣着的命名公约。

我不会在这样做时真正出售,但似乎要要求扩大我对克隆人/贾瓦人之间的了解,这是一个好的问题。

增 编

最佳回答

可悲的是,宏观clojure.contrib.import-static 不允许进口<>allall的静态最后领域。 你必须提供进口田地清单。

该表是 进口-static的直属包装:

(ns stackoverflow
  (:use clojure.contrib.import-static)
  (:import (java.lang.reflect Modifier)))

(defmacro import-static-fields
  "Imports all static final fields of the class as (private) symbols
  in the current namespace.

  Example: 
      user> (import-static-fields java.lang.Integer)
      # user/TYPE
      user> MAX_VALUE
      2147483647

  Note: The class name must be fully qualified, even if it has already
  been imported."
  [class]
  (let [final-static-field? (fn [field]
                  (let [modifiers (.getModifiers field)]
                (and (Modifier/isStatic modifiers) (Modifier/isFinal modifiers))))
    static-fields (map #(.getName %)
               (filter
                final-static-field?
                (.. Class (forName (str class)) getFields)))]
    `(import-static ~class ~@static-fields)))
问题回答

(本答案现在包括两种工作解决办法,一种基于我最初的想法,即:intern,另一种基于下述建议:使用c.c.import-static。 我猜测,我需要稍后清理,但现在我可以花更多的时间。

选取静态田:

(filter #(bit-and java.lang.reflect.Modifier/STATIC (.getModifiers %))
        (.getFields YourClass))

www.un.org/Depts/DGACM/index_french.htm 注:该轨道未经测试,特别是,I m无法确定nil in .get; 试验java.lang。 外地,见与你的班次做什么。

<><>上>

Ok, 实际上是intern based approach is not that misreadability-wise:

user> (map #(intern *ns* (symbol (str "integer-" (.getName %))) (.get % java.lang.Integer))
           (filter #(bit-and java.lang.reflect.Modifier/STATIC
                             (.getModifiers %))
                   (.getFields java.lang.Integer)))
(# user/integer-MIN_VALUE # user/integer-MAX_VALUE # user/integer-TYPE # user/integer-SIZE)
user> integer-MIN_VALUE
-2147483648
user> integer-MAX_VALUE
2147483647
user> integer-TYPE
int
user> integer-SIZE
32

www.un.org/Depts/DGACM/index_french.htm

将关于<代码>clojure.contrib的知识与上述内容结合起来:

user> (map #(eval `(import-static java.lang.Integer ~(symbol (.getName %))))
           (filter #(bit-and java.lang.reflect.Modifier/STATIC
                             (.getModifiers %))
                   (.getFields java.lang.Integer)))
(# user/MIN_VALUE # user/MAX_VALUE # user/TYPE # user/SIZE)
user> MIN_VALUE
-2147483648
user> MAX_VALUE
2147483647
user> TYPE
int
user> SIZE
32

它使用<代码>eval......,因此很难达到“技能性能”,实际上可以读懂,使用<代码>intern加以详细表述。 <>m>(实际上不是......) www.un.org/Depts/DGACM/index_french.htm 如果我上面的草图不正确,可以给你适当的想法。

我曾尝试过,但也许可以装饰。

公正的检查: 在使用进口统计时,你必须指明这些方法/领域,但我在此留有答案,因为这可能有助于寻求相关答案的人。





相关问题
How to improve Clojures error messages

I ve been playing a bit with Clojure and so far is fairly impressed, but one thing that I keep running into is wierd error messages from Clojure. This comes in two forms: Java errors, like null ...

clojure rmi classpath problem

I am trying to use clojure to implement a "plugin" for some vendor supplied software. Here is a little background on the vendor supplied software. It expects me to implement a particular interface ...

Help translating this Java codeblock to Clojure?

I m getting my feet wet with Clojure, and trying to get used to functional programming. I ve been translating various imperative functions from other languages into their Clojure equivalents -- and ...

Is functional Clojure or imperative Groovy more readable?

OK, no cheating now. No, really, take a minute or two and try this out. What does "positions" do? Edit: simplified according to cgrand s suggestion. (defn redux [[current next] flag] [(if flag ...

taking java method names as function arg in clojure

All, I want to create a function that takes a symbol representing a java method and applies it to some object: (user=> (defn f [m] (. "foo" (m))) When I execute this, I get a result much ...

how to efficiently apply a medium-weight function in parallel

I m looking to map a modestly-expensive function onto a large lazy seq in parallel. pmap is great but i m loosing to much to context switching. I think I need to increase the size of the chunk of work ...

热门标签