| 1 | (defpackage :abcl-introspect/jvm/tools/procyon |
|---|
| 2 | (:use :cl) |
|---|
| 3 | (:export |
|---|
| 4 | #:disassemble-class-bytes)) |
|---|
| 5 | (in-package :abcl-introspect/jvm/tools/procyon) |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | (defun bytes->temp-file (bytes) |
|---|
| 9 | (let* ((create-temp-file-method |
|---|
| 10 | (java:jmethod "java.io.File" "createTempFile" |
|---|
| 11 | "java.lang.String" "java.lang.String" "java.io.File")) |
|---|
| 12 | (write-method (java:jmethod "java.io.FileOutputStream" "write" "byte[]")) |
|---|
| 13 | (temp-file (java:jstatic create-temp-file-method "java.io.File" |
|---|
| 14 | (symbol-name (gensym "classfile")) ".class" java:+null+)) |
|---|
| 15 | (temp-file-output-stream (java:jnew "java.io.FileOutputStream" temp-file)) |
|---|
| 16 | (to-string-method (java:jmethod "java.lang.Object" "toString"))) |
|---|
| 17 | (java:jcall write-method temp-file-output-stream bytes) |
|---|
| 18 | (java:jcall to-string-method temp-file))) |
|---|
| 19 | |
|---|
| 20 | (defun disassemble-class-bytes (object) |
|---|
| 21 | #| |
|---|
| 22 | <https://bitbucket.org/mstrobel/procyon/wiki/Decompiler%20API> |
|---|
| 23 | |
|---|
| 24 | final DecompilerSettings settings = DecompilerSettings.javaDefaults(); |
|---|
| 25 | |
|---|
| 26 | try (final FileOutputStream stream = new FileOutputStream("path/to/file"); |
|---|
| 27 | final OutputStreamWriter writer = new OutputStreamWriter(stream)) { |
|---|
| 28 | |
|---|
| 29 | Decompiler.decompile( |
|---|
| 30 | "java/lang/String", |
|---|
| 31 | new PlainTextOutput(writer), |
|---|
| 32 | settings |
|---|
| 33 | ); |
|---|
| 34 | } |
|---|
| 35 | catch (final IOException e) { |
|---|
| 36 | // handle error |
|---|
| 37 | } |
|---|
| 38 | |# |
|---|
| 39 | (let ((decompile-method (java:jmethod "com.strobel.decompiler.Decompiler" |
|---|
| 40 | "decompile" "java.lang.String" |
|---|
| 41 | "com.strobel.decompiler.ITextOutput")) |
|---|
| 42 | (string-writer (java:jnew "java.io.StringWriter"))) |
|---|
| 43 | (java:jstatic decompile-method |
|---|
| 44 | "com.strobel.decompiler.Decompiler" |
|---|
| 45 | (bytes->temp-file object) |
|---|
| 46 | (java:jnew "com.strobel.decompiler.PlainTextOutput" string-writer)) |
|---|
| 47 | (java:jcall (java:jmethod "java.io.StringWriter" "toString") string-writer))) |
|---|
| 48 | |
|---|
| 49 | |
|---|
| 50 | |
|---|
| 51 | (eval-when (:load-toplevel :execute) |
|---|
| 52 | (pushnew `(:procyon . abcl-introspect/jvm/tools/procyon::disassemble-class-bytes) |
|---|
| 53 | sys::*disassemblers*) |
|---|
| 54 | (format cl:*load-verbose* "~&; ~a: Successfully added procyon disassembler.~%" *package*)) |
|---|