Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decompile class instead of form? #9

Open
bsless opened this issue Oct 25, 2020 · 2 comments
Open

Decompile class instead of form? #9

bsless opened this issue Oct 25, 2020 · 2 comments

Comments

@bsless
Copy link

bsless commented Oct 25, 2020

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))))
@alexander-yakushev
Copy link
Member

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)?

@bsless
Copy link
Author

bsless commented Oct 25, 2020

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants