It's is dreary to insert getters and setters in each bean you are writing. Here's a small elisp function that makes it automatical for you if you're using Emacs. Yeah, it sucks and it doesn't indent automatically... Any ideas on how to fix the indentation problem?

Put this into your .emacs. (Note that I like to use the 'm_' -prefix in member variables. It's not Java standard, but I feel that this a) reduces danger of confusion which variables are member variables and which are locals, and b) I hate seeing this. everywhere. See WhyUsingThisIsBad.)

;; FIXME: This function still requires the setting of indent level.
;; It is currently hardcoded to four.

(defun insert-gettersetter (type field)
  "Inserts a Java field, and getter/setter methods."
  (interactive "MType: \nMField: ")

  ;; If you don't like the automatic m_ -prefix, then just set this
  ;; to whatever you want.  Empty is okay.
  (setq getsetprefix "m_")
  (setq capfield (concat (capitalize (substring field 0 1)) (substring field 1)))
  (setq field (concat getsetprefix field))
  (insert (concat "private " type " " field ";\n\n" 
                  "public " type " get" capfield "()\n"
                  "    return " field ";\n"
                  "public void set" capfield "( " type " arg )\n"
                  "    " field " = arg;\n"


Use with M-x insert-gettersetter, or bind to a key with
(global-set-key [f9] 'insert-gettersetter)

Emacs will then ask you the type and the name ("field") of the field to create, and insert a new getXXX() and setXXX() method at the current point.

Here's an improved version with indenting and use of the `this.var = var' convention =).#

Also, it doesn't use `setq', which introduces persistent variables into the global Emacs namespace.
(defun insert-gettersetter (type field)
  "Inserts a Java field, and getter/setter methods."
  (interactive "MType: \nMField: ")

  (let ((oldpoint (point))
        (capfield (concat (capitalize (substring field 0 1)) (substring field 1)))
    (insert (concat "public " type " get" capfield "()\n"
                    "    return this." field ";\n"
                    "public void set" capfield "(" type " " field ")\n"
                    "    this." field " = " field ";\n"
    (c-indent-region oldpoint (point) t)

-- √ėyvind Stegard

Here's a reloaded version with automatic variable recognition.#

(defun make-class-getter-setter (type var)
   (concat "public %s get%s() { return %s; }\n"
           "public %s set%s(%s %s) { this.%s = %s; }\n")
   type (capitalize var) var                    ; getter line                                    
   type (capitalize var) type var var var))     ; setter line

(defun extract-class-variables (&rest modifiers)
  (let ((regexp
	  "^\\([ \t]*\\)"
	  "\\(" (mapconcat (lambda (m) (format "%S" m)) modifiers "\\|") "\\)"
	  "\\([ \t]*\\)"
	  "\\([ \t]*\\)"
      (goto-char (point-min))
      (loop for pos = (search-forward-regexp regexp nil t)
	    while pos collect (let ((modifier (match-string 2))
				    (type (match-string 4))
				    (name (match-string 6)))
				(list modifier type name))))))
(defun generate-class-getter-setter (&rest modifiers)
  (let ((oldpoint (point)))
     (mapconcat (lambda (var) (apply 'make-class-getter-setter (rest var)))
                (apply 'extract-class-variables modifiers)
    (c-indent-region oldpoint (point) t)))

Just assign your favorite key combinations to below commands:

(generate-class-getter-setter 'public 'private)
(generate-class-getter-setter 'private)

- Volkan YAZICI

Maggie Leber: If you're not an emacs user, (or can be talked out of it) let me put in a good word for JEdit. It has a plugin to insert getters and setters, and for many, many other routine Java programming tasks (including simple refactoring), and decent support for dealing with XML documents. It's written in Java, open source, and macros for it are written in BeanShell, another tool I think every Java programmer should know about.

This is great, however, it does not handle properly variables such as table_name I get public String getTable_name() { return this.table_name; }

public void setTable_name(String table_name) { this.table_name = table_name; }

instead of this more elegant form

public String getTableName() { return this.table_name; }

public void setTableName(String table_name) { this.table_name = table_name; }

--AnonymousCoward, 15-Sep-2006

If you are using Emacs for Java development, check the JDEE: an add-on software package that turns Emacs into a comprehensive system for creating, editing, debugging, and documenting Java applications.

It comes with that functionality (and many, many other useful ones) out of the box.

-- NascifAbousalhNeto, 15-Sep-2006

Add new attachment

Only authorized users are allowed to upload new attachments.
« This page (revision-5) was last changed on 07-Apr-2007 13:29 by