Property assignment triggering instantiation of intermediate objects
In general, if you try to assign a property on a variable and the variable evaluates to
null at run time, Gosu throws a null pointer
exception. However, if you set a property by using property path syntax that
contains at least two objects, you can instruct Gosu to automatically
instantiate an intermediate object in the path.
For example, suppose the following conditions hold:
- You have Gosu classes called
AClassandBClass. AClasshas a property calledBthat contains a reference to an instance of class BClass.- The BClass class has a property called
Namethat contains aStringvalue. - Your code has a variable called
athat contains an instance of typeAClass.
At run time, if a.B contains a non-null reference, you can predict what the following code does:
a.B.Name = "John"
Gosu first evaluates a.B, and then on the result object
sets the Name property
to the value "John".
If the AClass.B property supports instantiation of intermediate objects, the same code works even if
a.B is null at run time.
At run time, if Gosu detects that a.B is null:
- Gosu instantiates a new instance of
BClass. - Gosu sets
a.Bto the new instance. - Gosu sets
a.B.Nameproperty on the new instance ofBClass.
For all Guidewire entity types, properties that contain a foreign key reference support automatic instantiation of intermediate objects.
You can add instantiation of intermediate objects to any Gosu class property. On the line before the property declaration, add the annotation:
@Autocreate
See also
