diff options
-rw-r--r-- | src/clojure/contrib/java_utils.clj | 17 | ||||
-rw-r--r-- | src/clojure/contrib/shell_out.clj | 39 |
2 files changed, 37 insertions, 19 deletions
diff --git a/src/clojure/contrib/java_utils.clj b/src/clojure/contrib/java_utils.clj index 7af4bc93..e8d93782 100644 --- a/src/clojure/contrib/java_utils.clj +++ b/src/clojure/contrib/java_utils.clj @@ -204,3 +204,20 @@ Raise an exception if any deletion fails unless silently is true." (defmethod as-url String [#^String x] (URL. x)) (defmethod as-url File [#^File x] (.toURL x)) + +(defn wall-hack-method + "Calls a private or protected method. + params is a vector of class which correspond to the arguments to the method + obj is nil for static methods, the instance object otherwise + the method name is given as a symbol or a keyword (something Named)" + [class-name method-name params obj & args] + (-> class-name (.getDeclaredMethod (name method-name) (into-array Class params)) + (doto (.setAccessible true)) + (.invoke obj (into-array Object args)))) + +(defn wall-hack-field + "Access to private or protected field." + [class-name field-name obj] + (-> class-name (.getDeclaredField (name field-name)) + (doto (.setAccessible true)) + (.get obj))) diff --git a/src/clojure/contrib/shell_out.clj b/src/clojure/contrib/shell_out.clj index 874a1999..5e0be467 100644 --- a/src/clojure/contrib/shell_out.clj +++ b/src/clojure/contrib/shell_out.clj @@ -79,6 +79,7 @@ collecting its stdout"} (map? arg) (into-array String (map (fn [[k v]] (str (as-env-key k) "=" v)) arg)) true arg)) + (defn sh "Passes the given strings to Runtime.exec() to launch a sub-process. @@ -111,26 +112,26 @@ collecting its stdout"} proc (.exec (Runtime/getRuntime) (into-array (:cmd opts)) (as-env-string (:env opts)) - (as-file (:dir opts))) - in-stream (.getInputStream proc)] - (when (:in opts) + (as-file (:dir opts)))] + (if (:in opts) (with-open [osw (OutputStreamWriter. (.getOutputStream proc))] - (.write osw (:in opts)))) - (let [stdout (.getInputStream proc) - stderr (.getErrorStream proc) - [[out err] combine-fn] - (if (= (:out opts) :bytes) - [(for [strm [stdout stderr]] - (into-array Byte/TYPE (map byte (stream-seq strm)))) - #(aconcat Byte/TYPE %1 %2)] - [(for [strm [stdout stderr]] - (apply str (map char (stream-seq - (InputStreamReader. strm (:out opts)))))) - str]) - exit-code (.waitFor proc)] - (if (:return-map opts) - {:exit exit-code :out out :err err} - (combine-fn out err))))) + (.write osw (:in opts))) + (.close (.getOutputStream proc))) + (with-open [stdout (.getInputStream proc) + stderr (.getErrorStream proc)] + (let [[[out err] combine-fn] + (if (= (:out opts) :bytes) + [(for [strm [stdout stderr]] + (into-array Byte/TYPE (map byte (stream-seq strm)))) + #(aconcat Byte/TYPE %1 %2)] + [(for [strm [stdout stderr]] + (apply str (map char (stream-seq + (InputStreamReader. strm (:out opts)))))) + str]) + exit-code (.waitFor proc)] + (if (:return-map opts) + {:exit exit-code :out out :err err} + (combine-fn out err)))))) (comment |