I forgot one thing. These were inserted just for testing,
please remove these lines from jde-gen-enum-template:
;; tmp
"(progn (tempo-save-named 'enums \"FFI, FFR, APPLE\") nil)"
Sorry for the confusion.
Paul Landes writes:
> Here is something I needed wrote this weekend. What this
> provides is:
>
> Templates:
> - jde-gen-enum: an `enumeration' class, which is a static
> collection of identifiers much like C `enums' but object
> oriented. The generated class is optimized for
> serialization and used as keys.
>
> - jde-gen-constructor: Creates a constructor and prompts
> for the access level, which defaults to `public'. Seems
> only natural to already have this, therefore, I wonder if
> it doesn't already exist.
>
> All functions were added for the templates above, but were
> written to be reused, except for
> `jde-gen-insert-enum-identifiers' which displaces a lot of
> emacs code which is tightly coupled with the `jde-gen-enum'
> template.
>
> Feel free to improve, commit to CVS, etc.
>
>
> ;;; code starts here
>
> (defconst jde-class-modifiers
> (set-difference (mapcar #'car jde-imenu-default-modifier-abbrev-alist)
> '("native" "synchronized" "transient" "volatile")
> :test 'equal)
> "*List of all possible class modifiers.")
>
> (defun jde-parse-goto-class-modifiers (&optional class-regexp no-move-point-p)
> "Return and maybe set the point to where the class definition modifiers are.
>
> CLASS-REGEXP matches the class to add the modifiers to.
> NO-MOVE-POINT-P whether or not to move the point."
> (interactive)
> (let (pos re)
> (save-excursion
> (save-match-data
> (setq pos (jde-parse-get-top-of-class (or class-regexp ".*")))
> (if (null pos) (error "class not found"))
> (setq re (concat "^\\(" (mapconcat #'identity
> jde-class-modifiers
> "[ \t]+\\|")
> "[ \t]+\\)*"))
> (if (not (re-search-backward (concat re "class") nil t))
> (error "parse error: can't find beginning of class definition"))
> (re-search-forward re)
> (setq pos (point))))
> (if (and pos (not no-move-point-p)) (goto-char pos))
> ))
>
> (defun jde-add-class-modifier (modifiers &optional class-regexp)
> "Insert modifiers for a class definition.
>
> MODIFIERS a list of modifiers to insert in the class definition.
> CLASS-REGEXP matches the class to add the modifiers to."
> (interactive
> (let* ((mod (completing-read "Modifier: "
> (mapcar #'list jde-class-modifiers) nil t))
> (cr (read-string "Class (default to first found): ")))
> (list (list mod)
> (if (= 0 (length cr)) nil cr)
> )))
> (save-excursion
> (save-match-data
> (if (not (jde-parse-goto-class-modifiers class-regexp))
> (error "class not found"))
> (insert (concat (mapconcat #'identity modifiers " ") " "))
> )))
>
> (defcustom jde-gen-constructor
> '(
> "(p \"Constructor access (default `public'): \" access 'noinsert)"
>
> ;; Constructor
> "'> (jde-javadoc-insert-start-block)"
> "'> (format \" * Create a new <code>%s</code> object.\""
> " (file-name-sans-extension (file-name-nondirectory buffer-file-name)))"
> "'> 'n '>"
> "(jde-javadoc-insert 'tempo-template-jde-javadoc-end-block \"*/\")"
>
> "(jde-gen-method-signature"
> " (let ((m (tempo-lookup-named 'access)))"
> " (if (= 0 (length m)) \"public\" m))"
> " \"\""
> " (file-name-sans-extension (file-name-nondirectory buffer-file-name))"
> " \"\""
> " )"
> "'>"
> "(if jde-gen-k&r "
> " ()"
> " 'n)"
> "\"{\"'>'n"
> "\"}\"'>'n"
> "'>'n"
> )
> "*Template for creating a constructor.
> Setting this variable defines a template instantiation
> command `jde-gen-constructor', as a side-effect."
> :group 'jde-gen
> :type '(repeat string)
> :set '(lambda (sym val)
> (defalias 'jde-gen-constructor
> (tempo-define-template
> "java-constructor"
> (jde-gen-read-template val)
> nil
> "Create a Java constructor."))
> (set-default sym val)))
>
>
> (defun jde-gen-insert-enum-identifiers (&optional include-javadoc-p)
> "Insert enumeration identifiers into a template. This is to be used with
> the `jde-gen-enum-template' or `jde-gen-enum' command."
> (let ((cn (file-name-sans-extension
> (file-name-nondirectory buffer-file-name)))
> (count -1))
> (flet ((ti (arg)
> (if (listp arg)
> (dolist (elt arg) (tempo-insert elt nil))
> (tempo-insert arg nil)
> ))
> (tj (arg)
> (when include-javadoc-p
> (ti '>)
> (jde-javadoc-insert-start-block)
> (ti `(> ,(concat " * " arg) > n >))
> (jde-javadoc-insert 'tempo-template-jde-javadoc-end-block "*/")
> )))
>
> (require 'jde-javadoc)
> (dolist (elt (tempo-lookup-named 'enums))
> (tj (format "Indicates %s <em>%s</em> enumeration."
> (if (member* (string-to-char elt)
> '(?a ?e ?i ?o ?u)
> :test 'equalp) "an" "a")
> elt))
> (ti (format "public final static %s %s = " cn (upcase elt)))
> (ti (format "new %s((byte)%d, \"%s\");"
> cn (incf count) elt))
> (ti '(> n n)))
>
> (tj "All enumerations.")
> (ti (concat "private transient final static "
> cn "[] ALL_ENUMERATIONS = {"))
> (ti '(> n))
> (ti (mapconcat #'identity (tempo-lookup-named 'enums) ", "))
> (ti '(> n "};" > n n))
> )))
>
> (defcustom jde-gen-enum-template
> '(
> ;; add class shell
> "(jde-gen-class)"
>
> ;; tmp
> "(progn (tempo-save-named 'enums \"FFI, FFR, APPLE\") nil)"
>
> "(p \"Enumerations (comma separated): \" enums 'noinsert)"
> "(progn (tempo-save-named 'enums"
> " (split-string (tempo-lookup-named 'enums) \"[ \t]*,[ \t]*\")) nil)"
>
> ;; position point at top of class, inserting space if needed
> "(jde-gen-get-top-class-insert-point 'first)"
>
> ;; enums are identifiers, thus usually serializable and cloneable
> "(jde-wiz-generate-interface \"java.io.Serializable\")"
> "(jde-import-insert-import '(\"java.io.Serializable\"))"
> "(jde-wiz-generate-interface \"java.lang.Cloneable\")"
>
> "(jde-add-class-modifier '(\"final\"))"
>
> "&'n"
>
> "(jde-gen-insert-enum-identifiers t)"
>
> "\"private transient byte enumeration;\"'>'n"
> "\"private transient String name;\"'>'n"
> "\"private transient int hashCode;\"'>'n'n"
>
> ;; private constructor
> "(jde-gen-method-signature"
> " \"private\""
> " \"\""
> " (file-name-sans-extension (file-name-nondirectory buffer-file-name))"
> " \"byte enumeration, String name\""
> " )"
>
> "(if jde-gen-k&r "
> "()"
> "'>'n)"
> "\"{\"'>'n"
>
> "\"hashCode += enumeration;\" '>'n"
> "\"hashCode += name.hashCode();\" '>'n'n"
> "\"this.enumeration = enumeration;\"'>'n"
> "\"this.name = name;\"'>'n"
> "\"}\" '>'n'n"
>
>
> ;; name property
> "'> (jde-javadoc-insert-start-block)"
> "'> \" * Get the name property.\" '> 'n '>"
> "(jde-javadoc-insert 'tempo-template-jde-javadoc-end-block \"*/\")"
>
> "(jde-gen-method-signature"
> " \"public\""
> " \"String\""
> " \"getName\""
> " \"\""
> " )"
>
> "(if jde-gen-k&r "
> "()"
> "'>'n)"
> "\"{\"'>'n"
> "\"return name;\" '>'n"
> "\"}\" '>'n'n"
>
> ;; all property
> "'> (jde-javadoc-insert-start-block)"
> "'> \" * Get all enumerations.\" '> 'n '>"
> "(jde-javadoc-insert 'tempo-template-jde-javadoc-end-block \"*/\")"
>
> "(jde-gen-method-signature"
> " \"public static\""
> " (concat (file-name-sans-extension"
> " (file-name-nondirectory buffer-file-name)) \"[]\")"
> " \"getAll\""
> " \"\""
> " )"
>
> "(if jde-gen-k&r "
> "()"
> "'>'n)"
> "\"{\"'>'n"
> "\"return ALL_ENUMERATIONS;\" '>'n"
> "\"}\" '>'n'n"
>
> ;; Object.equals override
> "(jde-gen-method-signature"
> " \"public\""
> " \"boolean\""
> " \"equals\""
> " \"Object o\""
> " )"
>
> "(if jde-gen-k&r "
> "()"
> "'>'n)"
> "\"{\"'>'n"
> "\"if (this == o) return true;\" '>'n"
> "\"if ((o == null) || !getClass().equals(o.getClass())) return false;\" '>'n'n"
> "(let ((cn (file-name-sans-extension"
> " (file-name-nondirectory buffer-file-name))))"
> " (list 'l (concat cn \" c = (\" cn \")o;\") '> 'n))"
> "\"return name.equals(c.name);\" '>'n"
> "\"}\" '>'n'n"
>
> ;; Object.toString override
> "(jde-gen-method-signature"
> " \"public\""
> " \"String\""
> " \"toString\""
> " \"\""
> " )"
>
> "(if jde-gen-k&r "
> "()"
> "'>'n)"
> "\"{\"'>'n"
> "\"return name;\" '>'n"
> "\"}\" '>'n'n"
>
> ;; Object.hashCode override
> "(jde-gen-method-signature"
> " \"public\""
> " \"int\""
> " \"hashCode\""
> " \"\""
> " )"
>
> "(if jde-gen-k&r "
> "()"
> "'>'n)"
> "\"{\"'>'n"
> "\"return hashCode;\" '>'n"
> "\"}\" '>'n'n"
>
> ;; Object.clone override
> "(jde-gen-method-signature"
> " \"public\""
> " \"Object\""
> " \"clone\""
> " \"\""
> " )"
>
> "(if jde-gen-k&r "
> "()"
> "'>'n)"
> "\"{\"'>'n"
> "\"return this;\" '>'n"
> "\"}\" '>'n'n"
>
>
> ;; serialize methods
>
> ;; write object
> "(jde-gen-method-signature"
> " \"private\""
> " \"void\""
> " \"writeObject\""
> " \"ObjectOutputStream out\""
> " \"IOException\""
> " )"
>
> "(if jde-gen-k&r "
> "()"
> "'>'n)"
> "\"{\"'>'n"
> "\"out.writeByte(enumeration);\" '>'n"
> "\"}\" '>'n'n"
>
> "(jde-import-insert-import '(\"java.io.ObjectOutputStream\"))"
> "(jde-import-insert-import '(\"java.io.IOException\"))"
>
> ;; read object
> "(jde-gen-method-signature"
> " \"private\""
> " \"void\""
> " \"readObject\""
> " \"ObjectInputStream in\""
> " \"IOException, ClassNotFoundException\""
> " )"
>
> "(if jde-gen-k&r "
> "()"
> "'>'n)"
> "\"{\"'>'n"
> "\"enumeration = in.readByte();\" '>'n"
> "\"if (enumeration >= ALL_ENUMERATIONS.length)\" '>'n"
> "\"throw new java.io.InvalidObjectException\" '>'n"
> "\"(\\\"expecting byte enumeration but got byte \\\" + enumeration +\""
> "\"\\\" instead\\\");\" '>'n"
> "\"}\" '>'n'n"
>
> "(jde-import-insert-import '(\"java.io.ObjectInputStream\"))"
>
> ;; readResolve object
> "(jde-gen-method-signature"
> " \"private\""
> " \"Object\""
> " \"readResolve\""
> " \"\""
> " \"ObjectStreamException\""
> " )"
>
> "(if jde-gen-k&r "
> "()"
> "'>'n)"
> "\"{\"'>'n"
> "\"return ALL_ENUMERATIONS[enumeration];\" '>'n"
> "\"}\" '>"
>
> "(jde-import-insert-import '(\"java.io.ObjectStreamException\"))"
> )
> "*Template for creating a Java enumuration object.
> Setting this variable defines a template instantiation
> command `jde-gen-enum', as a side-effect."
> :group 'jde-gen
> :type '(repeat string)
> :set '(lambda (sym val)
> (defalias 'jde-gen-enum
> (tempo-define-template
> "java-enum-object"
> (jde-gen-read-template val)
> nil
> "Create a Java enumeration object."))
> (set-default sym val)))
>
> ;;; code ends here
>
> --
> Paul Landes
> [EMAIL PROTECTED]
>
--
Paul Landes
[EMAIL PROTECTED]