| <!DOCTYPE html |
| PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "../xhtml1-strict.dtd"> |
| <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> |
| <head> |
| <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> |
| <link rel="stylesheet" type="text/css" href="../css/ot.css" /> |
| <link rel="stylesheet" type="text/css" href="../css/otjld.css" /> |
| <title>OT/J Language Definition v1.3</title> |
| </head> |
| <body class="otdt"> |
| <div id="content"> |
| <table class="nav"> |
| <tr> |
| <td class="back"><a id="top"></a><a href="s2.3.html" rel="prev"><< §2.3 Lifting</a></td> |
| <td class="top"><a href="index.html" rel="contents">↑ Table of Contents ↑</a></td> |
| <td class="next"><a href="s2.5.html" rel="next">§2.5 Abstract Roles >></a></td> |
| </tr> |
| </table> |
| <div class="breadcrumb"><a class="nav" href="s2.html" rel="section">§2 Role Binding</a></div> |
| <div class="sect depth2" id="s2.4"> |
| <h2 class="sect">§2.4 Explicit role creation<a class="img" href="s2.4.html" |
| title="PermaLink to §2.4 Explicit role creation"><img style="vertical-align:text-top;margin-left:5px;" src="../images/permalink.png" |
| alt="" /></a></h2> |
| <p>Lifting is the normal technique by which role objects are created implicitly. |
| This section defines under which conditions a role can also be created explicitly. |
| |
| </p> |
| <div class="sect depth3" id="s2.4.1"> |
| <h3 class="sect">§2.4.1 Role creation via a lifting constructor<a class="img" href="s2.4.1.html" |
| title="PermaLink to §2.4.1 Role creation via a lifting constructor"><img style="vertical-align:text-top;margin-left:5px;" src="../images/permalink.png" |
| alt="" /></a><span class="toplink"><a href="#top">↑ §2.4</a></span></h3> |
| <p>Lifting uses the default constructor for roles (see <a href="s2.3.1.html" title="§2.3.1 Implicit role creation" |
| class="sect">§2.3.1</a>). |
| This constructor can be invoked from client code, if the following rules are respected. |
| |
| </p> |
| <div class="subsect depth4" id="s2.4.1.a"> |
| <h4 class="subsect">(a) <span class="title">Team context</span><a class="img" href="s2.4.1.a.html" title="PermaLink to (a) Team context"><img style="vertical-align:text-top;margin-left:5px;" src="../images/permalink.png" |
| alt="" /></a></h4> |
| <p>The lifting constructor can be used only within the enclosing team of |
| the role to be instantiated. Thus, qualified allocation expressions |
| (<code>someTeam.new SomeRole(..)</code>) may never use the lifting constructor. |
| |
| </p> |
| </div> |
| <div class="subsect depth4" id="s2.4.1.b"> |
| <h4 class="subsect">(b) <span class="title">Fresh base object</span><a class="img" href="s2.4.1.b.html" |
| title="PermaLink to (b) Fresh base object"><img style="vertical-align:text-top;margin-left:5px;" src="../images/permalink.png" |
| alt="" /></a></h4> |
| <p>If the argument to a lifting constructor invocation is a <code>new</code> |
| expression, creating a fresh base object, the use of the lifting constructor |
| is safe. Otherwise the rules of (c) below apply. |
| |
| </p> |
| </div> |
| <div class="subsect depth4" id="s2.4.1.c"> |
| <h4 class="subsect">(c) <span class="title">Duplicate role runtime check</span><a class="img" href="s2.4.1.c.html" |
| title="PermaLink to (c) Duplicate role runtime check"><img style="vertical-align:text-top;margin-left:5px;" src="../images/permalink.png" |
| alt="" /></a></h4> |
| <p>If it cannot be syntactically derived, that the argument to a lifting |
| constructor is a freshly created base object (b), a compile time warning will |
| signal that an additional runtime check is needed: It must be prevented that |
| a new role is created for a base object, which already has a role of the |
| required type in the given team. It is not possible to replace an existing |
| role by use of the lifting constructor. At runtime, any attempt to do so |
| will cause a <code>org.objectteams.DuplicateRoleException</code> to be thrown. |
| This exception can only occur in situations where the mentioned compile |
| time warning had been issued. |
| <br /><a href="s6.1.html" title="§6.1 Reflection" class="sect">§6.1</a> will introduce reflective functions |
| which can be used to manually prevent errors like a duplicate role. |
| |
| </p> |
| </div> |
| </div> |
| <div class="sect depth3" id="s2.4.2"> |
| <h3 class="sect">§2.4.2 Role creation via a regular constructor<a class="img" href="s2.4.2.html" |
| title="PermaLink to §2.4.2 Role creation via a regular constructor"><img style="vertical-align:text-top;margin-left:5px;" src="../images/permalink.png" |
| alt="" /></a><span class="toplink"><a href="#top">↑ §2.4</a></span></h3> |
| <p>Roles may also be created explicitly using a custom constructor with arbitrary signature |
| other than the signature of the lifting constructor.<br /> |
| Within role constructors, four kinds of self-calls are possible: |
| |
| </p> |
| <dl> |
| <dt><code>base(..)</code></dt> |
| <dd>A constructor of the corresponding base class (<a href="sA.html#sA.5.3" title="§A.5.3 BaseCall" class="sect">§A.5.3</a>(c)), |
| <span class="underline">unless</span> the role is involved in base class circularity (<a href="s2.1.2.b.html" title="§2.1.2.(b) Cycles" class="sect">§2.1.2.(b)</a>), |
| in which case a base constructor call is illegal. |
| </dd> |
| <dt><code>this(..)</code></dt> |
| <dd>Another constructor of the same class.</dd> |
| <dt><code>super(..)</code></dt> |
| <dd>A constructor of the super-class (normal <code>extends</code>), <span class="underline">unless</span> the super-class is bound to a different base class, in which case calling <code>super(..)</code> is not legal. |
| </dd> |
| <dt><code>tsuper(..)</code></dt> |
| <dd>A constructor of the corresponding role of the super-team (<a href="sA.html#sA.5.4" title="§A.5.4 TSuperCall" class="sect">§A.5.4</a>(e)). Also see the constraint in <a href="s1.3.2.c.html" |
| title="§1.3.2.(c) Constructors and overridden 'extends' " |
| class="sect">§1.3.2.(c)</a>. |
| </dd> |
| </dl> |
| <div class="subsect depth4" id="s2.4.2.a"> |
| <h4 class="subsect">(a) <span class="title">Unbound roles</span><a class="img" href="s2.4.2.a.html" title="PermaLink to (a) Unbound roles"><img style="vertical-align:text-top;margin-left:5px;" src="../images/permalink.png" |
| alt="" /></a></h4> |
| <p>Each constructor of a role that is <strong>not bound</strong> to a base class must use |
| one of <code>this(..)</code>, <code>super(..)</code> or <code>tsuper(..)</code>. |
| |
| </p> |
| </div> |
| <div class="subsect depth4" id="s2.4.2.b"> |
| <h4 class="subsect">(b) <span class="title">Bound roles</span><a class="img" href="s2.4.2.b.html" title="PermaLink to (b) Bound roles"><img style="vertical-align:text-top;margin-left:5px;" src="../images/permalink.png" |
| alt="" /></a></h4> |
| <p>Each constructor of a <strong>bound role</strong> must directly or indirectly invoke either |
| a <code>base(..)</code> constructor or a lifting constructor (see <a href="s2.3.1.html" title="§2.3.1 Implicit role creation" |
| class="sect">§2.3.1</a>). |
| Indirect calls to the base constructor or lifting constructor may use any of <code>this(..)</code>, <code>super(..)</code> |
| or <code>tsuper(..)</code>, which simply delegates the obligation to the called constructor. |
| <br /> |
| If a constructor referenced by <code>base(..)</code> is not visible according to the |
| regular rules of Java, it may still be called using <b>decapsulation</b> (see |
| also <a href="s3.4.html" title="§3.4 Overriding access restrictions" |
| class="sect">§3.4</a>, <a href="s2.1.2.c.html" title="§2.1.2.(c) Base class decapsulation" |
| class="sect">§2.1.2.(c)</a>). |
| <br /> |
| Note, that if the super or tsuper role is not bound, delegating the obligation to that unbound role will not work. |
| |
| </p> |
| </div> |
| <div class="subsect depth4" id="s2.4.2.c"> |
| <h4 class="subsect">(c) <span class="title">Super-call for bound roles</span><a class="img" href="s2.4.2.c.html" |
| title="PermaLink to (c) Super-call for bound roles"><img style="vertical-align:text-top;margin-left:5px;" src="../images/permalink.png" |
| alt="" /></a></h4> |
| <p>Instead of or prior to calling <code>base(..)</code> a constructor of a bound role explicitly or implicitly calls a super constructor. |
| Which constructor is applicable depends on the super role and its <code>playedBy</code> clause. |
| |
| </p> |
| <ul> |
| <li>If the super role is bound to the same base class as the current role is, |
| |
| <ul> |
| <li>not writing a super-call causes the lifting constructor of the super role to be invoked.</li> |
| <li>explicitly calling a super constructor requires the super constructor to <i>either</i><ol> |
| <li>create a role instance using a base constructor call (directly or indirectly), <i>or</i></li> |
| <li>be a lifting constructor receiving a base instance, which the current role must provide as the argument.</li> |
| </ol> |
| </li> |
| </ul> |
| </li> |
| <li>If the super role is bound but the current role refines the <code>playedBy</code> |
| relationship (cf. <a href="s2.1.c.html" title="§2.1.(c) Covariant refinement" |
| class="sect">§2.1.(c)</a>), |
| |
| <ul> |
| <li>a lifting constructor must be called explicitly passing a base object as the argument.</li> |
| </ul> |
| </li> |
| <li>If the role has an explicit or implicit super role which is unbound the constructor may optionally |
| call a super constructor (using <code>super(..)</code> or <code>tsuper(..)</code>) prior to calling |
| <code>base(..)</code>. Otherwise the default constructor is implicitly invoked. |
| |
| </li> |
| </ul> |
| <p>When invoking a lifting constructor of a super role the base object can optionally be obtained by using a base constructor |
| call as an expression: |
| |
| </p> |
| <div class="listing plain"><pre>super(base(<i><args></i>));</pre></div> |
| </div> |
| <p>The language system evaluates the base constructor by creating an |
| instance of the appropriate base class using a constructor with matching |
| signature. Also the internal links are setup that are needed for accessing the |
| base object from the role and for lifting the base object to the new role |
| in the future. |
| |
| </p> |
| <p>The syntax for base constructors follows the rule that role implementations |
| never directly refer to any names of base classes or their features. |
| |
| </p> |
| </div> |
| <div class="sect depth3" id="s2.4.3"> |
| <h3 class="sect">§2.4.3 Role creation in the presence of smart lifting<a class="img" href="s2.4.3.html" |
| title="PermaLink to §2.4.3 Role creation in the presence of smart lifting"><img style="vertical-align:text-top;margin-left:5px;" src="../images/permalink.png" |
| alt="" /></a><span class="toplink"><a href="#top">↑ §2.4</a></span></h3> |
| <p>Explicitly instantiating a role <code>R1</code> bound to a base <code>B</code> where smart lifting of <code>B</code> to <code>R1</code> would actually |
| provide a subrole <code>R2</code> is dangerous: Instantiation enters the <code>R1</code> into the team's internal cache. If at any time later lifting |
| this <code>B</code> to <code>R2</code> is requested, which is a legal request, the runtime system will answer by throwing a <code>org.objectteams.WrongRoleException</code> |
| because it finds the <code>R1</code> instead of the required <code>R2</code>. |
| For this reason, in this specific situation the explicit instantiation <code>new R1(..)</code> will be flagged by a warning. |
| The problem can be avoided by using <code>R2</code> in the instantiation expression. |
| |
| </p> |
| <h5 class="listing">Example code (WrongRoleException):</h5> |
| <div class="listing example frame"> |
| <table class="listing"> |
| <tr class="line odd"> |
| <td class="ln">1</td> |
| <td><pre><b>public</b> <b>class</b> B { <b>void</b> bm() {} }</pre></td> |
| </tr> |
| <tr class="line even"> |
| <td class="ln">2</td> |
| <td><pre><b>public</b> <b>team</b> <b>class</b> T {</pre></td> |
| </tr> |
| <tr class="line odd"> |
| <td class="ln">3</td> |
| <td><pre> <b>protected</b> <b>class</b> R1 <b>playedBy</b> B {...}</pre></td> |
| </tr> |
| <tr class="line even"> |
| <td class="ln">4</td> |
| <td><pre> <b>protected</b> <b>class</b> R2 <b>extends</b> R1 { <span class="comment">// inherits the binding to B</span></pre></td> |
| </tr> |
| <tr class="line odd"> |
| <td class="ln">5</td> |
| <td><pre> <b>void</b> rm() { <span class="comment">/* body omitted */</span> }</pre></td> |
| </tr> |
| <tr class="line even"> |
| <td class="ln">6</td> |
| <td><pre> }</pre></td> |
| </tr> |
| <tr class="line odd"> |
| <td class="ln">7</td> |
| <td><pre> <b>public</b> B getDecoratedB() {</pre></td> |
| </tr> |
| <tr class="line even"> |
| <td class="ln">8</td> |
| <td><pre> <b>return</b> <em><b>new</b> R1</em>(<b>new</b> B()); <span class="comment">// <span class="error">compile-time warning!</span></span></pre></td> |
| </tr> |
| <tr class="line odd"> |
| <td class="ln">9</td> |
| <td><pre> }</pre></td> |
| </tr> |
| <tr class="line even"> |
| <td class="ln">10</td> |
| <td><pre> <b>public</b> <b>void</b> requestLifting(B <b>as</b> R2 r) {}</pre></td> |
| </tr> |
| <tr class="line odd"> |
| <td class="ln">11</td> |
| <td><pre>}</pre></td> |
| </tr> |
| <tr class="line even"> |
| <td class="ln">12</td> |
| <td><pre><span class="comment">// plus these calls:</span></pre></td> |
| </tr> |
| <tr class="line odd"> |
| <td class="ln">13</td> |
| <td><pre>T t = <b>new</b> T();</pre></td> |
| </tr> |
| <tr class="line even"> |
| <td class="ln">14</td> |
| <td><pre>B b = t.getDecoratedB(); <span class="comment">// creates an R1 for b</span></pre></td> |
| </tr> |
| <tr class="line odd"> |
| <td class="ln">15</td> |
| <td><pre>t.requestLifting(b); <span class="comment">// => <span class="error"><code>org.objectteams.WrongRoleException!</code></span></span></pre></td> |
| </tr> |
| </table> |
| </div> |
| <div class="codecomment"> |
| <ul> |
| <li>A note on line 8: this line passes a fresh instance of <code>B</code> to the lifting constructor of <code>R1</code> |
| (see <a href="#s2.4.1.b" title="§2.4.1.(b) Fresh base object" |
| class="sect">§2.4.1.(b)</a>). In order to return this <code>B</code> instance lowering is implicitly used for the return statement. |
| </li> |
| <li>When line 15 is executed, a lifting of <code>b</code> to <code>R2</code> is requested but due to line 8 an <code>R1</code> is found in the internal cache. |
| </li> |
| </ul> |
| </div> |
| </div> |
| </div> |
| <table class="nav"> |
| <tr> |
| <td class="back"><a href="s2.3.html" rel="prev"><< §2.3 Lifting</a></td> |
| <td class="top"><a href="index.html" rel="contents">↑ Table of Contents ↑</a></td> |
| <td class="next"><a href="s2.5.html" rel="next">§2.5 Abstract Roles >></a></td> |
| </tr> |
| </table> |
| <div class="breadcrumb"><a class="nav" href="s2.html" rel="section">§2 Role Binding</a></div> |
| </div> |
| <div id="footer"> |
| <hr /><a class="w3c img" href="http://jigsaw.w3.org/css-validator/check/referer" |
| shape="rect"><img src="../images/valid-css2-blue.png" alt="Valid CSS!" height="31" width="88" /></a><a class="w3c img" href="http://validator.w3.org/check?uri=referer" shape="rect"><img src="../images/valid-xhtml10-blue.png" alt="Valid XHTML 1.0 Strict" height="31" |
| width="88" /></a><address>© Stephan Herrmann, Christine Hundt, Marco Mosconi</address> |
| OT/J version 1.3 — last modified: 2011-05-15 |
| </div> |
| </body> |
| </html> |