Enhancements

Gosu enhancements provide a way for you to augment classes and other types with additional concrete methods and properties. For example, use enhancements to define additional utility methods on a class or interface that cannot be directly modified. You can enhance Guidewire entity types and also classes defined in either Gosu or Java. The most valuable use of enhancements is to define additional utility methods on a Java class or interface that cannot be directly modified. This feature is most useful if the source code for a class is unavailable or class is marked final and so cannot be subclassed.

Comparing enhancements and subclasses

Enhancements differ from subclasses in important ways. Enhancement methods and properties on a class are available to all objects of the enhanced class. Methods and enhancements on a subclass are available only to objects of the subtype, not to objects of the superclass. Use enhancements to add functionality that the original authors of a class did not provide.

Enhancing a class makes the new methods and properties available to all instances of that class, not merely to subclass instances. For example, if you add an enhancement method to the String class, all String instances in Gosu have the additional method.

Additional differences between classes and enhancements include:
  • Enhancements cannot save state information by allocating new variables or properties on the enhanced type.
  • Enhancements can create new methods but cannot override existing methods.
  • To reference methods on the enhanced class or type, use the symbol this to access the enhancements. For example, to call the enhanced object’s myAction method, use the syntax this.myAction(). Do not use the keyword super in an enhancement.
    Note: Enhancements are defined in terms of the external interface of the enhanced type. The keyword super implies a superclass rather than an interface, so super is inappropriate for enhancements.

Static dispatch of enhancements

Gosu enhancements are statically dispatched. Enhancement code is run at run time, but availability and the relevant code is determined at compile time. Availability of enhancement properties and methods is based on the compile-time type, not the run-time type. This behavior is different from regular properties and methods that are accessed at run time.

This distinction is important for two reasons:
Subtype/supertype distinctions might prevent access
Suppose a method argument is declared as the type Object. At compile time, the argument variable has type Object, even though at run time, it could be a much more specific type than Object. If at run time, the object type is java.util.ArrayList, the enhancement method on ArrayList is unavailable. You can work around this limitation by downcasting (myObject as ArrayList) before accessing the enhancement. However, downcasting could fail at run time if the runtime type is not compatible with ArrayList, so you must understand the risks of that technique.
Subtype/supertype distinctions might dispatch in unexpected ways
If you have multiple enhancements that have the same names for properties and methods, there could be enhancements on more than one type in the hierarchy. For example, consider enhancements of both the java.util.Map interface and java.util.HashMap with different enhancement logic. At compile time, Gosu uses the most specific type for that programming context. If you use a variable declared to type java.util.Map, its enhancement methods are determined at compile time, even if at run time that object has subtype java.util.HashMap.