Skip to content

Commit 1007408

Browse files
vemvbbatsov
authored andcommitted
Address most reflection warnings
1 parent 461cbc2 commit 1007408

File tree

17 files changed

+183
-163
lines changed

17 files changed

+183
-163
lines changed

project.clj

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,14 @@
140140
{:dependencies [[clj-kondo "2021.03.31"]]}]
141141

142142
:eastwood [:test
143-
{:plugins [[jonase/eastwood "0.4.0"]]
143+
{:plugins [[jonase/eastwood "0.9.2"]]
144144
:eastwood {:config-files ["eastwood.clj"]
145145
:exclude-namespaces [cider.nrepl.middleware.test-filter-tests]
146146
:ignored-faults {:unused-ret-vals-in-try {cider.nrepl.middleware.profile-test [{:line 25}]}
147+
;; This usage of `proxy` can't avoid reflection warnings given that the `proxy` construct dispatches based on name only:
148+
:reflection {cider.nrepl.middleware.out [{:line 55}
149+
{:line 57}
150+
{:line 59}
151+
{:line 61}
152+
{:line 65}]}
147153
:suspicious-test {cider.nrepl.middleware.profile-test [{:line 25}]}}}}]})

src/cider/nrepl/middleware/out.clj

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@
1818

1919
(declare unsubscribe-session)
2020

21-
(def original-output
21+
(defn original-output
2222
"Store the values of the original output streams so we can refer to them."
23-
{:out *out*
24-
:err *err*})
23+
^PrintWriter
24+
[k]
25+
({:out *out*
26+
:err *err*} k))
2527

2628
(defmacro with-out-binding
2729
"Run body with v bound to the output stream of each msg in msg-seq.

src/cider/nrepl/print_method.clj

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,13 @@
2323
(defmacro def-print-method [dispatch-val arg & strings]
2424
`(defmethod print-method ~dispatch-val [~arg ~'^Writer w]
2525
(if *pretty-objects*
26-
(do ~@(map #(list '.write 'w %) strings))
26+
(do ~@(map #(list '.write
27+
(with-meta 'w {:tag `Writer})
28+
%)
29+
strings))
2730
(#'clojure.core/print-object ~arg ~'w))))
2831

29-
(defn- translate-class-name [c]
32+
(defn- translate-class-name ^String [c]
3033
(main/demunge (.getName (class c))))
3134

3235
;;; Atoms
@@ -55,11 +58,14 @@
5558
(.setAccessible field false))
5659
name)))
5760

58-
(def-print-method MultiFn c
59-
"#multifn["
61+
(defn multifn-name-or-translated-name ^String [c]
6062
(try (multifn-name c)
6163
(catch SecurityException _
62-
(translate-class-name c)))
64+
(translate-class-name c))))
65+
66+
(def-print-method MultiFn c
67+
"#multifn["
68+
(multifn-name-or-translated-name c)
6369
;; MultiFn names are not unique so we keep the identity HashCode to
6470
;; make sure it's unique.
6571
(format " 0x%x]" (System/identityHashCode c)))
@@ -72,7 +78,7 @@
7278
"]")
7379

7480
;;; Agents, futures, delays, promises, etc
75-
(defn- deref-name [c]
81+
(defn- deref-name ^String [c]
7682
(let [class-name (translate-class-name c)]
7783
(if-let [[_ ^String short-name] (re-find #"^clojure\.lang\.([^.]+)" class-name)]
7884
(.toLowerCase short-name)

test/clj/cider/nrepl/middleware/apropos_test.clj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
(is (= (:status response) #{"done"}))
6969
(is (= (:type match) "macro"))
7070
(is (= (:name match) "clojure.core/->"))
71-
(is (.startsWith (:doc match) (str/capitalize doc-query)))))))
71+
(is (-> match ^String (:doc) (.startsWith (str/capitalize doc-query))))))))
7272

