§2.3.1.(d) Fine-tuning role instantiation
If the lifting operation as defined above degrades the program performance, the lifting semantics can be modified per role
class
by adding the annotation @org.objectteams.Instantiation
which requires an argument of type
org.objectteams.InstantiationPolicy
in order to select between the following behaviors:
- ONDEMAND
- This is the default behavior as defined above.
- ALWAYS
- This strategy avoids maintaining the internal role cache, but instead a fresh role instance is created for each lifting request.
This may increase the number of role instances but cuts the costs of accessing the cache, which could otherwise become
expensive if a cache grows large. As a result of this strategy role state can no longer be shared
over time, thus it is discouraged to define fields in a role with this strategy. Also, comparing roles could lead
to
unexpected results. Therefor, roles with this strategy should implement custom
equals
and hashCode
methods, which should simply delegate to the base instance (using callout §3).
- NEVER
- Roles with this instantiation policy are never instantiated by lifting.
Such roles cannot define non-static fields.
Otherwise this optimization is fully transparent, specifically callout bindings will refer to the correct base instance.
As of version 2.0 the OT/J compiler does not implement this strategy.
- SINGLETON
- Roles declaring this strategy will be instantiated at most once per team. Subsequent lifting requests in the same team
will always answer the same role instance. Such roles may receive triggers from callin bindings, but cannot define
callout bindings.
As of version 2.0 the OT/J compiler does not implement this strategy.