Flash Builder 4 Feature – Getter/Setter Generation
Creating a well structured VO can be REALLY BORING.
Create class.
Class name is PersonVO.
Type out private variables.
Type out public getters.
Type out public setters.
Type Type Type Type Type.
Now with Flex 4 that isn’t an issue anymore. Adobe has now helped simplify all the repetitive typing necessary to properly create well formed Getters and Setters.
Starting with our test case we have a new VO that has been created called “PersonVO”. The initial code for this class is here:
{
import mx.utils.ObjectUtil;
public class PersonVO
{
public var id:int;
public var firstName:String;
public function PersonVO()
{
}
public function toString():String
{
return ObjectUtil.toString(this);
}
}
}
This part we have all done a million times and we are all sick of the next steps it takes to make getters and setters for our new class. Now with Flex 4 all you need to do is right-click (win) or control+click (mac) on the variable name and new options are available to us. In the popup window scroll down to “Source > Generate Getter/Setter”.

The next screen will look as follows.
From this option screen you can make different selections to your generation process, and after selecting your options you can even preview the generated code before committing.
Lastly, you also will have the option to select where to put the generated code. The first option “Before first method”, will produce the following code.
{
import mx.utils.ObjectUtil;
public class PersonVO
{
public var id:int;
private var _firstName:String;
public function PersonVO()
{
}
public function get firstName():String
{
return _firstName;
}
public function set firstName(v:String):void
{
_firstName = v;
}
public function toString():String
{
return ObjectUtil.toString(this);
}
}
}
The second option “After last method”, will produce the following code.
{
import mx.utils.ObjectUtil;
public class PersonVO
{
public var id:int;
private var _firstName:String;
public function PersonVO()
{
}
public function toString():String
{
return ObjectUtil.toString(this);
}
public function get firstName():String
{
return _firstName;
}
public function set firstName(v:String):void
{
_firstName = v;
}
}
}
The third option “After variable declaration”, will produce the following code.
{
import mx.utils.ObjectUtil;
public class PersonVO
{
public var id:int;
private var _firstName:String;
public function get firstName():String
{
return _firstName;
}
public function set firstName(v:String):void
{
_firstName = v;
}
public function PersonVO()
{
}
public function toString():String
{
return ObjectUtil.toString(this);
}
}
}
After you have made your selection check out the preview panel to see your generated code. Preview panel looks as such.
The only thing left to do is hit “Ok” and keep going. The one thing that I would have liked to see in the Getter/Setter process would also be the dispatchEvent/Bindable event options on each variable. That would speed up the last bit of typing that is typically having to be done.







Sigh
I hoped that this feature would allow a little more power. The reason to have a mutator (besides allowing Binding) is to allow side effects of setting a new value. I prefer that the generated mutator would have something like a File Template to allow us to leverage this feature.
For example, my mutators almost always look like:
private function set thingie(value : String) : void
{
if (_thingie != value)
{
_thingie = value;
// side-effect code goes here
}
}
I completely agree, the idea for a file template is something I have requested to some folks at Adobe. If you have additional requests file a feature request, or vote here for mine to get additional attention: https://bugs.adobe.com/jira/browse/FB-19871
I create a bogus class:
package
{
public class TestClass
{
public function TestClass()
{
private var myheight:int;
private var mywidth:int;
}
}
}
Then I highlight myheight and right click and Source > Generate Getters Setters is disabled. Thoughts or suggestions?
Guess it would help if I put the variables in the proper place, duh.