7373
(deftest error-handling-test
7474
(testing "Handles a fake error done via mocked function"
@@ -77,11 +77,11 @@
7777
(let [response (session/message {:op "apropos" :query "doesn't matter"})]
7878
(is (= (:status response) #{"apropos-error" "done"}))
7979
(is (= (:ex response) "class java.lang.Exception"))
80-
(is (.startsWith (:err response) "java.lang.Exception: boom"))
80+
(is (-> response ^String (:err) (.startsWith "java.lang.Exception: boom")))
8181
(is (:pp-stacktrace response)))))
8282

8383
(testing "Handles a real error caused by an improper regular expression"
8484
(let [response (session/message {:op "apropos" :query "*illegal"})]
8585
(is (= (:status response) #{"apropos-error" "done"}))
86-
(is (.startsWith (:err response) "java.util.regex.PatternSyntaxException: Dangling"))
86+
(is (-> response ^String (:err) (.startsWith "java.util.regex.PatternSyntaxException: Dangling")))
8787
(is (:pp-stacktrace response)))))

test/clj/cider/nrepl/middleware/classpath_test.clj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
(with-redefs [classpath-reply (fn [_] (throw (Exception. "cp error")))]
1919
(let [response (session/message {:op "classpath"})]
2020
(is (= (:status response) #{"done" "classpath-error"}))
21-
(is (.startsWith (:err response) "java.lang.Exception: cp error"))
21+
(is (-> response ^String (:err) (.startsWith "java.lang.Exception: cp error")))
2222
(is (= (:ex response) "class java.lang.Exception"))
2323
(is (:pp-stacktrace response)))))
2424

test/clj/cider/nrepl/middleware/complete_test.clj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
(testing "basic usage"
6767
(let [response (session/message {:op "complete-doc" :sym "true?"})]
6868
(is (= (:status response) #{"done"}))
69-
(is (.startsWith (:completion-doc response) "clojure.core/true?\n([x")))))
69+
(is (-> response ^String (:completion-doc) (.startsWith "clojure.core/true?\n([x"))))))
7070

7171
(deftest complete-flush-caches-test
7272
(testing "basic usage"
@@ -79,13 +79,13 @@
7979
(let [response (session/message {:op "complete" :ns "doesn't matter" :prefix "fake"})]
8080
(is (= (:ex response) "class java.lang.Exception"))
8181
(is (= (:status response) #{"complete-error" "done"}))
82-
(is (.startsWith (:err response) "java.lang.Exception: complete-exc"))
82+
(is (-> response ^String (:err) (.startsWith "java.lang.Exception: complete-exc")))
8383
(is (:pp-stacktrace response)))))
8484

8585
(testing "complete-doc op error handling"
8686
(with-redefs [c/completion-doc (fn [& _] (throw (Exception. "complete-doc-exc")))]
8787
(let [response (session/message {:op "complete-doc" :sym "doesn't matter"})]
8888
(is (= (:ex response) "class java.lang.Exception"))
8989
(is (= (:status response) #{"complete-doc-error" "done"}))
90-
(is (.startsWith (:err response) "java.lang.Exception: complete-doc-exc"))
90+
(is (-> response ^String (:err) (.startsWith "java.lang.Exception: complete-doc-exc")))
9191
(is (:pp-stacktrace response))))))

test/clj/cider/nrepl/middleware/debug_integration_test.clj

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@
4848
(defn with-nrepl-session*
4949
"Run the given function with *transport* and *session-id* bound."
5050
[f]
51-
(with-open [server (nrepl.server/start-server :handler (debug-handler))
51+
(with-open [^nrepl.server.Server
52+
server (nrepl.server/start-server :handler (debug-handler))
53+
^nrepl.transport.FnTransport
5254
transport (nrepl/connect :port (:port server))]
5355
;; Create a session by sending the "clone" op
5456
(transport/send transport {:op "clone" :id (next-id)})
@@ -61,7 +63,7 @@
6163

6264
;;; Helpers for initiating a new debugger session.
6365

64-
(def ^:dynamic *debugger-key*
66+
(def ^:dynamic ^LinkedBlockingQueue *debugger-key*
6567
"Queue of :key values received in :debug-value messages."
6668
nil)
6769

@@ -584,6 +586,7 @@
584586
:debug-value "\"bar\""
585587
:coor [3 1 1]
586588
:locals [["x" "\"bar\""]]})
589+
^String
587590
file (:file msg)]
588591
(is (.endsWith file "/cider/nrepl/middleware/debug_integration_test/fn.clj"))
589592
(is (.startsWith file "file:/"))
@@ -653,7 +656,8 @@
653656
(--> :in)
654657

655658
(let [msg (<-- {:debug-value "{}"
656-
:coor [3 1 1 1]})
659+
:coor [3 1 1 1]})
660+
^String
657661
file (:file msg)]
658662
(.startsWith file "jar:file:")
659663
(.endsWith file "/nrepl/server.clj"))

test/clj/cider/nrepl/middleware/format_test.clj

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -84,24 +84,24 @@
8484
(is (= "(foo/bar 1\n 2)" (:formatted-code alias-map-reply)))))
8585

8686
(testing "format-code op error handling"
87-
(let [{:keys [status err ex]} (session/message {:op "format-code"
88-
:code "*/*/*!~v"})]
87+
(let [{:keys [status ^String err ex]} (session/message {:op "format-code"
88+
:code "*/*/*!~v"})]
8989
(is (= #{"format-code-error" "done"} status))
9090
(is (.startsWith err "clojure.lang.ExceptionInfo: Invalid"))
9191
(is (= ex "class clojure.lang.ExceptionInfo"))))
9292

9393
(testing "format-code returns an error if indents option is invalid"
94-
(let [{:keys [status err ex] :as reply} (session/message {:op "format-code"
95-
:code "(+ 1 2 3)"
96-
:options {"indents" "INVALID"}})]
94+
(let [{:keys [status ^String err ex] :as reply} (session/message {:op "format-code"
95+
:code "(+ 1 2 3)"
96+
:options {"indents" "INVALID"}})]
9797
(is (= #{"format-code-error" "done"} status))
9898
(is (.startsWith err "java.lang.IllegalArgumentException:"))
9999
(is (= ex "class java.lang.IllegalArgumentException"))))
100100

101101
(testing "format-code returns an error if alias-map option is invalid"
102-
(let [{:keys [status err ex] :as reply} (session/message {:op "format-code"
103-
:code "(+ 1 2 3)"
104-
:options {"alias-map" "INVALID"}})]
102+
(let [{:keys [status ^String err ex] :as reply} (session/message {:op "format-code"
103+
:code "(+ 1 2 3)"
104+
:options {"alias-map" "INVALID"}})]
105105
(is (= #{"format-code-error" "done"} status))
106106
(is (.startsWith err "java.lang.IllegalArgumentException:"))
107107
(is (= ex "class java.lang.IllegalArgumentException")))))
@@ -120,8 +120,8 @@
120120
(is (= #{"done"} status))))
121121

122122
(testing "format-edn returns an error if the given EDN is malformed"
123-
(let [{:keys [err status] :as response} (session/message {:op "format-edn"
124-
:edn unmatched-delimiter-edn-sample})]
123+
(let [{:keys [^String err status] :as response} (session/message {:op "format-edn"
124+
:edn unmatched-delimiter-edn-sample})]
125125
(is (= #{"format-edn-error" "done"} status))
126126
(is (.startsWith err "clojure.lang.ExceptionInfo: Unmatched delimiter"))
127127
(is (:pp-stacktrace response))))

test/clj/cider/nrepl/middleware/info_test.clj

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@
115115
(is (= (:argtypes response) []))
116116
(is (= (:returns response) "int"))
117117
(is (= (:modifiers response) "#{:public}"))
118-
(is (.startsWith (:javadoc response) "cider/nrepl/test/TestClass.html#getInt")))
118+
(is (-> response ^String (:javadoc) (.startsWith "cider/nrepl/test/TestClass.html#getInt"))))
119119

120120
(if (SystemUtils/IS_JAVA_1_8)
121121
(testing "JDK 1.8 Javadoc URL style"
@@ -142,7 +142,7 @@
142142
(is (= (:argtypes response) ["int" "int" "java.lang.String"]))
143143
(is (= (:returns response) "void"))
144144
(is (= (:modifiers response) "#{:private :static}"))
145-
(is (.startsWith (:javadoc response) "cider/nrepl/test/TestClass.html#doSomething")))
145+
(is (-> response ^String (:javadoc) (.startsWith "cider/nrepl/test/TestClass.html#doSomething"))))
146146

147147
(if (SystemUtils/IS_JAVA_1_8)
148148
(testing "JDK 1.8 Javadoc URL style"
@@ -169,7 +169,7 @@
169169
(is (= (:argtypes response) []))
170170
(is (= (:returns response) "int"))
171171
(is (= (:modifiers response) "#{:public :bridge :synthetic}"))
172-
(is (.startsWith (:javadoc response) "https://docs.oracle.com/")))
172+
(is (-> response ^String (:javadoc) (.startsWith "https://docs.oracle.com/"))))
173173

174174
(if (SystemUtils/IS_JAVA_1_8)
175175
(testing "JDK 1.8 Javadoc URL style"
@@ -191,8 +191,8 @@
191191
(is (= (:name response) "."))
192192
(is (= (:url response) "https://clojure.org/java_interop#dot"))
193193
(is (= (:special-form response) "true"))
194-
(is (.startsWith (:doc response) "The instance member form works"))
195-
(is (.startsWith (:forms-str response) "(.instanceMember instance args*)\n(.instanceMember"))))
194+
(is (-> response ^String (:doc) (.startsWith "The instance member form works")))
195+
(is (-> response ^String (:forms-str) (.startsWith "(.instanceMember instance args*)\n(.instanceMember")))))
196196

197197
(testing "get info of a clojure non-core file, located in a jar"
198198
(let [response (session/message {:op "info" :sym "resource" :ns "clojure.java.io"})]
@@ -201,8 +201,8 @@
201201
(is (= (:resource response) "clojure/java/io.clj"))
202202
(is (= (:ns response) "clojure.java.io"))
203203
(is (= (:arglists-str response) "[n]\n[n loader]"))
204-
(is (.startsWith (:doc response) "Returns the URL for a named"))
205-
(is (.startsWith (:file response) "jar:file:"))))
204+
(is (-> response ^String (:doc) (.startsWith "Returns the URL for a named")))
205+
(is (-> response ^String (:file) (.startsWith "jar:file:")))))
206206

207207
(testing "nested members"
208208
(let [response (session/message {:op "info" :ns (ns-name *ns*) :sym "toString"})
@@ -225,7 +225,7 @@
225225
(is (= (:name response) "as->"))
226226
(is (= (:arglists-str response) "[expr name & forms]"))
227227
(is (= (:macro response) "true"))
228-
(is (.startsWith (:doc response) "Binds name to expr, evaluates")))
228+
(is (-> response ^String (:doc) (.startsWith "Binds name to expr, evaluates"))))
229229
(finally
230230
(System/clearProperty "fake.class.path"))))
231231

@@ -235,6 +235,7 @@
235235
:sym "junk-protocol-client"})
236236
status (:status reply)
237237
client-name (:name reply)
238+
^String
238239
file (:file reply)
239240
protocol (:protocol reply)
240241
ns (:ns reply)]
@@ -346,44 +347,44 @@
346347
(let [response (session/message {:op "info" :class "test"})]
347348
(is (= (:status response) #{"info-error" "done"}))
348349
(is (= (:ex response) "class java.lang.Exception"))
349-
(is (.startsWith (:err response) "java.lang.Exception: Either"))
350+
(is (-> response ^String (:err) (.startsWith "java.lang.Exception: Either")))
350351
(is (:pp-stacktrace response))))
351352

352353
(testing "handle the exception thrown if no member provided to a java class eldoc query"
353354
(let [response (session/message {:op "eldoc" :class "test"})]
354355
(is (= (:status response) #{"eldoc-error" "done"}))
355356
(is (= (:ex response) "class java.lang.Exception"))
356-
(is (.startsWith (:err response) "java.lang.Exception: Either"))
357+
(is (-> response ^String (:err) (.startsWith "java.lang.Exception: Either")))
357358
(is (:pp-stacktrace response))))
358359

359360
(testing "handle the exception thrown if no class provided to a java member info query"
360361
(let [response (session/message {:op "info" :member "test"})]
361362
(is (= (:status response) #{"info-error" "done"}))
362363
(is (= (:ex response) "class java.lang.Exception"))
363-
(is (.startsWith (:err response) "java.lang.Exception: Either"))
364+
(is (-> response ^String (:err) (.startsWith "java.lang.Exception: Either")))
364365
(is (:pp-stacktrace response))))
365366

366367
(testing "handle the exception thrown if no class provided to a java member eldoc query"
367368
(let [response (session/message {:op "eldoc" :member "test"})]
368369
(is (= (:status response) #{"eldoc-error" "done"}))
369370
(is (= (:ex response) "class java.lang.Exception"))
370-
(is (.startsWith (:err response) "java.lang.Exception: Either"))
371+
(is (-> response ^String (:err) (.startsWith "java.lang.Exception: Either")))
371372
(is (:pp-stacktrace response))))
372373

373374
(testing "handle the exception thrown if there's a mocked info retrieval error"
374375
(with-redefs [info/info (fn [& _] (throw (Exception. "info-exception")))]
375376
(let [response (session/message {:op "info" :sym "test" :ns "user"})]
376377
(is (= (:status response) #{"info-error" "done"}))
377378
(is (= (:ex response) "class java.lang.Exception"))
378-
(is (.startsWith (:err response) "java.lang.Exception: info-exception"))
379+
(is (-> response ^String (:err) (.startsWith "java.lang.Exception: info-exception")))
379380
(is (:pp-stacktrace response)))))
380381

381382
(testing "handle the exception thrown if there's a mocked eldoc retreival error "
382383
(with-redefs [info/eldoc-reply (fn [& _] (throw (Exception. "eldoc-exception")))]
383384
(let [response (session/message {:op "eldoc" :sym "test" :ns "user"})]
384385
(is (= (:status response) #{"eldoc-error" "done"}))
385386
(is (= (:ex response) "class java.lang.Exception"))
386-
(is (.startsWith (:err response) "java.lang.Exception: eldoc-exception"))
387+
(is (-> response ^String (:err) (.startsWith "java.lang.Exception: eldoc-exception")))
387388
(is (:pp-stacktrace response))))))
388389

389390
;;;; eldoc datomic query

0 commit comments

Comments
 (0)