ObjectTeams/Java Language Definition - §4.

§4. Callin Binding

Notion of callin binding

Callin bindings realize a forwarding in the direction opposite to callout bindings. Both names are chosen from the perspective of a role, which controls its communication with an associated base object.
Technically, callin bindings are equivalent to weaving additional code (triggers) into existing base methods.
Callin Methods of a base class may be intercepted by a callin binding (the base method "calls into" the role).
Before/after/replace The modifiers before, after, replace control the composition of original method and callin method.
Activation Callin bindings may be active or inactive according to §5.

§4.1. Callin method binding

(a) Method call interception
A role method may intercept calls to a base method by a callin binding.
(b) Prerequisite: Class binding
A callin binding requires the enclosing class to be a role class bound to a base class according to §2.1. An unliftable role (see §2.3.4(a)) cannot define callin bindings. In that case callin bindings can only be introduced in sub-roles which (by an appropriately refined playedBy clause) disambiguate the lifting translation.
(c) Callin declaration
A callin binding composes an existing role method with a given base method. It may appear within the role class at any place where feature declarations are allowed. It is denoted by
role_method_designator "<-" callin_modifier base_method_designator;
Just like with callout bindings, method designators may or may not contain parameters lists and return type but no modifiers; also, each method designator must exactly and uniquely select one method (cf. §3.1(c)).
For callin modifiers see below (§ 4.2).
(d) Multiple base methods
Base method designators may furthermore enumerate a list of methods. If multiple base methods are bound in one callin declaration generally all signatures in this binding must be conform.
However, extraneous parameters from base methods may be ignored at the role.
For result types different rules exist, depending on the applied callin modifier (see next).
(e) Named callin binding
Any callin binding may be labeled with a name. The name of a callin binding is used for declaring precedence (§ 4.8). A named callin binding overrides any inherited callin binding (explicit and implicit (§ 1.3.1)) with the same name.
It is an error to use the same callin name more than once within the same role class.
(f) Callin to final
When binding to a final base method, the enclosing role must be played by the exact base class declaring the final method. I.e., callin binding to a final method inherited from the base class's super-class is not allowed.
(g) Declared exceptions
It is an error if a role method to be bound by callin declares in its throws clause any exceptions that are not declared by the corresponding base method(s).

§4.2. Callin modifiers (before, after, replace)

(a) Method composition
The kind of method composition is controlled by adding one of the modifiers before, after or replace after the "<-" token of the binding declaration.
(b) Additive composition
The before and after modifiers have the effect of adding a call to the role method at the beginning or end of the base method, resp.
In this case no data are transferred from the role to the base, so if the role method has a result, this will always be ignored.

Example code:

1
 team class Company {
2
    protected class Employee playedBy Person {
3
       public void recalculateIncome() { ... }
4
       recalculateIncome <- after haveBirthday; // callin binding
5
    }
6
 }

Effects:

(c) Replacing composition
The replace modifier causes only the role method to be invoked, replacing the base method.
In this case, if the base method declares a result, this should be provided by the role method. Special cases of return values in callin bindings are discussed in §4.3(e)
(d) Callin methods
Role methods to be bound by a callin replacement binding must have the modifier callin. This modifier is only allowed for methods of a role class. The only legal way to call a method with the callin modifier is via a callin replace binding. It is illegal for a callin method A callin method cannot override a regular method and vice versa, however, overriding one callin method with another callin method is legal and dynamic binding applies to callin method just like regular methods.
A callin method must not declare its visibility using any of the modifiers public, protected or private. Since callin methods can only be invoked via callin bindings such visibility control would not be useful.

§4.3. Base calls


