Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

add wall-hack-method and wall-hack-field. fixes #43

Signed-off-by: Chouser <chouser@n01se.net>
  • Loading branch information...
commit cc4e2ec2bf558f059330ebc97a031d7806a1e364 1 parent 12e935b
@hiredman hiredman authored Chouser committed
Showing with 37 additions and 19 deletions.
  1. +17 −0 src/clojure/contrib/java_utils.clj
  2. +20 −19 src/clojure/contrib/shell_out.clj
View
17 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)))
View
39 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
Please sign in to comment.
Something went wrong with that request. Please try again.