-
Notifications
You must be signed in to change notification settings - Fork 55
/
jdee-backend.el
299 lines (253 loc) · 11.1 KB
/
jdee-backend.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
;;; jdee-backend -- Facade to JVM backend
;;; Commentary:
;; This is going to be a Facade to a JVM backed.
;; Currently it's only a set of functions extracted from all over the project,
;; but towards the Facade.
;;; Code:
(require 'jdee-classpath)
(require 'beanshell)
(require 'jdee-bsh)
(defun jdee-backend-browse-class (fqn)
"Browse class FQN in backend's buffer."
(bsh-eval
(oref-default 'jdee-bsh the-bsh)
(format "browseClass(\"%s\");" fqn)))
(defun jdee-backend-explore-class (class-to-open)
"Explore class CLASS-TO-OPEN."
(bsh-eval
(oref-default 'jdee-bsh the-bsh)
(concat "exploreClass(\"" class-to-open "\");")))
(defun jdee-backend-get-ant-start-server-command (command)
"Start the Ant Server with given COMMAND."
(concat "jde.util.AntServer.start(\"" command "\");" "\n"))
(defun jdee-backend-class-exists-p (fqc)
"Return t when class FQC exists."
(jdee-jeval-r
(concat "jde.util.JdeUtilities.classExists(\"" fqc "\");")))
(defun jdee-backend-jeval-classname (fmt interface-name &optional eval-return)
"Try jdee-jeval on the command derived from (format FMT INTERFACE-NAME),
if that fails (as it will when INTERFACE-NAME is an inner-class name),
then try after replacing INTERFACE-NAME with (jdee-dollar-name INTERFACE-NAME).
If EVAL-RETURN is t, then return (jdee-jeval ... t), else return (read (jdee-jeval ...))"
(cl-flet ((jeval (name)
(if eval-return
(jdee-jeval (format fmt name) t)
(read (jdee-jeval (format fmt name))))))
(let ((code (jeval interface-name)) dollar-name)
(if (and code (eq (car code) 'error)
(setq dollar-name (jdee-dollar-name interface-name))
;; recurse as long as '.'s are changing:
(not (string-equal dollar-name interface-name)))
;; try again with dollar-name
(jdee-backend-jeval-classname fmt dollar-name eval-return)
code))))
;; To retrieve all the possible completions, it uses the java code in
;; jde.util.Completion.getClassInfo(), called by beanshell. That
;; need the class to be compiled (but that's not worst than an etag
;; call).
;; Known bugs/problems :
;; - The first call to the bsh function is bugged, and part of the
;; output is trashed. Starting the bsh before completing, or just
;; ignoring the first error and call the completion again, work fine.
;; - Due to the way the JVM works, it is not possible to explicitly
;; unload a class. So, if major changes are done in a class, the
;; beanshell must be restarted in order to reload the class.
(defun jdee-backend-get-class-info (name)
"Return the class info list for the class NAME.
Possibly it's short java name. This list contains lists of elements,
which car is a possible completion, and the cdr gives additional
informations on the car."
(bsh-eval
(oref-default 'jdee-bsh the-bsh)
(concat "jde.util.Completion.getClassInfo(\"" name "\");")))
(defun jdee-backend-get-qualified-name (unqualified-class)
"Return a list containing all qualified name for UNQUALIFIED-CLASS."
(jdee-jeval-r
(concat "jde.util.JdeUtilities.getQualifiedName(\""
unqualified-class
"\");")))
(defun jdee-backend-get-classinfo-access (name access)
"Invoke the method jde.util.Completion.getClassInfo(String NAME, int ACCESS)."
(jdee-jeval-r
(format "jde.util.Completion.getClassInfo(\"%s\",%d);" name access)))
(defun jdee-backend--get-classinfo-javacode (name import)
"Return the java code that calls the
jde.util.Completion.getClassInfo function with the short java class
name NAME and the package list IMPORT where to look at."
(save-excursion
(concat "{ "
"String[] lst = new String[" (length import) "];\n"
(let ((count -1))
(mapconcat (function (lambda (x)
(setq count (+ 1 count))
(concat "lst[" count "]=\""
(car (nth count import)) "\";\n")))
import
" "))
"jde.util.Completion.getClassInfo(\"" name "\",lst);\n"
"}")))
(defun jdee-backend-get-class-info-for-import (name import)
"Return the class info list for the class NAME and IMPORT.
Possibly it's short java name. This list contains lists of elements,
which car is a possible completion, and the cdr gives additional
informations on the car."
(bsh-eval
(oref-default 'jdee-bsh the-bsh)
(jdee-backend--get-classinfo-javacode name import)))
;; TODO unify with below
(defun jdee-backend-load-project-class-list2 ()
"Update the class list used to resolve class names."
(when (jdee-bsh-running-p)
(jdee-jeval (jdee-backend-create-prj-values-str))))
(defun jdee-backend-load-project-class-list ()
"Update the class list used to resolve class names."
(if (jdee-bsh-running-p)
(progn
(message "Rescanning classes...")
(jdee-jeval (jdee-backend-create-prj-values-str))
(jdee-jeval "jde.util.JdeUtilities.updateClassList();")
(message "Rescanning classes...Complete"))))
(defun jdee-backend-update-class-list (class-dir)
"Request JVM Backend to update list of classes in CLASS-DIR."
(jdee-jeval (concat
"jde.util.JdeUtilities.updateClassList(\""
class-dir
"\");")))
(defun jdee-build-path-arg (arg path-list &optional quote symbol)
"Build a command-line path argument from a list of paths."
(let ((path (jdee-build-classpath path-list symbol)))
(if quote
(setq path (concat "\"" path "\"")))
(setq path (concat arg " " path))))
(defun jdee-build-classpath-arg (path-list &optional quote symbol)
"Build a classpath from a list of paths."
(jdee-build-path-arg "-classpath" path-list quote symbol))
(defun jdee-backend-create-prj-values-str ()
"Create Java expression that updates the JDEE's class list
to include all the classes on `jdee-global-classpath', if
defined, otherwise the classpath specified by the CLASSPATH
environment variable."
(let* ((directory-sep-char ?/) ;; Override NT/XEmacs setting
(classpath
(jdee-build-path-arg nil (jdee-get-global-classpath) t 'jdee-global-classpath)))
(format "jde.util.JdeUtilities.setProjectValues(\"%s\", %s);"
jdee-current-project
classpath)))
(defun jdee-backend-launch ()
"Start JVM backend if it's not rurnnig."
(if (not (jdee-backend-running-p))
(bsh-launch (oref-default 'jdee-bsh the-bsh))))
(defun jdee-backend-run ()
"Start JVM backend."
(interactive)
(jdee-bsh-run))
(defun jdee-backend-exit ()
"Stop JVM backend."
(interactive)
(jdee-bsh-exit))
(defun jdee-backend-load-project ()
"Load the current project on JVM backend."
(jdee-backend-launch)
(bsh-eval (oref-default 'jdee-bsh the-bsh)
(jdee-backend-create-prj-values-str)))
(defun jdee-backend-compile (arg-array buffer)
"Compile ARG-ARRAY in BUFFER."
(jdee-backend-load-project)
(bsh-buffer-eval
(oref-default 'jdee-bsh the-bsh)
(concat (format "jde.util.CompileServer.compile(%s);" arg-array) "\n")
buffer))
(defun jdee-backend-compile-eclipse (path arg-array buffer)
"Add PATH to class path and compile ARG-ARRAY in BUFFER using Eclipse Compiler."
(jdee-backend-load-project)
(jdee-backend-add-to-class-path path)
(bsh-buffer-eval
(oref-default 'jdee-bsh the-bsh)
(concat
(format
"if ((new org.eclipse.jdt.internal.compiler.batch.Main(new java.io.PrintWriter(System.out), new java.io.PrintWriter(System.out), true, null, null)).compile(%s)) { print (\"0\");} else {print (\"1\");};"
arg-array)
"\n")
buffer))
(defun jdee-backend-add-to-class-path (path)
"Add PATH to compilation class path."
(bsh-eval (oref-default 'jdee-bsh the-bsh)
(format "addClassPath(\"%s\");" path)))
(defun jdee-backend-is-ancestor-of (ancestor-fqn fqn)
"Return t when ANCESTOR-FQN is ancestor of FQN."
(jdee-jeval-r
(format "jde.util.Completion.isAncestorOf(%S,%S);" ancestor-fqn fqn)))
(defun jdee-backend-parse-file (filename)
"Parse file FILENAME and return parse errors if any."
(jdee-jeval-r
(concat "jde.parser.ParserMain.parseFile(\"" filename "\");")))
(defun jdee-backend-get-component-type-name (array-class)
"Return name of component type for given ARRAY-CLASS."
(jdee-jeval
(concat "System.out.println( Class.forName(\""
array-class
"\").getComponentType().getName()) ;")))
(defun jdee-backend-get-abstract-class-imports ()
"Return list of imported classes."
(jdee-jeval-r
"jde.wizards.AbstractClassFactory.getImportedClasses();"))
(defun jdee-backend-get-interface-imports ()
"Return list of imported classes."
(jdee-jeval-r
"jde.wizards.InterfaceFactory.getImportedClasses();"))
(defun jdee-backend-get-event-source-imports ()
"Return list of imported classes."
(jdee-jeval-r
"jde.wizards.EventSourceFactory.getImportedClasses();"))
(defun jdee-backend-get-delegate-imports ()
"Return list of imported classes."
(jdee-jeval-r
"jde.wizards.DelegateFactory.getImportedClasses();"))
(defun jdee-backend-get-method-override-imports ()
"Return list of imported classes."
(jdee-jeval-r
"jde.wizards.MethodOverrideFactory.getImportedClasses();"))
(defun jdee-backend-maket-abstract-class-expr (class-name)
"Return abstract class expression for CLASS-NAME."
(jdee-backend-jeval-classname
"jde.wizards.AbstractClassFactory.makeAbstractClassExpression(\"%s\", true);"
class-name))
(defun jdee-backend-make-interface-expr (interface-name)
"Return interface expression for interface INTERFACE-NAME."
(jdee-backend-jeval-classname
"jde.wizards.InterfaceFactory.makeInterfaceExpression(\"%s\",true);"
interface-name))
(defun jdee-backend-make-event-source-expr (interface-name)
"Return event source expression for interface INTERFACE-NAME."
(jdee-backend-jeval-classname
"jde.wizards.EventSourceFactory.makeEventSourceSupportExpression(\"%s\", true);"
interface-name))
(defun jdee-backend-make-method-skeleton-expr (variant)
"Return method skeletor expression for VARIANT."
(jdee-jeval-r
(concat
"jde.wizards.MethodOverrideFactory.getMethodSkeletonExpression("
variant ");")))
(defun jdee-backend-get-candidate-signatures (qualified-name method-name)
"Return candidate signatures for QUALIFIED-NAME and METHOD-NAME."
(let ((fmt (concat
"jde.wizards.MethodOverrideFactory.getCandidateSignatures"
"(\"%s\",\"" method-name "\");")))
(jdee-backend-jeval-classname fmt qualified-name t)))
(defun jdee-backend-make-delegator-methods (delegee class-name)
"Return delegator methods to DELEGEE of CLASS-NAME."
(let ((fmt (concat
"jde.wizards.DelegateFactory.makeDelegatorMethods(\""
delegee "\", \"%s\", true);")))
(jdee-backend-jeval-classname fmt class-name)))
(defun jdee-backend-running-p ()
"Return t if JDEE backend is running."
(jdee-bsh-running-p))
(defun jdee-backend-get-java-version ()
"Return backend's Java version."
(jdee-jeval-r "jde.util.JdeUtilities.getJavaVersion();"))
(defun jdee-backend-get-process ()
"Return process of the JVM backend."
(bsh-get-process (oref-default 'jdee-bsh the-bsh)))
(provide 'jdee-backend)
;;; jdee-backend.el ends here