-
Notifications
You must be signed in to change notification settings - Fork 0
/
grimple-mode.el
164 lines (144 loc) · 6.1 KB
/
grimple-mode.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
(setq grimple-font-lock-keywords
;; not necessarily exhaustive
;; not sure if while, try, etc are keywords in grimple
`((,(concat "\\b" (regexp-opt '("break" "case" "catch" "class" "do" "extends"
"final" "finally" "for" "from" "goto" "if"
"implements" "import" "new" "package" "private"
"protected" "public" "return" "specialinvoke" "static"
"staticinvoke" "switch" "synchronized" "throw"
"throws" "to" "transient" "try" "virtualinvoke" "with"
"while")) "\\b")
. font-lock-function-name-face)
;; TODO labels, strings
(,(concat "\\b" (regexp-opt '("boolean" "char" "double" "float" "int" "long"
"short" "void")) "\\b")
. font-lock-type-face)
("\\b\\(null\\)\\b" . font-lock-constant-face)
("\\blabel[[:digit:]]+\\b" . font-lock-constant-face)))
;; copied from archive-tmpdir (arc-mode.el)
(defcustom grimple-tmpdir
;; make-temp-name is safe here because we use this name
;; to create a directory.
(make-temp-name
(expand-file-name ;;(if (eq system-type 'ms-dos) "ar" "archive.tmp")
temporary-file-directory))
"Directory for temporary files made by `grimple-mode.el'."
:type 'directory
:group 'grimple)
(defcustom grimple-soot-jar
nil
"Complete path to Soot 2.5.0 jar for Grimple decompilation."
:type '(file :must-match t)
:group 'grimple)
(defcustom grimple-soot-classpath
nil
"Extra classpath for Soot Grimple decompilation."
:type '(set (const 'directory))
:group 'grimple)
(defcustom grimple-decompile-format
;;"grimp"
"jimple"
"Soot format used to de/re-compile code."
:type 'string
:group 'grimple)
(setq grimple-soot-jar
;; "/home/jbalint/sw/java-sw/soot-2.5.0.jar")
"/home/jbalint/sw/java-sw/soot-trunk-2015-04-19.jar")
(setq grimple-soot-classpath nil)
;;'("/home/jbalint/sw/stardog-2.1.3/expand"))
(put 'grimple-class-mode 'mode-class 'special)
;; Find the name of the class contained in the given file by executing
;; `javap'. `javap' will not discriminate if the file is not in the
;; correct package as determined by the classpath. In Java 6+ a
;; warning will be printed containing the complete class name +
;; package.
(defun grimple-classname-of-file (file-name)
"Find the name of the class contained in the given file"
(let* ((unqual-classname (file-name-base file-name))
(command
;; $ -> \$ to prevent var replace on inner classes
(replace-regexp-in-string "\\$" "\\\\$"
(mapconcat 'identity
`("javap" "-cp" ,(file-name-directory file-name)
,unqual-classname) " ")))
(output (shell-command-to-string command)))
(if (string-match "contains\\s-+\\([[:alnum:]$\\._]+\\)\\b" output)
(match-string 1 output)
(error (concat "Could not determine classname. Output from `javap': "
(substring output 0 100))))))
(defun grimple-rt-jar-path ()
""
(concat (getenv "JAVA_HOME") "/jre/lib/rt.jar"))
(defun grimple-base-classpath (&rest extras)
""
(mapconcat 'identity (append (list (grimple-rt-jar-path) grimple-tmpdir) grimple-soot-classpath extras) ":"))
(defun grimple-package-of-class (classname)
"Separate the package from the given fully qualified classname."
(if (string-match "\\(.*\\)\\.[[:alnum:]]+" classname)
(match-string 1 classname)))
(defun grimple-build-args (extra-classpath &rest inargs)
""
(let ((args (or inargs '())))
(append `("-jar" ,grimple-soot-jar "-cp" ,(grimple-base-classpath extra-classpath)) args)))
(defun grimple-soot-classpath-add-jardir (dir)
"Add a directory full of jars to `grimple-soot-classpath'."
(interactive)
(setq grimple-soot-classpath
(append grimple-soot-classpath
(directory-files dir t "jar$"))))
(defun grimple-decompile-class (classpath-in classname outdir)
"Decompile."
(let ((args (grimple-build-args classpath-in "-f" grimple-decompile-format
"-output-dir" outdir classname))
(buf (get-buffer-create "*soot-decompile*")))
(with-current-buffer buf
(insert (format "\n\njava soot-decompile args: %s" args)))
;; TODO would be nice to use the buf variable here instead of the buffer name
(unless (= 0 (apply 'call-process (append '("java" nil "*soot-decompile*" t) args)))
(switch-to-buffer buf)
(error "Cannot decompile"))))
(defun grimple-save-class ()
"Save (recompile) a class."
(let ((args (grimple-build-args nil "-src-prec" "J" "-output-dir" grimple-base-path grimple-classname))
(buf (get-buffer-create "*soot-recompile*")))
(write-region nil nil grimple-decompiled-file)
(with-current-buffer buf
(insert (format "\n\njava soot-recompile args: %s\n" args)))
(unless (= 0 (apply 'call-process (append '("java" nil "*soot-recompile*" t) args)))
(switch-to-buffer buf)
(error "Cannot save"))))
(defun grimple-replace-buffer-contents-with-decompiled ()
""
(grimple-decompile-class grimple-base-path grimple-classname grimple-tmpdir)
(erase-buffer)
(insert-file-contents grimple-decompiled-file)
(set-buffer-modified-p nil))
(defun grimple-setup-class-mode ()
""
(set (make-local-variable 'grimple-classname)
(grimple-classname-of-file buffer-file-name))
(set (make-local-variable 'grimple-base-path)
(let ((package-dir (replace-regexp-in-string "\\." "/" (grimple-package-of-class grimple-classname))))
(replace-regexp-in-string (concat "/" package-dir "/") "" (file-name-directory buffer-file-name))))
(set (make-local-variable 'grimple-decompiled-file)
(concat grimple-tmpdir "/" grimple-classname "." grimple-decompile-format))
(set (make-local-variable 'write-file-functions)
'grimple-save-class)
(grimple-replace-buffer-contents-with-decompiled))
(defun grimple-mode ()
""
(interactive)
(kill-all-local-variables)
(setq major-mode 'grimple-mode)
(setq mode-name "Grimple")
(if (string-match "\\.class$" buffer-file-name)
(grimple-setup-class-mode))
(auto-save-mode 0)
;(setq file-precious-flag t)
(set (make-local-variable 'font-lock-defaults)
'(grimple-font-lock-keywords nil nil nil nil))
(font-lock-mode nil)
(turn-on-font-lock))
(add-to-list 'auto-mode-alist '("\\.grimp\\(le\\)?\\'" . grimple-mode))
(add-to-list 'auto-mode-alist '("\\.class\\'" . grimple-mode))
(provide 'grimple-mode)