I realize many IDEs do this, but I felt like writing a few lines of script to spit this out instead. This will grab every line marked “private”, whether method or member variable and generates a default getter/setter.

Note: it doesn’t insert the code for you or even capture to a file. Maybe next I’ll do a Javascript version.

	private Date businessDate;
	private String eventTypeCode;
	private String sourceCode;
 
// becomes the following
 
	public Date getBusinessDate(){ 
		return businessDate;
	}
	public void setBusinessDate(Date businessDate){ 
		this.businessDate = businessDate;
	}
	public String getEventTypeCode(){ 
		return eventTypeCode;
	}
	public void setEventTypeCode(String eventTypeCode){ 
		this.eventTypeCode = eventTypeCode;
	}
	public String getSourceCode(){ 
		return sourceCode;
	}
	public void setSourceCode(String sourceCode){ 
		this.sourceCode = sourceCode;
	}

Script source:

#!/bin/env sh
 
# takes a single filename of a .java file and generates a default getter/setter stub for each private variable
cat $1 | grep private | awk '{
	varName=substr($3, 1, length($3) - 1);
	print "	public " $2 " get" toupper(substr(varName,1,1)) substr(varName, 2) "(){ ";
	print "		return " varName ";";
	print "	}"
 
	print "	public void set" toupper(substr(varName,1,1)) substr(varName, 2) "(" $2 " " varName "){ ";
	print "		this." varName " = " varName ";";
	print "	}"
}'
Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • LinkedIn
  • Live
  • StumbleUpon
  • Tumblr
  • TwitThis
  • FriendFeed
  • Netvibes
  • Ping.fm
  • Posterous
  • Technorati
  • Tipd
  • Twitter
  • Yahoo! Buzz