Skip to content

Commit 19ababb

Browse files
authored
orchard.java: add *analyze-sources* dynamic var (#212)
1 parent 04593a8 commit 19ababb

File tree

4 files changed

+29
-3
lines changed

4 files changed

+29
-3
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
## master (unreleased)
44

5+
## New features
6+
7+
* `orchard.java`: add `*analyze-sources*` dynamic var.
8+
* You can bind this to `false` in order to increase performance / decrease the amount of information returned.
9+
510
## 0.19.0 (2023-11-04)
611

712
## New features

project.clj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@
8484
`add-cognitest)]}
8585

8686
;; Development tools
87-
:dev {:plugins [[cider/cider-nrepl "0.41.0"]
87+
:dev {:plugins [[cider/cider-nrepl "0.43.1"]
8888
[refactor-nrepl "3.9.0"]]
8989
:dependencies [[nrepl/nrepl "1.0.0"]
9090
[org.clojure/tools.namespace "1.4.4"]]

src/orchard/java.clj

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,12 @@
312312
;; for dynamically loaded classes:
313313
:reflector (JavaReflector. (.getClassLoader c))))
314314

315+
(def ^:dynamic *analyze-sources*
316+
"Whether to analyze .java sources in addition to reflection-gathered info.
317+
318+
Bind this to `false` in order to increase performance / decrease the amount of information returned."
319+
true)
320+
315321
(defn class-info*
316322
"For the class symbol, return Java class and member info. Members are indexed
317323
first by name, and then by argument types to list all overloads."
@@ -321,7 +327,8 @@
321327
(catch LinkageError _))]
322328
(let [package (some-> c package symbol)
323329
{:keys [members] :as result} (misc/deep-merge (reflect-info (reflection-for c))
324-
(source-info class)
330+
(when *analyze-sources*
331+
(source-info class))
325332
{:name (-> c .getSimpleName symbol)
326333
:class (-> c .getName symbol)
327334
:package package
@@ -378,7 +385,11 @@
378385
info (if (and cached stale)
379386
(class-info* class)
380387
info)]
381-
(when (or (not cached) stale)
388+
(when (and
389+
;; Only cache full values that possibly were slowly computed.
390+
;; It would be a mistake to cache the fast values, letting them shadow a full computation:
391+
*analyze-sources*
392+
(or (not cached) stale))
382393
(.put cache class {:info info, :last-modified last-modified}))
383394
info))
384395

test/orchard/java_test.clj

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,3 +528,13 @@
528528
(is (not (string/includes? s "^function.Function java.util.function.Function")))
529529
(is (not (string/includes? s "^java.util.function.Function java.util.function.Function")))
530530
(assert (is (not (string/includes? s "java.lang"))))))))))
531+
532+
(when (and util/has-enriched-classpath?
533+
@@sut/parser-next-available?)
534+
(deftest *analyze-sources*-test
535+
(with-redefs [cache (LruMap. 100)]
536+
(binding [sut/*analyze-sources* false]
537+
(is (nil? (:doc (sut/resolve-symbol 'user `Thread/activeCount)))
538+
"Binding this var to `false` results in source info being omitted"))
539+
(is (seq (:doc (sut/resolve-symbol 'user `Thread/activeCount)))
540+
"Subsequent calls aren't affected, since there's no caching interference"))))

0 commit comments

Comments
 (0)