Wednesday, November 18, 2009

Generating getters and setters in Flex Builder

Fine print: This post discusses not so thoroughly tested regexes that the author used to edit his source code (and to fulfill his newly found affection for exploring regular expressions). Author cannot be held responsible or abused for the damages that these regexes might incur to your code and/or job. Eclipse has this cool feature that lets you generate getters and setters for the variables in your source code. Flex Builder 3, though based on Eclipse platform, doesn't have such an option. For those interested, here is a way to generate get and set methods for your ActionScript private/protected variables using the inbuilt Find/Replace and regular expressions. Though it assumes that you're using tabs for indenting the code (which is the default in FB), you can modify it for spaces easily. Open the Find/Replace window (Ctrl-F) and add the following regular expression to the Find field.


Now add the following pattern to the Replace field.

$0$5$1public function set $3(value:$4):void$5$1{$5$1$2_$3 = value;$5$1}$5$5$1public function get $3():$4$5$1{$5$1$2return _$3;$5$1}$5$5

Don't forget to select the Regular expressions check box in the options box. The replace pattern doesn't accept any meta characters other than $, and hence we need to capture a tab and a new-line using the regex itself. Now use the Find, (Alt-N), Replace (Alt-R) and Replace/Find (Alt-D) to generate get and set methods for the required properties. If you want to generate get/set methods for all the private/protected variables in your class - and you're really courageous, use Replace All (Alt-A).


  1. Thank you Amarghosh, this saved me about 200 lines of code today on a large Value Object!


  2. Beautiful piece of work... thnx

  3. excellent work,Thank You

  4. GOOD WORK...THANK YOU...Amarghosh Vadakkoot