<commit_before>(in-ns 'atom-finder.util)
(import '(java.lang.instrument Instrumentation))

(defn heap-size [] (.totalMemory (Runtime/getRuntime)))
(defn free-memory [] (.freeMemory (Runtime/getRuntime)))
(defn used-memory [] (long (/ (- (heap-size) (free-memory)) (* 1024 1024))))

;(defn object-size [o] (Instrumentation/getObjectSize o))

;; print methods of java object
;; http://stackoverflow.com/questions/5821286/how-can-i-get-the-methods-of-a-java-class-from-clojure
(defn java-methods
  "list methods of java object"
  [obj]
  (->> obj
       rflct/reflect
       (:members)
       ;(filter :exception-types)
       (map #(dissoc % :exception-types))
       (map #(dissoc % :declaring-class))
       (sort-by :name)
       ))

(defn public-methods
  "list public methods of java object"
  [obj]
  (->> obj
       (java-methods)
       (filter #(:public (:flags %)))
       ))

(defn ppublic-methods
  "print public methods of java object"
  [obj]
  (let [simplify-name #(some-> % name (str/split #"\.") last)]
    (->> obj
         public-methods
         (map (partial map-values #(if (seqable? %1)
                                     (str/join " " (map simplify-name %1))
                                     (simplify-name %1))))
         print-table
         )))

; https://gist.github.com/sunng87/13700d3356d5514d35ad
(defn invoke-private-method [obj fn-name-string & args]
  (let [m (first (filter (fn [x] (.. x getName (equals fn-name-string)))
                         (.. obj getClass getDeclaredMethods)))]
    (. m (setAccessible true))
    (. m (invoke obj args))))

(defn private-field [obj fn-name-string]
  (let [m (.. obj getClass (getDeclaredField fn-name-string))]
    (. m (setAccessible true))
        (. m (get obj))))


;; https://groups.google.com/forum/#!msg/clojure/YJNRnGXLr2I/wiXZVnd3gUIJ
'(defmacro call-method "Call a method by name from a string" [inst m & args]
   `(. ~inst ~(symbol (eval m)) ~@args))

'(defn call-method "Call a method by name from a string" [obj m & args]
   (eval `(. ~obj ~(symbol m) ~@args)))

(defn call-method "Call a method by name from a string" [obj m & args]
  (clojure.lang.Reflector/invokeInstanceMethod obj m (to-array args)))

(defn write-tempfile
  [content]
  ; https://github.com/clojure-cookbook/clojure-cookbook/blob/master/04_local-io/4-10_using-temp-files.asciidoc
  (let [my-temp-file (java.io.File/createTempFile "filename" ".txt")]
    (with-open [file (clojure.java.io/writer my-temp-file)]
      (binding [*out* file]
        (print content)))

    my-temp-file))
<commit_msg>available-processors<commit_after>(in-ns 'atom-finder.util)
(import '(java.lang.instrument Instrumentation))

(defn heap-size [] (.totalMemory (Runtime/getRuntime)))
(defn free-memory [] (.freeMemory (Runtime/getRuntime)))
(defn used-memory [] (long (/ (- (heap-size) (free-memory)) (* 1024 1024))))
(defn available-processors [] (.. Runtime getRuntime availableProcessors))

;(defn object-size [o] (Instrumentation/getObjectSize o))

;; print methods of java object
;; http://stackoverflow.com/questions/5821286/how-can-i-get-the-methods-of-a-java-class-from-clojure
(defn java-methods
  "list methods of java object"
  [obj]
  (->> obj
       rflct/reflect
       (:members)
       ;(filter :exception-types)
       (map #(dissoc % :exception-types))
       (map #(dissoc % :declaring-class))
       (sort-by :name)
       ))

(defn public-methods
  "list public methods of java object"
  [obj]
  (->> obj
       (java-methods)
       (filter #(:public (:flags %)))
       ))

(defn ppublic-methods
  "print public methods of java object"
  [obj]
  (let [simplify-name #(some-> % name (str/split #"\.") last)]
    (->> obj
         public-methods
         (map (partial map-values #(if (seqable? %1)
                                     (str/join " " (map simplify-name %1))
                                     (simplify-name %1))))
         print-table
         )))

; https://gist.github.com/sunng87/13700d3356d5514d35ad
(defn invoke-private-method [obj fn-name-string & args]
  (let [m (first (filter (fn [x] (.. x getName (equals fn-name-string)))
                         (.. obj getClass getDeclaredMethods)))]
    (. m (setAccessible true))
    (. m (invoke obj args))))

(defn private-field [obj fn-name-string]
  (let [m (.. obj getClass (getDeclaredField fn-name-string))]
    (. m (setAccessible true))
        (. m (get obj))))


;; https://groups.google.com/forum/#!msg/clojure/YJNRnGXLr2I/wiXZVnd3gUIJ
'(defmacro call-method "Call a method by name from a string" [inst m & args]
   `(. ~inst ~(symbol (eval m)) ~@args))

'(defn call-method "Call a method by name from a string" [obj m & args]
   (eval `(. ~obj ~(symbol m) ~@args)))

(defn call-method "Call a method by name from a string" [obj m & args]
  (clojure.lang.Reflector/invokeInstanceMethod obj m (to-array args)))

(defn write-tempfile
  [content]
  ; https://github.com/clojure-cookbook/clojure-cookbook/blob/master/04_local-io/4-10_using-temp-files.asciidoc
  (let [my-temp-file (java.io.File/createTempFile "filename" ".txt")]
    (with-open [file (clojure.java.io/writer my-temp-file)]
      (binding [*out* file]
        (print content)))

    my-temp-file))

{'fraction_non_alphanumeric': 0.16641045349730976, 'fraction_numerical': 0.01614142966948501, 'mean_word_length': 2.7772133526850507, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 17, 'https://': 6, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2249841', 'n_tokens_mistral': 1884, 'n_tokens_neox': 1789, 'n_words': 429}