Skip to content

Commit

Permalink
initial type signature support
Browse files Browse the repository at this point in the history
  • Loading branch information
jgpc42 committed Apr 16, 2020
1 parent b6b187e commit cf46849
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/insn/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@
ClassWriter/COMPUTE_MAXS)

cv (doto (ClassWriter. wflags)
(.visit bversion (util/flags flags) this nil
(.visit bversion (util/flags flags) this (util/type-sig (:signature t))
super (into-array String ifaces)))

ctor? #(= "<init>" (util/method-name (:name %)))
Expand Down Expand Up @@ -199,7 +199,7 @@
"F" (float v), "D" (double v)
v))
fv (.visitField cv (util/flags flags)
(name (:name f)) ftype nil fval)]
(name (:name f)) ftype (util/type-sig (:signature f)) fval)]
(ann/visit fv (:annotations f))
(.visitEnd fv)))

Expand All @@ -223,7 +223,7 @@
(if (:desc m)
(util/method-desc (:desc m))
"()V"))
mv (.visitMethod cv (util/flags flags) mname desc nil nil)
mv (.visitMethod cv (util/flags flags) mname desc (util/type-sig (:signature m)) nil)
emit (if (fn? (:emit m))
(:emit m)
(op/compile (:emit m)))]
Expand Down
10 changes: 10 additions & 0 deletions src/insn/util.clj
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
(type [x]
"Return an ASM Type object."))

(defprotocol TypeSig
(type-sig [x]
"Return an internal type signature string."))

;;;

(def ^{:doc "The internal class name of the class being generated."
Expand Down Expand Up @@ -353,3 +357,9 @@
(type [s] (Type/getType ^String (type-desc s)))
Type
(type [t] t))

(extend-protocol TypeSig
nil
(type-sig [_])
String
(type-sig [s] s))
22 changes: 21 additions & 1 deletion test/insn/core_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
[java.lang.annotation Retention RetentionPolicy Target ElementType]
[javax.annotation.processing SupportedOptions]
[javax.xml.ws WebServiceRef WebServiceRefs]
[javax.xml.ws.soap Addressing]))
[javax.xml.ws.soap Addressing]
[java.lang.reflect ParameterizedType]))

(defn make [& {:as m}]
(core/new-instance m))
Expand Down Expand Up @@ -333,3 +334,22 @@
[:invokevirtual String "concat"]
[:areturn]]}]})]
(is (= "foo-bc" (.bar obj (.toCharArray "abcd")))))))

(deftest test-signature
(let [
;; List<String> foo
field-sig "Ljava/util/List<Ljava/lang/String;>;"

;; <T extends Iterable> List<T> bar (List<T> arg)
method-sig "<T::Ljava/lang/Iterable;>(Ljava/util/List<TT;>;)Ljava/util/List<TT;>;"

List java.util.List
klass (core/define
{:flags [:public]
:fields [{:flags [:public], :name "foo", :type List, :signature field-sig}]
:methods [{:flags [:public], :name "bar", :desc [List List] :signature method-sig
:emit [[:aload 0] [:areturn]]}]})]
(is (-> klass (.getField "foo")
^ParameterizedType (.getGenericType) .getActualTypeArguments first (= String)))
(is (= "T" (-> klass (.getMethod "bar" (into-array Class [List]))
^ParameterizedType (.getGenericReturnType) .getActualTypeArguments first str)))))

0 comments on commit cf46849

Please sign in to comment.