-
Notifications
You must be signed in to change notification settings - Fork 55
/
jdee-gen.el
4127 lines (3654 loc) · 137 KB
/
jdee-gen.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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; jdee-gen.el -- JDEE code templates
;; Author: Paul Kinnucan <paulk@mathworks.com>
;; Keywords: java, tools
;; Copyright (C) 1997, 1998, 2000, 2001, 2002, 2003, 2004 Paul Kinnucan.
;; Copyright (C) 2009 by Paul Landes
;; GNU Emacs 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 2, or (at your option)
;; any later version.
;; Gnu Emacs 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 GNU Emacs; see the file COPYING. If not, write to the
;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.
;;
;;; Commentary:
;;; Code:
(require 'tempo)
(require 'jdee-classpath)
;; FIXME: (require 'cc-cmds) doesn't work
(declare-function c-indent-line "cc-cmds" (&optional syntax quiet ignore-point-pos))
(declare-function c-indent-exp "cc-cmds" (&optional shutup-p))
(declare-function c-indent-command "cc-cmds" (&optional arg))
;; FIXME: refactor
(declare-function jdee-import-find-and-import "jdee-import" (class &optional no-errors no-exclude qualifiedp))
(declare-function jdee-package-convert-directory-to-package "jdee-package" (dir))
(declare-function jdee-package-get-package-directory "jdee-package" ())
(declare-function jdee-wiz-get-name "jdee-wiz" (variable))
(declare-function jdee-wiz-implement-interface-internal "jdee-wiz" (interface-name))
(defvar jdee-package-unknown-package-name)
;; Allow tempo to understand ~ as destination of point
;; http://www.emacswiki.org/emacs/TempoMode#toc3
(defvar tempo-initial-pos nil
"Initial position in template after expansion")
(defadvice tempo-insert( around tempo-insert-pos act )
"Define initial position."
(if (eq element '~)
(setq tempo-initial-pos (point-marker))
ad-do-it))
(defadvice tempo-insert-template( around tempo-insert-template-pos act )
"Set initial position when defined using ~."
(setq tempo-initial-pos nil)
ad-do-it
(if tempo-initial-pos
(progn
(put template 'no-self-insert t)
(goto-char tempo-initial-pos))
(put template 'no-self-insert nil)))
(defgroup jdee-gen nil
"JDE Autocoder"
:group 'jdee
:prefix "jdee-gen-")
(defcustom jdee-gen-k&r t
"*If non-nil, use braces in Original Kernighan & Ritchie Style.
The Creators of C started using brace placement style:
class Some {
}
But there is also the alternative line-up style:
class Some
{
}
Setting this variable to t, uses K&R style in skeletons and tempaltes.
Note: According to the Java Code Convention [section 6.4], this value should
be non-nil."
:group 'jdee-gen
:type 'boolean)
(defcustom jdee-gen-create-javadoc t
"*If non-nil, generate Javadoc for all jdee-gen-* code generation functions."
:group 'jdee-gen
:type 'boolean)
(defcustom jdee-gen-test-path "src/test/java"
"The directory within a project where test code is stored. For Maven
and Gradle \"src/test/java\" is standard path."
:group 'jdee-gen
:type 'string)
(defcustom jdee-gen-space-after-castings t
"*If non-nil, add space between a class casting and what comes after it."
:group 'jdee-gen
:type 'boolean)
; There must be some cleverer way to do this ...
(defun jdee-gen-delete-preceding-whitespace ()
"Delete any syntactical whitespace (including newlines)
before point."
(while (and (not (bobp))
(or (bolp)
(re-search-backward "\\s-\\=" nil t)))
(delete-char -1)))
(defun jdee-gen-extract-ids-from-params (params)
"Given a parameter lsit \"Type1 id1, Type2, id2, ...\" extract the
ids and return as \"id1, id2, ...\"."
(mapconcat
(lambda (arg)
(nth 1 (split-string
(jdee-replace-in-string
(jdee-replace-in-string arg "^[ \t\n\f\l]+" "")
"[ \t\n\f\l]+$" ""))))
(split-string params "[,]") ", "))
(defun jdee-gen-lookup-named (name)
"Lookup some saved data under the name NAME.
Returns the data if NAME was found, and nil otherwise."
(cdr (assq name tempo-named-insertions)))
(defun jdee-gen-read-template (strings)
"Converts an autocode template represented as a list
of strings to a list of Lisp objects as required by
tempo."
(let ((template-string "")
(n (length strings))
(i 0))
(while (< i n)
(setq template-string
(concat template-string (nth i strings) "\n"))
(setq i (1+ i)))
(setq template-string
(concat "'(" template-string ")"))
(eval (car (read-from-string template-string)))))
(defcustom jdee-gen-buffer-boilerplate nil
"*Lines of boilerplate text to put at the head of a buffer template."
:group 'jdee-gen
:type '(repeat (string :tag "Line")))
(defcustom jdee-gen-boilerplate-function 'jdee-gen-create-buffer-boilerplate
"*Specifes buffer boilerplate text function.
This variable specifies a function to create boilerplate text for
insertion at the head of Java source buffers generated by JDE
templates. The specified function should take no arguments and should
return a text string. The default value is
`jdee-gen-create-buffer-boilerplate', which returns the lines of text
specified by `jdee-gen-buffer-boilerplate'."
:group 'jdee-gen
:type 'function)
(defun jdee-gen-create-buffer-boilerplate ()
"This function creates buffer boilerplate from the
variable `jdee-gen-buffer-boilerplate'."
(if jdee-gen-buffer-boilerplate
(let ((bp "")
(n (length jdee-gen-buffer-boilerplate))
(i 0))
(while (< i n)
(setq bp
(concat bp (elt jdee-gen-buffer-boilerplate i) "\n"))
(setq i (1+ i)))
bp)))
(defun jdee-gen-get-extend-class ()
(let ((super-class (read-from-minibuffer "extends: ")))
(if (not (string= super-class ""))
(progn
(jdee-import-find-and-import super-class)
(concat "extends " super-class " ")))))
(defcustom jdee-gen-final-arguments t
"Specifies whether to add final modifiers to method arguments.
If this variable is t, `jdee-gen-method-signature' automatically
adds final modifiers to each method argument."
:group 'jdee-gen
:type 'boolean)
(defcustom jdee-gen-final-methods t
"Specifies whether to add final method modifier.
If this variable is t, `jdee-gen-method-signature' automatically adds
the final method modifier. This option is ignored within final classes.
Note that anonymous classes are implicitly final."
:group 'jdee-gen
:type 'boolean)
(defun jdee-gen-final-argument-modifier (method-args)
"Inserts the final modifier depending on `jdee-gen-final-arguments'."
(if jdee-gen-final-arguments
(let ((comma nil)
(arg-list (split-string method-args ",")))
(setq method-args "")
(mapc
(lambda (arg)
(if (string-match "\\<final\\>" arg)
(setq method-args (concat method-args comma arg))
(setq method-args
(concat method-args
comma
(if (string-match "^[ \t]" arg) " ")
"final"
(if (string-match "^[^ \t]" arg) " ")
arg)))
(setq comma ","))
arg-list)))
method-args)
(defun jdee-gen-final-method-modifier (method-modifiers)
"Insert final modifier according to `jdee-gen-final-methods'."
(let ((class-info (jdee-parse-get-class-modifiers)))
;; Anonymous classes are implicitly final. See:
;; Java Language specification, section 15.9.5.
;; http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html
(unless (or (member "final" class-info) ; explicit final modifier?
(save-excursion
(and class-info
(goto-char (cdar class-info))
(looking-at "new")))) ; anonymous class?
(if (and jdee-gen-final-methods
(not (string-match "final" method-modifiers))
(not (string-match "private" method-modifiers))
(not (string-match "abstract" method-modifiers)))
;; Find correct position according to
;; Java Language specification, section 8.4.3.
;; http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html
(let* ((pos (max (if (string-match "public" method-modifiers) (match-end 0) 0)
(if (string-match "protected" method-modifiers) (match-end 0) 0)
(if (string-match "static" method-modifiers) (match-end 0) 0)))
(left (substring method-modifiers 0 pos))
(right (substring method-modifiers pos)))
(setq method-modifiers (concat
left
(if (> (length left) 0) " ")
"final"
(if (and (= (length left) 0) (> (length right) 0)) " ")
right))))))
method-modifiers)
(defmacro jdee-gen-save-excursion (&rest body)
"Evaluate BODY within a let form.
Protects `tempo-marks', `tempo-named-insertions', `tempo-region-start'
and `tempo-region-stop' against modifications by invoked template.
Those tempo vars will be set to their default values.
This allows to invoke tempo-templates within tempo-templates
without having mutual interference of those templates.
Returns nil."
`(progn
(let ((tempo-marks)
(tempo-named-insertions)
(tempo-region-start (make-marker))
(tempo-region-stop (make-marker)))
(progn ,@body))
nil))
(defun jdee-gen-electric-brace (&optional padding)
"Insert a brace according to `jdee-gen-k&r'.
Optional argument PADDING is inserted before brace
if jdee-gen-k&r is enabled."
(if jdee-gen-k&r
(if padding (insert padding))
(progn
(indent-according-to-mode)
(insert "\n")))
(insert "{")
(indent-according-to-mode)
(insert "\n")
nil)
(defun jdee-gen-indent ()
"Indent current line if it is not empty.
Removes indentation if the current line contains only whitespaces.
The purpose of this function is to avoid trailing whitespaces
in generated java code. Returns nil."
(if (save-excursion
(beginning-of-line)
(looking-at "\\s-+$"))
(replace-match ""))
(if (not (and (bolp) (eolp)))
(c-indent-line))
nil) ;; must return nil to prevent template insertion.
(defun jdee-gen-blank-lines (n &optional m)
"Ensure exactly N blank lines.
Leaves point at the beginning of the line following blank lines.
If M is non-nil, (`forward-line' M) will be called after inserting.
If point is at the beginning of a non-blank line, blank lines will appear before that line.
If point is at the end of a non-blank line, blank lines will appear after that line.
If point is in the middle of a non-blank line, this line will be split at point.
Indentation and trailing whitespace of surrounding non-blank lines will stay unchanged.
Returns nil."
(interactive "*")
(if (or (bolp) (eolp)
(save-excursion
(beginning-of-line)
(looking-at "\\s-*$")))
(delete-region
(+ (point) (skip-chars-backward " \t\n") (skip-chars-forward " \t"))
(+ (point) (skip-chars-forward " \t\n") (skip-chars-backward " \t"))))
(while (>= n 0)
(insert "\n")
(setq n (1- n)))
(if m
(forward-line m))
nil)
; In order to avoid problems with recursive tempo-template invocations
; interface generation has been split in two parts.
; jdee-gen-get-interface-implementation prompts for the
; interface and stores point-marker and interface name in buffer local
; variables. jdee-gen-insert-interface-implementation is invoked after
; the template for class or inner class and generates the interface
; implementation.
(defvar jdee-gen-interface-to-gen nil
"Insertion marker and name (MARKER . NAME) of interface
implemented by the most recently generated class template.
Used by `jdee-gen-get-interface-implementation' to
store name and location of interface to be inserted
by `jdee-gen-insert-interface-implementation'.")
(defun jdee-gen-get-interface-implementation (&optional insert-immediately)
"Prompts the user to enter the name of an interface
implemented by a class being generated by a tempo template.
If the user enters a name, stores the current point as marker in
the buffer and the interface name as a cons (MARKER . NAME)
in the global variable `jdee-gen-interface-to-gen'. Otherwise
it sets this variable to nil. If INSERT-IMMEDIATELY is non-nil,
`jdee-gen-insert-interface-implementation' will be called immediately
and jdee-gen-interface-to-gen will be set to nil."
(let ((interface (read-from-minibuffer "implements: ")))
(if (not (string= interface ""))
(progn
(setq jdee-gen-interface-to-gen
(cons (point-marker) interface))
(if insert-immediately
(progn
(jdee-gen-insert-interface-implementation)
(setq jdee-gen-interface-to-gen nil))))
(setq jdee-gen-interface-to-gen nil)))
nil) ;; must return nil to prevent template insertion.
(defun jdee-gen-insert-interface-implementation ()
"Generates the interface specified by the cdr of `jdee-gen-interface-to-gen'
and inserts it in the current buffer at the location specified
by the car of `jdee-gen-interface-to-gen'."
(if jdee-gen-interface-to-gen
(let ((ins-pos (marker-position (car jdee-gen-interface-to-gen)))
(interface (cdr jdee-gen-interface-to-gen)))
(save-excursion
(goto-char ins-pos)
(jdee-wiz-implement-interface-internal interface)))))
(defvar jdee-gen-package-name nil)
(defun jdee-gen-get-package-statement (&optional package)
"Return the formated package statement to insert into a java buffer.
If PACKAGE is set, use it as a default.
Ask the user for confirmation. Also sets buffer local
`jdee-gen-package-name'."
(require 'jdee-package)
(let* ((package-dir (jdee-package-get-package-directory))
(suggested-package-name
(or package
(if (or
(string= package-dir jdee-package-unknown-package-name)
(string= package-dir ""))
nil
(jdee-package-convert-directory-to-package package-dir))))
(package-name
(or jdee-gen-package-name
(read-from-minibuffer "Package: " suggested-package-name))))
(if (and
package-name
(not (string= package-name "")))
(progn
(set (make-local-variable 'jdee-gen-package-name) package-name)
(format "package %s;\n\n" package-name)))))
(defcustom jdee-gen-method-signature-padding-1 ""
"String that comes just after the function name and just before
the opening parenthesis of the argument list for a method call or definition.
For example, if set to a single space [\" \"], then a generated method might
look like:
void setXxxx () {
^
If not set, the same generated method would look like:
void setXxxx() {
^
Note: According to the Java Code Convention [section 6.4], this value should
be the empty string."
:group 'jdee-gen
:type 'string)
(defcustom jdee-gen-method-signature-padding-2 ""
"String which comes just after the opening parenthesis and just before
the closing parenthesis of an argument list for a method call or definition.
For example, if set to a single space [\" \"], then a generated method might
look like:
void setXxxx( boolean newValue ) {
^ ^
If not set, the same generated method would look like:
void setXxxx(boolean newValue) {
^ ^
Note: According to the Java Code Convention [section 6.4], this value should
be the empty string."
:group 'jdee-gen
:type 'string)
(defcustom jdee-gen-method-signature-padding-3 " "
"String which comes just after the closing parenthesis of an
argument list for a method call or definition. For example, if set to
a single space [\" \"], then a generated method might look like:
void setXxxx(boolean newValue) {
^
If not set, the same generated method would look like:
void setXxxx(boolean newValue){
^
Note: According to the Java Code Convention [section 6.4], this value should
be a single space."
:group 'jdee-gen
:type 'string)
(defcustom jdee-gen-conditional-padding-1 " "
"The string to be inserted between a conditional keyword (if, while, etc.)
and the opening parenthesis for its conditional expression:
<keyword><1>(<2><expression><2>)<3>{
also: <3>else<3>{
where <1> is jdee-gen-conditional-padding-1
and <2> is jdee-gen-conditional-padding-2
and <3> is jdee-gen-conditional-padding-3.
For example, if <1> is a space, <2> is nil and <3> is a space, then a while
clause might look like:
while (true) {
Note: According to the Java Code Convention [section 7.4], this value should
be a single space."
:group 'jdee-gen
:type 'string)
(defcustom jdee-gen-conditional-padding-2 ""
"The string to be inserted before and after a conditional expression
between the parentheses. See `jdee-gen-conditional-padding-1' for details.
Note: According to the Java Code Convention [section 7.4], this value should
be the empty string."
:group 'jdee-gen
:type 'string)
(defcustom jdee-gen-conditional-padding-3 " "
"The string to be inserted after the closing parenthesis of the
conditional expression and before the opening brace. See
`jdee-gen-conditional-padding-1' for details.
Note: According to the Java Code Convention [section 7.4], this value should
be a single space."
:group 'jdee-gen
:type 'string)
(defun jdee-gen-method-signature (access type name arglist &optional throwslist)
"This function generates method signatures with a consistent format.
All jdee-gen functions and/or templates should use this function when
creating Java methods and constructors.
The signature is of the format:
<access> <type> <name><1>(<2><arglist><2>) throws <throwslist><3>
where <1> is the value of jdee-gen-method-signature-padding-1
and <2> is the value of jdee-gen-method-signature-padding-2
and <3> is the value of jdee-gen-method-signature-padding-3.
<3> will not be used if `jdee-gen-k&r' is nil.
This function takes care of `jdee-gen-final-methods' and
`jdee-gen-final-arguments'. final modifiers will be added
according to those flags.
If a parameter to this function is empty or nil, then it is omitted,
as well as the corresponding padding, whitespace and/or Java keywords."
(if (> (length type) 0) ; ordinary method?
(setq access (jdee-gen-final-method-modifier access)))
(let ((sig
(concat
(if (> (length access) 0)
(concat access " ")
());; if no access (e.g. "public static"), then nothing
(if (> (length type) 0)
(concat type " ")
());; if no type (e.g. "boolean" or "void"), then nothing
name
jdee-gen-method-signature-padding-1
"("
(if (> (length arglist) 0)
(concat jdee-gen-method-signature-padding-2 (jdee-gen-final-argument-modifier arglist)
jdee-gen-method-signature-padding-2 )
())
")"
(if (> (length throwslist) 0)
(concat " throws " throwslist)
())
(if jdee-gen-k&r
jdee-gen-method-signature-padding-3
()))))
sig))
(defcustom jdee-gen-class-create-constructor t
"*If non-nil, generate constructor for `jdee-gen-class-buffer'."
:group 'jdee-gen
:type 'boolean)
(defcustom jdee-gen-class-create-package 'jdee-gen-get-package-statement
"*If non-nil, generate constructor for `jdee-gen-class-buffer'."
:group 'jdee-gen
:type '(choice
(const :tag "Prompt for package" jdee-gen-get-package-statement)
(const :tag "Package updates automatically" jdee-package-update)))
;;(makunbound 'jdee-gen-class-buffer-template)
(defcustom jdee-gen-class-buffer-template
(list
"(funcall jdee-gen-boilerplate-function)"
"(let ((s (funcall jdee-gen-class-create-package)))"
" (if (or (null s) (not (string-match \"\n\n$\" s)))"
" (list 'l s 'n)"
" s))"
"(when jdee-gen-create-javadoc"
"(progn (require 'jdee-javadoc) (jdee-javadoc-insert-start-block))"
"'(l \" * Describe class \""
" (file-name-sans-extension (file-name-nondirectory buffer-file-name))"
" \" here.\" 'n"
" \" \" (jdee-javadoc-insert-empty-line)"
" \" \" (jdee-javadoc-insert-empty-line)"
" \" * Created: \" (current-time-string) 'n"
" \" \" (jdee-javadoc-insert-empty-line)"
" \" \" (jdee-gen-save-excursion (jdee-javadoc-insert 'tempo-template-jdee-javadoc-author-tag))"
" \" \" (jdee-gen-save-excursion (jdee-javadoc-insert 'tempo-template-jdee-javadoc-version-tag))"
" \" \" (jdee-javadoc-insert-end-block)))"
"\"public class \""
"(file-name-sans-extension (file-name-nondirectory buffer-file-name))"
"\" \" (jdee-gen-get-extend-class)"
"(jdee-gen-electric-brace)"
"'p'n"
"\"}\">"
"(if jdee-gen-comments (concat \" // \""
" (file-name-sans-extension (file-name-nondirectory buffer-file-name))))"
"'>'n"
";; Here comes the stuff that needs a fully generated class."
";; We jump back and add those things retrospectively."
"(progn (tempo-backward-mark)"
" (jdee-gen-save-excursion"
" (jdee-gen-get-interface-implementation t))"
" (jdee-gen-save-excursion"
" (if jdee-gen-class-create-constructor"
" (jdee-wiz-gen-method \"public\" \"\""
" (file-name-sans-extension (file-name-nondirectory buffer-file-name)) \"\" \"\" \"\"))))"
";; Move to constructor body. Set tempo-marks to nil in order to prevent tempo moving to mark."
"(progn (re-search-forward \"^[ \\t]*$\") (setq tempo-marks nil) nil)"
"(when jdee-gen-class-create-constructor"
" (up-list)"
" (forward-char))")
"*Template for new Java class.
Setting this variable defines a template instantiation
command `jdee-gen-class', as a side-effect."
:group 'jdee-gen
:type '(repeat string)
:set '(lambda (sym val)
(tempo-define-template "java-class-buffer-template"
(jdee-gen-read-template val)
nil
"Insert a generic Java class buffer skeleton.")
(defalias 'jdee-gen-class
(list 'lambda (list)
(list 'interactive)
(list 'tempo-template-java-class-buffer-template)
(list 'jdee-gen-insert-interface-implementation)))
(set-default sym val)))
;;;###autoload
(defun jdee-gen-class-buffer (file)
"Create a new Java buffer containing a class of the same name.
This command inserts the class template generated by `jdee-gen-class'.
"
(interactive "F")
(find-file file)
(jdee-gen-class))
;;(makunbound 'jdee-gen-interface-buffer-template)
(defcustom jdee-gen-interface-buffer-template
(list
"(funcall jdee-gen-boilerplate-function)"
"(jdee-gen-get-package-statement)"
"(progn (require 'jdee-javadoc) (jdee-javadoc-insert-start-block))"
"\" * Describe interface \""
"(file-name-sans-extension (file-name-nondirectory buffer-file-name))"
"\" here.\" 'n"
"\" \" (jdee-javadoc-insert-empty-line)"
"\" \" (jdee-javadoc-insert-empty-line)"
"\" * Created: \" (current-time-string) 'n"
"\" \" (jdee-javadoc-insert-empty-line)"
"\" \" (jdee-gen-save-excursion (jdee-javadoc-insert 'tempo-template-jdee-javadoc-author-tag))"
"\" \" (jdee-gen-save-excursion (jdee-javadoc-insert 'tempo-template-jdee-javadoc-version-tag))"
"\" \" (jdee-javadoc-insert-end-block)"
"\"public interface \""
"(file-name-sans-extension (file-name-nondirectory buffer-file-name))"
"\" \" (jdee-gen-get-extend-class)"
"(jdee-gen-electric-brace)"
"'p'n"
"\"}\">"
"(if jdee-gen-comments (concat \" // \""
" (file-name-sans-extension (file-name-nondirectory buffer-file-name))))"
"'>'n")
"*Template for new Java interface.
Setting this variable defines a template instantiation
command `jdee-gen-interface', as a side-effect."
:group 'jdee-gen
:type '(repeat string)
:set '(lambda (sym val)
(tempo-define-template "java-interface-buffer-template"
(jdee-gen-read-template val)
nil
"Insert a generic Java interface buffer skeleton.")
(defalias 'jdee-gen-interface
(list 'lambda (list)
(list 'interactive)
(list 'tempo-template-java-interface-buffer-template)))
(set-default sym val)))
;;;###autoload
(defun jdee-gen-interface-buffer (file)
"Create a new Java buffer containing an interface of the same name.
This command inserts the interface template generated by `jdee-gen-interface'.
It then moves the point to the location of the first method."
(interactive "F")
(find-file file)
(jdee-gen-interface))
;;(makunbound 'jdee-gen-console-buffer-template)
(defcustom jdee-gen-console-buffer-template
(list
"(funcall jdee-gen-boilerplate-function)"
"(jdee-gen-get-package-statement)"
"(progn (require 'jdee-javadoc) (jdee-javadoc-insert-start-block))"
"\" * Describe class \""
"(file-name-sans-extension (file-name-nondirectory buffer-file-name))"
"\" here.\" 'n"
"\" \" (jdee-javadoc-insert-empty-line)"
"\" \" (jdee-javadoc-insert-empty-line)"
"\" * Created: \" (current-time-string) 'n"
"\" \" (jdee-javadoc-insert-empty-line)"
"\" \" (jdee-gen-save-excursion (jdee-javadoc-insert 'tempo-template-jdee-javadoc-author-tag))"
"\" \" (jdee-gen-save-excursion (jdee-javadoc-insert 'tempo-template-jdee-javadoc-version-tag))"
"\" \" (jdee-javadoc-insert-end-block)"
"\"public final class \""
"(file-name-sans-extension (file-name-nondirectory buffer-file-name))"
"(jdee-gen-electric-brace \" \")"
"'p'n"
"\"}\">"
"(if jdee-gen-comments (concat \" // \""
" (file-name-sans-extension (file-name-nondirectory buffer-file-name))))"
"'>'n"
";; Here comes the stuff that needs a fully generated class."
";; We jump back and add those things retrospectively."
"(progn (tempo-backward-mark)"
" (jdee-gen-save-excursion"
" (jdee-gen-main))"
" (tempo-backward-mark)"
" (jdee-gen-save-excursion"
" (jdee-wiz-gen-method \"private\" \"\""
" (file-name-sans-extension (file-name-nondirectory buffer-file-name)) \"\" \"\" \"\")))"
";; Move to constructor body. Set tempo-marks to nil in order to prevent tempo moving to mark."
"(progn (re-search-forward \"^[ \\t]*$\") (setq tempo-marks nil) nil)")
"*Template for new Java console app main class buffer.
Setting this variable defines a template instantiation
command `jdee-gen-console', as a side-effect."
:group 'jdee-gen
:type '(repeat string)
:set '(lambda (sym val)
(defalias 'jdee-gen-console
(tempo-define-template "java-console-buffer-template"
(jdee-gen-read-template val)
nil
"Insert skeleton for a new Java console buffer"))
(set-default sym val)))
;;;###autoload
(defun jdee-gen-console-buffer (file)
"Create a new Java buffer containing a console class of the same name.
This command inserts the class template generated by `jdee-gen-class'.
It then moves the point to the location to the constructor."
(interactive "F")
(find-file file)
(jdee-gen-console))
(defun jdee-gen-deep-clone-copies ()
(let* ((class-tag (semantic-current-tag))
(class-name (semantic-tag-name class-tag))
(members (sort (jdee-parse-get-serializable-members class-tag)
'jdee-parse-compare-member-types)))
(apply #' append '(l)
(mapcar #'(lambda (elt)
(let ((e (car elt)))
(list (format "if (%s != null) ret.%s = %s.clone();" e e e) '> 'n)))
members))))
(defcustom jdee-gen-deep-clone-catch-exception t
"*Whether or not to catch CloneNotSupportedException."
:group 'jdee-gen
:type 'boolean)
;;(makunbound 'jdee-gen-deep-clone-template)
(defcustom jdee-gen-deep-clone-template
'(
"(jdee-wiz-generate-interface \"java.lang.Cloneable\")"
"'&'> (progn (require 'jdee-javadoc) nil)"
;; create the javadoc (todo: only generate if turned on)
"(when jdee-gen-create-javadoc "
"'(l (jdee-javadoc-insert-start-block)"
"\"* Create a deep clone of this object.\" '>'n"
"\"*\" '>'n"
"\"* @return a deep clone of this object.\" '>'n"
"'> (jdee-javadoc-insert-end-block)))"
;; create method declaration
"(let (jdee-gen-final-methods)"
" (jdee-gen-method-signature"
" \"public\""
" (file-name-sans-extension (file-name-nondirectory buffer-file-name)) "
; " \"Object\""
" \"clone\""
" nil"
" ))"
"'>"
"(jdee-gen-electric-brace)"
;; return declaration
"(file-name-sans-extension (file-name-nondirectory buffer-file-name))"
"\" ret = \" "
" (if jdee-gen-deep-clone-catch-exception "
" '(l \"null;\") "
" '(l \"(\" (file-name-sans-extension"
" (file-name-nondirectory buffer-file-name))"
" \")super.clone();\")) "
" '>'n'n "
;; must catch CloneNotSupported exception
"(when jdee-gen-deep-clone-catch-exception "
"(let ((beg (point)))"
" (insert \"ret = (\")"
" (insert (file-name-sans-extension"
" (file-name-nondirectory buffer-file-name)))"
" (insert \")\") "
" (if jdee-gen-space-after-castings (insert \" \"))"
" (insert \"super.clone();\")"
" (jdee-gen-try-catch-wrapper beg (point))"
;; at this point we are at the place to add what exception to catch
" (insert \"CloneNotSupportedException\")"
" nil))"
"(when jdee-gen-deep-clone-catch-exception "
;; first go out of the catch exception ()s
" (goto-char (scan-lists (point) 1 1))"
;; now go into the catch {}s definition part
" (goto-char (scan-lists (point) 1 -1))"
" (end-of-line)"
" 'n)"
"(when jdee-gen-deep-clone-catch-exception "
"'(l \"throw new InternalError(\\\"clone should be supported (forgot?)\\\");\""
"'(l '>'%)"
"))"
"(when jdee-gen-deep-clone-catch-exception "
"'(l (progn (goto-char (scan-lists (point) 1 1)) (end-of-line) '(l n))"
"''p 'n)"
")"
;; clone members
;; todo: only add those that implement Cloneable
"(jdee-gen-deep-clone-copies) 'n"
;; add return statement and finish method
"\"return ret;\" '>'n"
"\"}\" '>'n"
)
"*Template for creating a deep clone method.
Setting this variable defines a template instantiation
command `jdee-gen-deep-clone', as a side-effect."
:group 'jdee-gen
:type '(repeat string)
:set '(lambda (sym val)
(defalias 'jdee-gen-deep-clone
(tempo-define-template
"java-deep-clone-method"
(jdee-gen-read-template val)
nil
"Create a deep clone method at the current point."))
(set-default sym val)))
;; TO DO: Add support for style that puts member declarations at the bottom
;; of a class.
(defun jdee-gen-get-next-member-pos (modifiers &optional no-move-to-point)
"Return the position to add the next member in a class. Return
the point at end of a group of modifiers at the end of the line in a class
definition, or the top of the class if there are no variables with the
modifiers we supply. This assumes a style where modifiers always are at the
top of the class like:
public class Person {
public static final String RACE = \"HUMAN\";
private String name;
private int age;
...
MODIFIERS is a set of modifiers to put the point after.
NO-MOVE-POINT if nil move the point, either way, we return the position.
"
(if (stringp modifiers) (setq modifiers (split-string modifiers ",")))
(let* ((pair (jdee-parse-get-innermost-class-at-point))
(class-name (if pair (car pair)))
pos)
(if (null pair) (error "point is not in a class definition"))
(semantic-fetch-tags)
(setq pos (jdee-parse-get-nth-member class-name modifiers
nil -1 nil 'subset))
(if (null pos) (setq pos (jdee-parse-get-top-of-class class-name)))
(if (and pos (not no-move-to-point)) (goto-char pos))
pos))
(defun jdee-gen-insert-at-class-top (&optional class-regexp no-move-point)
"Position point at top of class, inserting space if needed.
CLASS-REGEXP the symbol used to find the class to find the top of. See
`jdee-parse-get-top-of-class' for more information.
NO-MOVE-POINT if non-nil just the position is returned, otherwise the point is
moved also."
(if (= (point) (jdee-parse-get-top-of-class class-regexp no-move-point))
(insert "\n"))
(insert "\n")
(backward-char 1))
(defun jdee-gen-get-set-member-annotations (type name)
"This is meant to override returning template symbols for private members.
Members include the private encapsulated data of the class.
TYPE is the type of member.
NAME is the member name.
See `jdee-gen-get-set-var-template'."
nil)
;;(makunbound 'jdee-gen-get-set-var-template)
(defcustom jdee-gen-get-set-var-template
'(
;; position point at top of class, inserting space if needed
"(jdee-gen-insert-at-class-top nil t)"
;; remember where we are for later, then go to the end of the private
;; declarations first found from the top of the class
"(progn (tempo-save-named 'mypos (point-marker)) nil)"
"(progn"
" (jdee-gen-get-next-member-pos '(\"private\")) nil)"
;; add class member
"(P \"Variable type: \" type t)"
"(P \"Variable name: \" name t)"
"'&'>"
"(jdee-gen-get-set-member-annotations"
" (tempo-lookup-named 'type)"
" (tempo-lookup-named 'name))"
"'& \"private \" (s type) \" \""
"(s name) \";\" '>"
"(progn (goto-char (marker-position (tempo-lookup-named 'mypos))) nil)"
"(jdee-gen-blank-lines 2 -1)"
;;we begin by the getter
"(jdee-gen-method-signature"
" \"public\""
" (jdee-gen-lookup-named 'type)"
" (if (string= \"boolean\" (jdee-gen-lookup-named 'type) ) "
" (concat \"is\" (jdee-gen-lookup-and-capitalize 'name))"
" (concat \"get\" (jdee-gen-lookup-and-capitalize 'name)))"
" nil"
" )"
"(jdee-gen-electric-brace)"
"\"return \" (s name) \";\" '>'n \"}\"'>'n"
;; leave a blank line with no indentation
"'n"
;;we continue with the setter
"(jdee-gen-method-signature "
" \"public\""
" \"void\""
" (concat \"set\" (jdee-gen-lookup-and-capitalize 'name))"
" (concat (jdee-gen-lookup-named 'type) \" \" "
" (jdee-gen-lookup-named 'name))"
" )"
"(jdee-gen-electric-brace)"
"'>\"this.\" (s name) \" = \" (jdee-gen-lookup-named 'name)"
"\";\" '>'n \"}\" '>'n"
"(when (looking-at \"\\\\s-*\\n\\\\s-*$\")"
" (forward-line 1) (end-of-line) nil)"
)
"*Template for creating a variable at the top of the class and
a get/set method pair for the variable at point.
Setting this variable defines a template instantiation
command `jdee-gen-get-set', as a side-effect."
:group 'jdee-gen
:type '(repeat string)
:set '(lambda (sym val)
(defalias 'jdee-gen-get-set
(tempo-define-template
"java-get-set-pair"
(jdee-gen-read-template val)
nil
"Insert variable at the top of the class and get-set method pair at point."))
(set-default sym val)))
(defalias 'jdee-gen-property 'jdee-gen-get-set)
(defun jdee-gen-get-set-methods (duples)
"Generate variables at the top of the class and get and set methods for
the variables at point.
DUPLES is either a list of list type name pairs or a string. If it is a
string, the string has the form:
type1,name1;type2,name2;...;typeN,nameN
where type is the primitive or class name of the getter setter and name is the
name of the property (getter/setter).
If DUPLES is a lisp object it must a list with the form:
((\"type1\" \"name1\") (\"type2\" \"name2\") ... )"
(interactive "sGets/Sets (type1,name1;type2,name2;...): ")
(if (stringp duples) (setq duples (split-string duples "[ \t]*;[ \t]*")))
(dolist (elt duples)
(let ((pair (if (stringp elt) (split-string elt "[ \t]*,[ \t]*") elt)))
(tempo-save-named 'type (car pair))
(tempo-save-named 'name (car (cdr pair)))
(jdee-gen-get-set))))
;; (makunbound 'jdee-gen-bean-template)
(defcustom jdee-gen-bean-template
'(
;; add class shell
";; generate class and save point which is in body of the constructor"
"(jdee-gen-class) 'p"
;; position point at end of class.
"(progn (end-of-buffer) (search-backward \"}\") (backward-char 1)) 'n"
"(jdee-gen-blank-lines 2 -1)"
"(jdee-gen-save-excursion"
;; import Java, per spec, bean stuff
" (jdee-wiz-generate-interface \"java.io.Serializable\")"