Role methods with a callin modifier should contain a base call which uses the special name base in order to invoke the original base method (original means: before replacement).
(a) Syntax
The syntax for base calls is base.m(), which is in analogy to super calls. A base.m() call must use the same name and signature as the enclosing method. This again follows the rule, that roles should never explicitly use base names, except in binding declarations.
(b) Missing base call
For each callin method, the compiler uses some flow analysis to check whether a base call will be invoked on each path of execution. The compiler will issue a warning if a base call is missing either on each path (definitely missing) or on some paths (potentially missing). Instead of directly invoking a base call, a callin method may also call its implicit super version using tsuper.m(). In this case the flow analysis will transitively include the called tsuper version.
(c) Duplicate base call
If a callin method contains several base calls, the compiler gives a warning if this will result in duplicate base call invocations on all paths (definitely duplicate) or on some paths (potentially duplicate). Again tsuper calls are included in the flow analysis (see 4.3(b)).
(d) Parameter tunneling
If a base method has more parameters than a callin method to which it is composed, additional parameters are implicitly passed unchanged from the original call to the base call (original means: before interception). I.e., a call base.m() may invisibly pass additional parameters that were provided by the caller, but are hidden from the role method.
(e) Fragile callin binding
If a role method returns void, but the bound base method declares a non-void result, this is reported as a fragile callin binding: The result can still be provided by the base call, but omitting the base call may cause problems depending on the return type: It is an error if a callin method involved in a fragile callin binding has definitely no base call.

Comment:

Base calls can occur in callin methods that are not yet bound. These methods have no idea of the names of base methods that a sub-role will bind to them. Also multiple base methods may be bound to the same callin method. Hence the use of the role method's own name and signature. The language implementation translates the method name and signature back to the base method that has originally been invoked.

Example code:

1
 public class ValidatorRole playedBy Point {
2
    callin void checkCoordinate(int value) {
3
       if (value < 0)
4
         base.checkCoordinate(-value);
5
       else
6
         base.checkCoordinate(value);
7
    }
8
    checkCoordinate <- replace setX, setY;
9
 }

Effects:

§4.4. Callin parameter mapping

(a) General case parameter mapping
The rules for mapping callin parameters and result type are mainly the same as for callout bindings (§3.2) except for reversing the -> and <- tokens and swapping left hand side and right hand side.
Callin bindings using before have no result mapping. For result in after callin bindings see §4.4(c) below.
(b) Restrictions for callin replace bindings
The right-hand side of a parameter mapping may either be the simple name of a base method argument without further computation, or an arbitrary expression not containing any base method argument.
Each base method argument must either appear as a simple name in exactly one parameter mapping or not be mapped at all. In the latter case, the original argument is "tunneled" to the base call, meaning, the callin method does not see the argument, but it is passed to the base method as expected.
If the base method declares a result, then These rules ensure that these bindings are reversible for the sake of base calls (§4.3).

As stated above a fragile callin binding (§4.3(e)) is not allowed with a callin method that definitely has no base call (§4.3(b)). A callin replace binding is not fragile if it provides the base result using a result mapping.

A callin method bound with replace to a base method returning void must not declare a non-void result.
(c) Mapping the result of a base method
In an after callin binding, the right-hand side of a parameter mapping may use the identifier result to refer to the result of the base method.
(d) Multiple base methods
A callin binding listing more than one base method may use parameter mappings with only the following restriction: if any base parameter should be mapped this parameter must have the same name and type in all listed base method designators.

§4.5. Lifting and lowering

For basic definition see §2.2 and §2.3.
(These rules are reverse forms of those from §3.3)
(a) Call target translation
Invoking a role method due to a callin binding first lifts the base object to the role class of the callin binding, in order to obtain the effective call target. This is why callin bindings cannot be defined in roles that are unliftable due to potential binding ambiguity (see §4.1(b) above and §2.3.4(a)).
(b) Parameter translation
During callin execution, each parameter for which the role method expects a role object is implicitly lifted to the declared role class.
(c) Result translation
Returning a role object from a callin method implicitly lowers this object.
(d) Typing rules
A parameter mapping (implicit by parameter position or explicit by a with clause) is well typed if the right hand side conforms to the left hand side, either by A result mapping (implicit or explicit by a with clause) is well typed, if the value at the left hand conforms to the right hand side according to the rules given above, except that translation polymorphism here applies lowering instead of lifting.
These rules define translation polymorphism as introduced in §2.3.
(e) Role arrays
For arrays of roles as parameters §2.3(d) applies accordingly. For arrays as return value §2.2(e) applies.
(f) Base calls
For base calls these rules are reversed again, i.e., a base call behaves like a callout binding.

§4.6. Overriding access restrictions

Callin bindings may also mention inaccessible methods (cf. decapsulation §3.4). Due to the reverse call direction this is relevant only for base calls within callin methods. Base calls have unrestricted access to protected base methods. Accessing a base method with private or default visibility is also allowed, but signaled by a compiler warning.
Comment:A base call to an inaccessible base method is considered harmless, since this is the originally intended method execution.

