Hi,
>>>Does anyone like this idea? I'm sure there are improvement ideas
too... :-)
This is working code from my .emacs, written by Alessandro (?) , to who i
thanks. Don't know from where i got it.
Maybe Paul considere including in JDE...
;I've extended the jde wizards with a feature for
;generating the method toString on all private and protected class
;attributes.
;I've used as base and code example jde-wiz.el.
;The users can customize if the fields must be written sorted and
the EOL
;for code lines (windows style, unix style).
;(I'm using jde-2.3.2)
;Alessandro
; filename="toString.el"
(require 'jde-wiz)
(defcustom jde-wiz-toString-sorted-fields t
"This variables indicates if fields are written sorted in to the
\"toString\" method"
:group 'jde-wiz
:type 'boolean)
(defcustom jde-wiz-toString-code-EOL (list "Unix")
"This variable defines a code EOL, es. \\r\\n for Windows, \\n for
Unix"
:group 'jde-wiz
:type '(list (radio-button-choice (const "Windows")
(const "Unix"))))
(defun jde-wiz-sort-variables(tokens)
"Returns the tokens sorted by name"
(let ( ht name-list sort-tokens)
(setq ht (make-hash-table))
(while tokens
(setq token (car tokens))
(setq name (semantic-token-name token));;variable name
(puthash name token ht)
(setq name-list (append name-list (list name)))
(setq tokens (cdr tokens))
)
(setq name-list (sort name-list 'string-lessp))
(while name-list
(setq name (car name-list))
(setq sort-tokens (append sort-tokens (list (gethash name
ht))))
(setq name-list (cdr name-list)))
sort-tokens)
)
(defun jde-wiz-toString()
"Generates toString method for all fields defined in the current
buffer."
(interactive)
(setq tokens (semantic-bovinate-toplevel t))
(setq type (semantic-find-nonterminal-by-token 'type tokens))
(setq class (jde-parse-get-class-at-point));;class name
(setq classes (split-string class "\\."))
(setq class-name (nth (- (length classes) 1) classes))
(setq parts (jde-wiz-get-class-parts class-name type))
(setq variables (semantic-find-nonterminal-by-token 'variable
parts))
(setq all-variables (jde-wiz-filter-variables-by-typemodifier
variables))
(if jde-wiz-toString-sorted-fields (setq all-variables
(jde-wiz-sort-variables all-variables)))
(setq functions (semantic-find-nonterminal-by-token 'function
parts))
(setq set-get-functions (jde-wiz-get-get-set-methods functions))
(setq signature "public String toString()")
(setq EOL "\n")
(if (string= (car jde-wiz-toString-code-EOL) "Windows") (setq EOL
"\r\n"))
(setq stringa (concat signature " { " EOL " return \" "
(jde-parse-get-buffer-class) ":\"" " +
System.getProperty(\"line.separator\") "))
(while all-variables
(setq var (car all-variables))
(setq name (semantic-token-name var));;variable name
(setq type (semantic-token-type var));;variable type i.e.
boolean
(setq staticp (member "static"
(semantic-token-variable-modifiers var)));;is it static
(setq finalp (member "final"
(semantic-token-variable-modifiers var)));;is it final
(setq stringa (concat stringa EOL "\t + \"" name " = " "\"
+" name ))
(if (> (length all-variables) 1) (setq stringa (concat
stringa " + System.getProperty(\"line.separator\") " )))
(setq all-variables (cdr all-variables))
)
(setq stringa (concat stringa ";" EOL " }" ) )
(insert stringa)
)
Best Regards,
Adrian
NB Thank you for JDE !
> ----------
> From: Jeff Jensen[SMTP:[EMAIL PROTECTED]
> Sent: Tuesday, November 11, 2003 9:28 PM
> To: [EMAIL PROTECTED]
> Subject: Code Gen Wiz Suggestion: toString()
>
> Hi,
>
> I have a JDE code gen wizard suggestion: generate a toString() method. I
> see
> and use this type of toString() regularly, and maintaining it with a
> larger
> than small quantity of instance variables is a pain. And Eclipse does not
> have
> this!
>
> Here is the general idea:
>
> First, insert a constant "STRINGBUFFER_SIZE", if it does not exist:
>
> /**
> * Initial size of the <code>StringBuffer</code> for
> * <code>toString()</code>.
> */
> private static final int STRINGBUFFER_SIZE = 2000;
>
>
> Next, insert the toString() template, if it does not exist:
>
> /**
> * [EMAIL PROTECTED]
> */
> public String toString()
> {
> StringBuffer sb = new StringBuffer(STRINGBUFFER_SIZE);
>
> return sb.toString();
> }
>
>
> Lastly, take all the instance variables in order and generate one line for
> each
> using this template:
>
> sb.append(" name=").append(name);
>
> For example, an instance variable named "firstName" would look like this:
>
> sb.append(" firstName=").append(firstName);
>
>
> Note that the first append should have no leading spaces and the
> subsequent
> ones do, e.g.
>
> sb.append("firstName=").append(firstName);
> sb.append(" middleName=").append(middleName);
> sb.append(" lastName=").append(lastName);
>
> The update strategy probably needs to compare the value in the string
> (e.g. "
> lastName=") with the instance variable name vs the append value to see if
> it
> already exists, because after generation, the developer could change the
> append
> () (e.g. from ".append(someType)" to ".append(someType.getCode()"). Of
> course,
> the string could get changed too, but it seems the best approach. Not
> sure of
> a better strategy.
>
> I think the feature should reorder the appends to match the instance
> variable
> order too.
>
>
> Potential configuration items are:
> - field separation string: instead of two spaces, maybe someone wants a
> comma
> and a space or other
> - the name of the created constant (STRINGBUFFER_SIZE)
> - the STRINGBUFFER_SIZE variable value (naturally it is easy to change
> after
> genning, and would be appropriate to do for the class
>
>
> Does anyone like this idea? I'm sure there are improvement ideas too...
> :-)
>
>
>
Questo documento e gli eventuali allegati sono indirizzati unicamente al
destinatario, a cui la lettura e l'utilizzo delle informazioni contenute
sono esclusivamente riservati. Nel caso di utilizzo non autorizzato, Banca
Sella potra' dar corso alle azioni piu' opportune per il risarcimento dei
danni subiti anche da terzi. Nell'ipotesi che la e-mail non fosse
indirizzata a Voi o il contenuto lasci intendere che possano esservi stati
errori o manipolazioni nella trasmissione, vogliate cortesemente contattare
i seguenti indirizzi di posta elettronica: [EMAIL PROTECTED]; [EMAIL PROTECTED]
This e-mail is directed uniquely to the interested party, which is the
exclusive addressee of any information contained herein. For any abuse about
the content of this message, Banca Sella will claim compensation for damages
occurred to third parties as well. In case the e-mail should be addressed to
other than you, or the content should reveal any transmission errors or
manipulations, please contact us at the following address: [EMAIL PROTECTED];
[EMAIL PROTECTED]