Skip to content

Decompile class instead of form? #9

@bsless

Description

@bsless

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions