Browse files

#407 use munge as cheap validity test for Java method names

Signed-off-by: Stuart Halloway <stu@thinkrelevance.com>
  • Loading branch information...
1 parent a2c95ef commit 60d5927fe7b9a2aa773b58666ad64ffa6379c1fe @stuarthalloway stuarthalloway committed Jul 29, 2010
Showing with 28 additions and 12 deletions.
  1. +11 −0 src/clj/clojure/genclass.clj
  2. +5 −12 test/clojure/test_clojure/genclass.clj
  3. +12 −0 test/clojure/test_clojure/helpers.clj
View
11 src/clj/clojure/genclass.clj
@@ -90,7 +90,18 @@
strx
(str "java.lang." strx))))))
+;; someday this can be made codepoint aware
+(defn- valid-java-method-name
+ [^String s]
+ (= s (clojure.lang.Compiler/munge s)))
+
+(defn- validate-generate-class-options
+ [{:keys [methods]}]
+ (let [[mname] (remove valid-java-method-name (map (comp str first) methods))]
+ (when mname (throw (IllegalArgumentException. (str "Not a valid method name: " mname))))))
+
(defn- generate-class [options-map]
+ (validate-generate-class-options options-map)
(let [default-options {:prefix "-" :load-impl-ns true :impl-ns (ns-name *ns*)}
{:keys [name extends implements constructors methods main factory state init exposes
exposes-methods prefix load-impl-ns impl-ns post-init]}
View
17 test/clojure/test_clojure/genclass.clj
@@ -9,25 +9,14 @@
(ns ^{:doc "Tests for clojure.core/gen-class"
:author "Stuart Halloway, Daniel Solano Gómez"}
clojure.test-clojure.genclass
- (:use clojure.test)
+ (:use clojure.test clojure.test-clojure.helpers)
(:import [clojure.test_clojure.genclass.examples ExampleClass
ExampleAnnotationClass]
[java.lang.annotation ElementType
Retention
RetentionPolicy
Target]))
-;; 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.)]
@@ -70,3 +59,7 @@
(let [target (aget first-param-annots 1)]
(is (instance? Target target))
(is (= [ElementType/TYPE ElementType/PARAMETER] (seq (.value target)))))))))))
+
+(deftest genclass-option-validation
+ (is (fails-with-cause? IllegalArgumentException #"Not a valid method name: has-hyphen"
+ (@#'clojure.core/validate-generate-class-options {:methods '[[fine [] void] [has-hyphen [] void]]}))))
View
12 test/clojure/test_clojure/helpers.clj
@@ -49,3 +49,15 @@
(report {:type :fail, :message ~msg,
:expected '~form, :actual t#})))))
+
+(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))))
+
+

0 comments on commit 60d5927

Please sign in to comment.