├── README.md ├── project.clj └── src └── wall └── hack.clj /README.md: -------------------------------------------------------------------------------- 1 | Introduction 2 | ============ 3 | A library for bypassing private & protect fields & methods on java classes. Extracted from old contrib. Derived from work by hiredman. 4 | 5 | 6 | Installation 7 | ============ 8 | ```clojure 9 | [clj-wallhack "1.0.1"] 10 | ``` 11 | 12 | Usage 13 | ===== 14 | 15 | Getting a field 16 | --------------- 17 | ```clojure 18 | (wall.hack/field foo.bar :field obj) 19 | ``` 20 | This returns the private/protected field named "field", declared in class "foo.bar" on instance obj. obj is an instanceof (or descendant of) class foo.bar. field can be anything named (a string, symbol or keyword). 21 | 22 | Calling a method 23 | ---------------- 24 | ```clojure 25 | (wall.hack/method foo.bar :aMethodCall [Integer Double] obj 3 5.0) 26 | ``` 27 | This calls the private/protected method "aMethodCall" on obj. The method is declared in class foo.bar. obj is an instance of, or descendant of class foo.bar. the call takes a seq of classes that correspond to the method's signature, and an equal number of &rest arguments. 28 | 29 | Pass nil instead of obj for static method calls. 30 | -------------------------------------------------------------------------------- /project.clj: -------------------------------------------------------------------------------- 1 | (defproject clj-wallhack "1.0.1" 2 | :description "a library for bypassing private/protected fields and methods on Java classes") 3 | -------------------------------------------------------------------------------- /src/wall/hack.clj: -------------------------------------------------------------------------------- 1 | ; Copyright (c) Stuart Halloway & Contributors, April 2009. All rights reserved. 2 | ; The use and distribution terms for this software are covered by the 3 | ; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) 4 | ; which can be found in the file epl-v10.html at the root of this distribution. 5 | ; By using this software in any fashion, you are agreeing to be bound by 6 | ; the terms of this license. 7 | ; You must not remove this notice, or any other, from this software. 8 | 9 | ;; 10 | ;; This library is extracted from 'old' clojure contrib. These fns appear to have been written by hiredman. 11 | ;; https://github.com/richhickey/clojure-contrib/commit/cc4e2ec2bf558f059330ebc97a031d7806a1e364 12 | 13 | (ns wall.hack) 14 | 15 | (defn method 16 | "Calls a private or protected method. 17 | 18 | class - the class where the method is declared 19 | params - a vector of Class which correspond to the arguments to the method 20 | obj - nil for static methods, the instance object otherwise 21 | method-name - something Named" 22 | [class method-name params obj & args] 23 | (-> class (.getDeclaredMethod (name method-name) (into-array Class params)) 24 | (doto (.setAccessible true)) 25 | (.invoke obj (into-array Object args)))) 26 | 27 | (defn field 28 | "Access to private or protected field. field-name must be something Named 29 | 30 | class - the class where the field is declared 31 | field-name - Named 32 | obj - the instance object, or a Class for static fields" 33 | [class field-name obj] 34 | (-> class (.getDeclaredField (name field-name)) 35 | (doto (.setAccessible true)) 36 | (.get obj))) 37 | 38 | (defn mirror 39 | "returns an object that if used like (:foo returned-obj) the value 40 | of the foo field on the original object will be returned, even if 41 | the field is private. 42 | 43 | (:original/object returned-obj) will return the obj that was passed in" 44 | [obj] 45 | (reify 46 | clojure.lang.ILookup 47 | (valAt [this k] 48 | (.valAt this k nil)) 49 | (valAt [this k not-found] 50 | (if (= k :original/object) 51 | obj 52 | (try 53 | (field (class obj) k obj) 54 | (catch Exception _ 55 | not-found)))))) 56 | --------------------------------------------------------------------------------