§6.1. Reflection
Object Teams supports reflection with respect to teams, roles, and role-base relationships.
(a)
Interface to the role registry
Each team instance internally has a registry of known role objects indexed by their base object. Programmers may make use
of this registry using the following reflective methods defined in
org.objectteams.Team:
|
boolean hasRole ( Object aBase ) ; |
|
|
This method checks whether a role for the passed base object already exists in the target team.
|
|
boolean hasRole ( Object aBase, Class expectedRole ) ; |
|
|
This method checks whether a instance of type expectedRole as a role for the passed base object aBase already exists in the target team.
The role may also be of any subtype of the specified role type.
|
|
Object getRole ( Object aBase ) ; |
|
|
If the passed base object aBase already has a role in the target team, this role is returned. Otherwise null is returned.
|
|
Object getRole ( Object aBase, Class expectedRole ) ; |
|
|
If the passed base object aBase already has a role of type expectedRole in the target team, this role is returned. Otherwise null is returned.
|
|
Object[] getAllRoles () ; |
|
|
Retrieves all existing (registered) roles in the target team.
This method uses internal structures of weak references. For that reason it may return role instances which were about to
be reclaimed by the garbage collector. If performance permits, it is thus advisable to always call System.gc() prior to calling getAllRoles() in order to achieve deterministic results (see also §2.1(f)).
|
|
Object[] getAllRoles ( Class expectedRole ) ; |
|
|
Retrieves all existing (registered) roles of type expectedRole in the target team. Class expectedRole must be a top-most bound role, ie., it must have a playedBy clause and no regular super class with a playedBy clause.
Note, that the returned array is actually an array of Object, ie., elements have to be cast to exepctedRole individually.
See the note about garbage collection above.
|
|
void unregisterRole ( Object aRole ) ; |
|
|
This method unregisters the passed role object from the target team. Thus the corresponding base looses this role. After calling
this method the role should no longer be used.
|
|
void unregisterRole ( Object aRole, Class roleClass ) ; |
|
|
This method unregisters the passed role object from the target team. Thus the corresponding base looses this role. After calling
this method the role should no longer be used. The only difference to the previous method is improved speed because no search
for the corresponding registry has to be performed.
|
It is desirable and possible to use these methods within guards (see
§5.4.). These methods allow to write the specification of guards in a more concise and more expressive way. Determined by the signature,
the first four methods can only be used in a base-level guard (
§5.4.2) because they require a reference to a base object.
Example code:
| 1 |
public team class SpecialConditions { |
| 2 |
public void participate(Account as BonusAccount ba) {} |
| 3 |
public class BonusAccount playedBy Account |
| 4 |
base when(SpecialConditions.this.hasRole(base, BonusAccount.class)) |
| 5 |
{ |
| 6 |
public callin void creditBonus(int amount) |
| 7 |
base.creditBonus(amount+bonus); |
| 8 |
} |
| 9 |
void creditBonus(int amount) <- replace void credit(int i) |
| 10 |
base when (i > 1000); |
| 11 |
} |
| 12 |
} |
|
(b)
Behavioral reflection
The next method does not access the registry but can be used to inspect whether a control flow has already been intercepted
by at least one callin binding of the current team. It can be used to avoid undesirable re-entrance to a team.
|
boolean isExecutingCallin () ; |
(c)
Class literals for roles
The Java syntax for so-called class literals,
MyClass.class (see
JLS 15.8.2) can be used for role types with slightly changed semantics: Role types are virtual types (
§1.3.1) that are bound dynamically (
§1.3.1(e)). This applies to role class literals, too. From this follows the constraint that a role class literal can only be used within
the non-static context of a team, ie., for evaluating a role class literal an enclosing team instance must be in scope.
Unlike regular type checking for role types, the class literal itself does not have a dependent type. Thus type checking of
calls to methods like
hasRole(Object,Class) cannot detect, whether the
Class instance has actually been obtained from the correct team instance. Any attempt to pass a class that is not known as a bound
role within the given team results in an
IllegalArgumentException at run-time.
§6.2. Other API Elements
(a)
Interfaces for role encapsulation
A set of pre-defined types exist that do
not extend
java.lang.Object
and have
no features except the operators
==
and
!=.
Note:
The JLS defines that each interface declares all methods
defined in java.lang.Object (JLS 9.2) and also each
object referenced by an interface type can be widened to
java.lang.Object.
Compilers commonly implement this by declaring
java.lang.Object the super-type of all interfaces.
Such implementation has no visible difference with respect to
the more complex definition in the JLS.
These predefined types are
| regular interface |
org.objectteams.IConfined |
| role interface |
org.objectteams.Team.IConfined |
| role class |
org.objectteams.Team.Confined |
These types provide now new functionality but inheriting from these types influences the semantics with respect to encapsulation.
The purpose and usage of these types is described in
§7.(b)
Interface for explicit lowering
The following role interface exists for the purpose of allowing explicit lowering:
| role interface |
org.objectteams.Team.ILowerable |
This interface was introduced in detail in
§2.2(d).
(c)
Team activation methods
Every team can be activated and deactivated by predefined methods of the class
org.objectteams.Team.
| Methods for activation of a team: |
activate() and
|
activate(Thread th) |
| Methods for deactivation of a team: |
deactivate() and
|
deactivate(Thread th) |
The usage of these Methods is described in
§5.2(b).