-
Notifications
You must be signed in to change notification settings - Fork 6
/
jdecomp.el
399 lines (316 loc) · 14.1 KB
/
jdecomp.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
;;; jdecomp.el --- Interface to Java decompilers -*- lexical-binding: t; -*-
;; Copyright (C) 2016 Tianxiang Xiong
;; Author: Tianxiang Xiong <[email protected]>
;; Keywords: decompile, java, languages, tools
;; Package-Requires: ((emacs "24.5"))
;; URL: https://github.com/xiongtx/jdecomp
;; Version: 0.2.0
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;; This file is not part of GNU Emacs.
;;; Commentary:
;; Interface to Java decompilers.
;;; Code:
(require 'arc-mode)
(require 'cl-lib)
(require 'subr-x)
;;;; Customize
(defgroup jdecomp nil
"Interface to Java decompilers."
:group 'development
:link '(url-link :tag "GitHub" "https://github.com/xiongtx/jdecomp")
:prefix "jdecomp-"
:tag "JDecomp")
(defcustom jdecomp-decompiler-type 'cfr
"Type of Java decompiler to use."
:group 'jdecomp
:type '(radio
(const :tag "CFR" 'cfr)
(const :tag "Fernflower" 'fernflower)
(const :tag "Procyon" 'procyon)))
(defcustom jdecomp-decompiler-paths nil
"Alist of Java decompiler types and their paths."
:group 'jdecomp
:options '(cfr fernflower procyon)
:type '(alist :key-type symbol :value-type (file :must-match t)))
(defcustom jdecomp-decompiler-options nil
"Alist of Java decompiler command line options."
:group 'jdecomp
:options '(cfr fernflower procyon)
:type '(alist :key-type symbol :value-type (repeat string)))
;;;; Utilities
(defvar jdecomp--jar-mime-types
'("application/java-archive"
"application/x-jar"
"application/x-java-archive"
"application/zip")
"MIME types for JAR files.")
(defvar jdecomp--preview-mode-jar-file)
(defun jdecomp--jar-p (file)
"Return t if FILE is a JAR."
(ignore-errors
(let ((type-output (with-output-to-string
(process-file "file" nil standard-output nil
"-bL" "--mime-type"
(expand-file-name file)))))
(member (string-trim type-output) jdecomp--jar-mime-types))))
(defun jdecomp--classfile-p (file)
"Return t if FILE is a Java class file."
(string= (file-name-extension file) "class"))
(defun jdecomp--java-files (dir)
"Return list of Java files in directory DIR."
(directory-files dir t "\\.java\\'"))
(defun jdecomp--make-temp-file (prefix &optional dir-flag suffix)
"Like `make-temp-file', but create parent dirs as necessary.
PREFIX, DIR-FLAG, and SUFFIX are as in `make-temp-file'."
(let ((umask (default-file-modes))
file)
(unwind-protect
(progn
;; Create temp files with strict access rights. It's easy to
;; loosen them later, whereas it's impossible to close the
;; time-window of loose permissions otherwise.
(set-default-file-modes ?\700)
(while (condition-case ()
(progn
(setq file
(make-temp-name
(if (zerop (length prefix))
(file-name-as-directory
temporary-file-directory)
(expand-file-name prefix
temporary-file-directory))))
(if suffix
(setq file (concat file suffix)))
(if dir-flag
;; All `make-temp-file' is missing here is a `t'
(make-directory file t)
(write-region "" nil file nil 'silent nil 'excl))
nil)
(file-already-exists t))
;; the file was somehow created by someone else between
;; `make-temp-name' and `write-region', let's try again.
nil)
file)
;; Reset the umask.
(set-default-file-modes umask))))
;; From: http://emacs.stackexchange.com/a/3843/10269
(defun jdecomp---extract-to-file (jar file)
"Return path of extracted FILE.
FILE is extracted from JAR using COMMAND. The extracted file is
saved to a temp dir."
(let* ((command archive-zip-extract)
(output-dir (jdecomp--make-temp-file (concat "jdecomp" "/" (file-name-sans-extension file)) t))
(output-file (expand-file-name (file-name-nondirectory file) output-dir)))
(apply #'call-process
(car command) ;program
nil ;infile
`(:file ,output-file) ;destination
nil ;display
(append (cdr command) (list jar file)))
output-file))
;;;; Internal
(defun jdecomp--decompiled-buffer-name (file)
"Return the buffer name of decompiled FILE."
(format "*Decompiled %s (%s)*" (file-name-nondirectory file) jdecomp-decompiler-type))
(defun jdecomp--decompiler-path (decompiler-type)
"Return path for DECOMPILER-TYPE from `jdecomp-decompiler-paths'."
(assoc-default decompiler-type jdecomp-decompiler-paths))
(defun jdecomp--decompiler-options (decompiler-type)
"Return list of normalized options for DECOMPILER-TYPE.
Normalization example:
(\"--foo-opt foo\" \"--bar-opt\" \"bar\")
=> (\"--foo-opt\" \"foo\" \"--bar-opt\" \"bar\")"
(let ((options (assoc-default decompiler-type jdecomp-decompiler-options)))
(cl-remove-if #'string-empty-p (split-string (string-join options " ") " "))))
(cl-defun jdecomp--decompile-command (&optional (decompiler-type jdecomp-decompiler-type))
"Return the decompile command.
Optional parameter DECOMPILER-TYPE defaults to
`jdecomp-decompiler-type'."
(condition-case nil
(cl-ecase decompiler-type
('cfr #'jdecomp--cfr-command)
('fernflower #'jdecomp--fernflower-command)
('procyon #'jdecomp--procyon-command))
(error (user-error "%s is not a known decompiler" decompiler-type))))
(cl-defun jdecomp--ensure-decompiler (&optional (decompiler-type jdecomp-decompiler-type))
"Ensure that the decompiler for DECOMPILER-TYPE is available.
Optional parameter DECOMPILER-TYPE defaults to
`jdecomp-decompiler-type'."
(unless (condition-case nil
(cl-ecase decompiler-type
('cfr (jdecomp--jar-p (jdecomp--decompiler-path 'cfr)))
('fernflower (jdecomp--jar-p (jdecomp--decompiler-path 'fernflower)))
('procyon (jdecomp--jar-p (jdecomp--decompiler-path 'procyon))))
(error (user-error "%s is not a known decompiler" decompiler-type)))
(user-error "%s decompiler is not available" decompiler-type)))
(defun jdecomp--cfr-command (file &optional jar)
"Decompile FILE with CFR and return result as string.
FILE must be a Java classfile.
Optional parameter JAR is the JAR file containing FILE, if
applicable."
(jdecomp--ensure-decompiler 'cfr)
(with-output-to-string
(let ((classpath (or jar (file-name-directory file) default-directory)))
(apply #'call-process "java" nil standard-output nil
`("-jar" ,(expand-file-name (jdecomp--decompiler-path 'cfr))
"--extraclasspath" ,classpath
,@(jdecomp--decompiler-options 'cfr)
,file)))))
(defun jdecomp--fernflower-decompile-file (file &optional extracted-p)
"Decompile FILE with Fernflower and return result as string.
FILE must be a Java classfile.
Optional parameter EXTRACTED-P, when non-nil, indicates that FILE
was extracted from a JAR with `jdecomp--extract-to-file'."
(jdecomp--ensure-decompiler 'fernflower)
(with-temp-buffer
(let* ((classpath (or (file-name-directory file) default-directory))
(destination (if extracted-p
(file-name-directory file)
(jdecomp--make-temp-file (concat "jdecomp" (file-name-sans-extension file)) t))))
;; The java-decompiler.jar is not executable
;; See: http://stackoverflow.com/a/39868281/864684
(apply #'call-process "java" nil nil nil
`("-cp" ,(expand-file-name (jdecomp--decompiler-path 'fernflower))
"org.jetbrains.java.decompiler.main.decompiler.ConsoleDecompiler"
"-cp" ,classpath
,@(jdecomp--decompiler-options 'fernflower)
,file
,destination))
(insert-file-contents (cl-first (jdecomp--java-files destination)))
(buffer-string))))
(defun jdecomp--fernflower-decompile-file-in-jar (file jar)
"Decompile FILE with Fernflower and return result as string.
FILE must be a Java classfile.
Optional parameter JAR is the JAR file containing FILE, if
applicable."
(let ((extracted-file (jdecomp---extract-to-file jar file)))
(jdecomp--fernflower-decompile-file extracted-file t)))
(defun jdecomp--fernflower-command (file &optional jar)
"Decompile FILE with Fernflower and return result as string.
FILE must be a Java classfile.
Optional parameter JAR is the JAR file containing FILE, if
applicable."
(if jar
(jdecomp--fernflower-decompile-file-in-jar file jar)
(jdecomp--fernflower-decompile-file file)))
(defun jdecomp--procyon-decompile-file (file)
"Decompile FILE with Procyon and return result as string.
FILE must be a Java classfile.
Optional parameter EXTRACTED-P, when non-nil, indicates that FILE
was extracted from a JAR with `jdecomp--extract-to-file'."
(jdecomp--ensure-decompiler 'procyon)
(with-output-to-string
(apply #'call-process "java" nil standard-output nil
`("-jar" ,(expand-file-name (jdecomp--decompiler-path 'procyon))
,@(jdecomp--decompiler-options 'procyon)
,file))))
(defun jdecomp--procyon-decompile-file-in-jar (file jar)
"Decompile FILE with Procyon and return result as string.
FILE must be a Java classfile.
Optional parameter JAR is the JAR file containing FILE, if
applicable."
(let ((extracted-file (jdecomp---extract-to-file jar file)))
(jdecomp--procyon-decompile-file extracted-file)))
(defun jdecomp--procyon-command (file &optional jar)
"Decompile FILE with and return result as string.
FILE must be a Java classfile.
Optional parameter JAR is the JAR file containing FILE, if
applicable."
(if jar
(jdecomp--procyon-decompile-file-in-jar file jar)
(jdecomp--procyon-decompile-file file)))
(defun jdecomp--preview-mode-update-modeline ()
"Update mode line for `jdecomp-preview-mode'.
Intended for use as `:after-hook' form."
(setq mode-name (format "JDecomp/%s" jdecomp-decompiler-type)))
(defun jdecomp--preview-mode-revert-buffer (_ignore-auto noconfirm)
"Function to revert buffer for `jdecomp-preview-mode'.
NOCONFIRM is as in `revert-buffer'."
(when (or noconfirm (yes-or-no-p (format "Decompile again with %s? " jdecomp-decompiler-type)))
(let ((pos (point)))
(jdecomp-decompile-and-view buffer-file-name jdecomp--preview-mode-jar-file)
(goto-char pos))))
;;;; API
(defvar jdecomp-preview-mode-map
(let ((map (copy-keymap special-mode-map)))
(set-keymap-parent map special-mode-map)
(define-key map (kbd "n") #'next-line)
(define-key map (kbd "p") #'previous-line)
map)
"Keymap for decompiled Java class file buffers.")
;;;###autoload
(define-derived-mode jdecomp-preview-mode java-mode ""
"Major mode for previewing decompiled Java class files.
\\{jdecomp-preview-mode-map}"
:after-hook (jdecomp--preview-mode-update-modeline)
(setq-local revert-buffer-function #'jdecomp--preview-mode-revert-buffer))
;;;###autoload
(defun jdecomp-decompile (file &optional jar)
"Decompile FILE and return buffer of decompiled contents.
FILE must be a Java class file.
Optional parameter JAR is the name of the JAR archive FILE is
in."
;; Check that FILE is a class file
(unless (jdecomp--classfile-p file)
(user-error (format "%s is not a Java class file" file)))
(let ((result (funcall (jdecomp--decompile-command) file jar))
(buf (get-buffer-create (jdecomp--decompiled-buffer-name file))))
(with-current-buffer buf
(let ((inhibit-read-only t))
(erase-buffer)
(insert result))
(setq buffer-read-only t)
(setq default-directory (file-name-directory (or jar file)))
(setq buffer-file-name file)
(goto-char (point-min))
(jdecomp-preview-mode)
(setq-local jdecomp--preview-mode-jar-file jar)
(set-buffer-modified-p nil))
buf))
;;;###autoload
(defun jdecomp-decompile-and-view (file &optional jar)
"Decompile FILE and view buffer of decompiled contents.
FILE must be a Java class file. If called interactively, FILE is
the name of the file the current buffer is visiting.
Optional parameter JAR is the JAR file containing FILE, if
applicable."
(interactive (list (buffer-file-name)))
(let ((buf (jdecomp-decompile file jar)))
(when buf
(switch-to-buffer buf))))
;;;; Minor mode
;;;###autoload
(define-minor-mode jdecomp-mode
"Automatically decompile Java class files."
:global t
(if jdecomp-mode
(progn
(add-hook 'find-file-hook #'jdecomp-hook-function)
(add-hook 'archive-extract-hook #'jdecomp-archive-hook-function))
(remove-hook 'find-file-hook #'jdecomp-hook-function)
(remove-hook 'archive-extract-hook #'jdecomp-archive-hook-function)))
(defun jdecomp-hook-function ()
"Hook function for decompiling classfiles."
(let ((file (buffer-file-name)))
(when (and jdecomp-mode
(jdecomp--classfile-p file))
(kill-buffer (current-buffer))
(jdecomp-decompile-and-view file))))
(defun jdecomp-archive-hook-function ()
"Hook function for decompiling extracted classfiles."
(pcase-let ((`(,jar ,file) (split-string (buffer-file-name) ":")))
(when (and jdecomp-mode
(jdecomp--classfile-p file))
(kill-buffer (current-buffer))
(jdecomp-decompile-and-view file jar))))
(provide 'jdecomp)
;;; jdecomp.el ends here