clj-java-decompiler
clj-java-decompiler copied to clipboard
Decompile class instead of form?
Hello again :)
I've been wondering if it's possible to decompile a class given the class object so I started experimenting.
Flow seems pretty straightforward: find the .class
inside the jar, extract, and proceed from there.
However, I hit a NPE for resolve-class-from-file
. I thought I'd float this before I go digging, maybe I just missed something fundamental.
See my additions below:
(defn- find-class-resource
[^Class clazz]
(.getResource (.getClassLoader clazz) (str (.replace ^String (.getName clazz) "." "/") ".class")))
(defn- extract-class-resource
[^Class clazz]
(let [^java.net.URL url (find-class-resource clazz)
name (.getSimpleName clazz)
path (str (.getPath tmp-dir) "/" name ".class")]
(with-open [in (io/reader url)
out (io/writer path)]
(io/copy in out))))
,,,
(defn decompile-class
[options clazz]
(try
(extract-class-resource clazz)
(run! #(decompile-classfile % options) (list-compiled-classes))
(catch Exception ex (throw ex))
(finally (cleanup-tmp-dir))))
Hello Ben! Do you have a particular usecase in mind? Absolute most Clojure libraries are distributed with sources only, no AOT (so no classes in the JAR). Do you want to use it just for Clojure's own functions (which happen to be AOT-compiled)?
I've been thinking about the cases where we interop with Java code. Most JARs aren't distributed with source but it would be nice to be able to peek in a class to see what goes on. IntelliJ, for example, allows you to jump into a Java class seamlessly.