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 AClass and BClass.
  • AClass has a property called B that contains a reference to an instance of class BClass.
  • The BClass class has a property called Name that contains a String value.
  • Your code has a variable called a that contains an instance of type AClass.

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:

  1. Gosu instantiates a new instance of BClass.
  2. Gosu sets a.B to the new instance.
  3. Gosu sets a.B.Name property on the new instance of BClass.

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