§4.7. Callin binding with static methods

The normal case of callin bindings refers to non-static methods on both sides (base and role). Furthermore, in Java inner classes can not define static methods. Both restrictions are relaxed by the following rules:
(a) Static role methods
A role class may define static methods (see also §1.2.1(f)).
(b) Binding static to static
A callin binding may bind a static role method to one or more static base methods. It is, however, an error to bind a static base method to a non-static role method, because such binding would require to lift a base object that is not provided.
(c) before/after
In addition to the above, before and after callin bindings may also bind a static role method to non-static base methods.
(d) replace
In contrast to §4.7(c) above, a replace callin binding cannot bind a static role method to a non-static base method.
The following table summarizes the combinations defined above:
<-   base method
static non-static
role
method
static OK before/after: OK
replace: illegal
non-static illegal OK

(e) no overriding
Since static methods are not dynamically bound, overriding does not apply in the normal semantics. Regarding callin bindings this has the following consequences (assuming a role RMid played by BMid plus its super-class BSuper and its sub-class BSub.
  1. If a static base method BMid.m is bound by a callin binding this has no effect on any method m in BSub.
  2. If a callin binding mentions a method m which is not present in BMid but resolves to a static method in BSuper the binding actually applies to BSuper.m. However, adding a matching method m to BMid will dissolve any connection between BSuper.m and the callin binding.
  3. In order to bind two static base methods with equal signatures, one being defined in a sub-class of the other one, two roles have to be defined where one role refines the playedBy clause of the other role (say: public class RSub extends RMid playedBy BSub). Now each role may bind to the static base method accessible in its direct base-class.

§4.8. Callin precedence

If multiple callins from the same team refer to the same base method and also have the same callin modifier (before, after or replace), the order in which the callin bindings shall be triggered has to be declared using a precedence declaration.
(a) Precedence declaration
A precedence declaration consists of the keyword precedence followed by a list of names referring to callin bindings (see § 4.1(e) for named callin bindings).
A precedence declaration is only legal within a role or team class.
(b) Qualified and unqualified names
Within a role class a callin binding may be referenced by its unqualified name. A precedence declaration in a team class must qualify the callin name with the name of the declaring role class. A team with nested teams may concat role class names. Elements of a qualified callin name are seperated by ".".
The callin binding must be found in the role specified by the qualifying prefix or in the enclosing role for unqualified names, or any super class of this role (including implicit super classes § 1.3.1).
(c) Class based precedence
At the team level a precedence declaration may contain role class names without explicitely mentioning callin bindings in order to refer to all callin bindings of the role.
(d) Multiple precedence statements
All precedence statements are collected at the outer-most team. At that level all precedence declarations involving the same base method are merged using the C3 algorithm [ citation ]. It is an error to declare incompatible precedence lists that cannot be merged by the C3 algorithm.
(e) Binding overriding
Precedence declarations may conflict with overriding of callin bindings (see § 4.1(e)): For each pair of callin bindings of which one callin binding overrides the other one, precedence declarations are not applicable, since dynamic binding will already select exactly one callin binding.
It is an error to explicitly mention such a pair of overriding callin bindings in a precedence declaration.
When a class-based precedence declaration implicitly refers to a callin binding that is overridden by, or overrides any other callin binding within the same precedence declaration, this does not affect the fact, that the most specific callin binding overrides less specific ones.

Callin binding example:

1
 public class LogLogin playedBy Database {
2
    callin void log (String what) {
3
       System.out.println("enter "+what);
4
       base.log(what.toLowerCase());
5
       System.out.println("leave "+what);
6
    }
7
    void log(String what) <- replace void login(String uid, String passwd) with {
8
        what <-  uid
9
    }
10
 }
11
 (new Database()).login("Admin", "Passwd");

Effects

Provided the callin bindings are active (cf. §5) then:

Open issues:

The pattern language for specifying sets of base methods (§ 4.1(d)) has not been fixed yet. Possibly, this will not even be part of the language, but an external tool may be required to query base signatures and insert an explicit list of base methods.
© Stephan Herrmann, Christine Hundt
OT/J Version 0.9 — Last modified: Wed Dec 20 2006