summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStuart Halloway <stu@thinkrelevance.com>2010-04-20 19:45:50 -0400
committerStuart Halloway <stu@thinkrelevance.com>2010-04-20 22:08:18 -0400
commit084e5604710eb16df43ad529adabf18f40795bf9 (patch)
treeb5b2e59fd2c371bfb1bd120d9566209fce8b4890
parente893050b513778e52ed8ef0cc0b5fc9f81b7bdc2 (diff)
munge genclass field names to keep Java side happy, see #104
Signed-off-by: Stuart Halloway <stu@thinkrelevance.com>
-rw-r--r--build.xml1
-rw-r--r--src/clj/clojure/genclass.clj2
-rw-r--r--test/clojure/test_clojure.clj1
-rw-r--r--test/clojure/test_clojure/genclass.clj38
-rw-r--r--test/clojure/test_clojure/genclass/examples.clj19
5 files changed, 60 insertions, 1 deletions
diff --git a/build.xml b/build.xml
index 99661c4e..0f7f14d2 100644
--- a/build.xml
+++ b/build.xml
@@ -116,6 +116,7 @@
failonerror="true">
<sysproperty key="clojure.compile.path" value="${test-classes}"/>
<arg value="clojure.test-clojure.protocols.examples"/>
+ <arg value="clojure.test-clojure.genclass.examples"/>
</java>
</target>
diff --git a/src/clj/clojure/genclass.clj b/src/clj/clojure/genclass.clj
index 91fc0492..de8ae088 100644
--- a/src/clj/clojure/genclass.clj
+++ b/src/clj/clojure/genclass.clj
@@ -121,7 +121,7 @@
factory-name (str factory)
state-name (str state)
main-name "main"
- var-name (fn [s] (str s "__var"))
+ var-name (fn [s] (clojure.lang.Compiler/munge (str s "__var")))
class-type (totype Class)
rt-type (totype clojure.lang.RT)
var-type #^Type (totype clojure.lang.Var)
diff --git a/test/clojure/test_clojure.clj b/test/clojure/test_clojure.clj
index bb7764d2..346aa406 100644
--- a/test/clojure/test_clojure.clj
+++ b/test/clojure/test_clojure.clj
@@ -49,6 +49,7 @@
:clojure-xml
:clojure-zip
:protocols
+ :genclass
])
(def test-namespaces
diff --git a/test/clojure/test_clojure/genclass.clj b/test/clojure/test_clojure/genclass.clj
new file mode 100644
index 00000000..f0d2b92b
--- /dev/null
+++ b/test/clojure/test_clojure/genclass.clj
@@ -0,0 +1,38 @@
+; Copyright (c) Rich Hickey. All rights reserved.
+; The use and distribution terms for this software are covered by the
+; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
+; which can be found in the file epl-v10.html at the root of this distribution.
+; By using this software in any fashion, you are agreeing to be bound by
+; the terms of this license.
+; You must not remove this notice, or any other, from this software.
+
+; Author: Stuart Halloway
+
+(ns clojure.test-clojure.genclass
+ (:use clojure.test)
+ (:import clojure.test-clojure.genclass.examples.ExampleClass))
+
+;; pull this up to a suite-wide helper if you find other tests need it!
+(defn get-field
+ "Access to private or protected field. field-name is a symbol or
+ keyword."
+ ([klass field-name]
+ (get-field klass field-name nil))
+ ([klass field-name inst]
+ (-> klass (.getDeclaredField (name field-name))
+ (doto (.setAccessible true))
+ (.get inst))))
+
+(deftest arg-support
+ (let [example (ExampleClass.)
+ o (Object.)]
+ (is (= "foo with o, o" (.foo example o o)))
+ (is (= "foo with o, i" (.foo example o (int 1))))
+ (is (thrown? java.lang.UnsupportedOperationException (.foo example o)))))
+
+(deftest name-munging
+ (testing "mapping from Java fields to Clojure vars"
+ (is (= #'clojure.test-clojure.genclass.examples/-foo-Object-int
+ (get-field ExampleClass 'foo_Object_int__var)))
+ (is (= #'clojure.test-clojure.genclass.examples/-toString
+ (get-field ExampleClass 'toString__var)))))
diff --git a/test/clojure/test_clojure/genclass/examples.clj b/test/clojure/test_clojure/genclass/examples.clj
new file mode 100644
index 00000000..70ff8aa2
--- /dev/null
+++ b/test/clojure/test_clojure/genclass/examples.clj
@@ -0,0 +1,19 @@
+(ns clojure.test-clojure.genclass.examples)
+
+(definterface ExampleInterface
+ (foo [a])
+ (foo [a b])
+ (foo [a #^int b]))
+
+(gen-class :name clojure.test-clojure.genclass.examples.ExampleClass
+ :implements [clojure.test-clojure.genclass.examples.ExampleInterface])
+
+;; -foo-Object unimplemented to test missing fn case
+
+(defn -foo-Object-Object
+ [_ o1 o2]
+ "foo with o, o")
+
+(defn -foo-Object-int
+ [_ o i]
+ "foo with o, i")