blob: 97d0f6c1f88cd91eefcb6321461e730eb6251167 [file] [log] [blame]
<!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="s1.4.html" rel="prev">&lt;&lt;&nbsp;&sect;1.4&nbsp;Name clashes</a></td>
<td class="top"><a href="index.html" rel="contents">&uarr;&nbsp;Table of Contents&nbsp;&uarr;</a></td>
<td class="next"></td>
</tr>
</table>
<div class="breadcrumb"><a class="nav" href="s1.html" rel="section">&sect;1&nbsp;Teams and Roles</a></div>
<div class="sect depth2" id="s1.5">
<h2 class="sect">&sect;1.5&nbsp;Team and role nesting<a class="img" href="s1.5.html"
title="PermaLink to &sect;1.5&nbsp;Team and role nesting"><img style="vertical-align:text-top;margin-left:5px;" src="../images/permalink.png"
alt="" /></a></h2>
<p>Multi-level nesting of classes is restricted only by the following rules.
</p>
<h5 class="listing">Example code (Nesting):</h5>
<div class="listing example frame" id="l1.5">
<table class="listing">
<tr class="line odd">
<td class="ln">1</td>
<td><pre><b>public</b> <b>team</b> <b>class</b> SuperOuter {</pre></td>
</tr>
<tr class="line even">
<td class="ln">2</td>
<td><pre> <b>public</b> <em><b>team</b> <b>class</b> RoleAndTeam</em> {</pre></td>
</tr>
<tr class="line odd">
<td class="ln">3</td>
<td><pre> <b>protected</b> <b>class</b> InnerRole {</pre></td>
</tr>
<tr class="line even">
<td class="ln">4</td>
<td><pre> Runnable foo() { <b>return</b> null; }</pre></td>
</tr>
<tr class="line odd">
<td class="ln">5</td>
<td><pre> }</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> <em><b>team</b> <b>class</b> RoleAndTeamSub</em> <b>extends</b> <strong>RoleAndTeam</strong> {</pre></td>
</tr>
<tr class="line even">
<td class="ln">8</td>
<td><pre> <b>protected</b> <b>class</b> InnerRole {</pre></td>
</tr>
<tr class="line odd">
<td class="ln">9</td>
<td><pre> Runnable foo() { <b>throw</b> <b>new</b> RuntimeException(); }</pre></td>
</tr>
<tr class="line even">
<td class="ln">10</td>
<td><pre> }</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>}</pre></td>
</tr>
<tr class="line odd">
<td class="ln">13</td>
<td><pre><b>public</b> <b>team</b> <b>class</b> OuterTeam <b>extends</b> SuperOuter {</pre></td>
</tr>
<tr class="line even">
<td class="ln">14</td>
<td><pre> <b>public</b> <em><b>team</b> <b>class</b> RoleAndTeam</em> {</pre></td>
</tr>
<tr class="line odd">
<td class="ln">15</td>
<td><pre> <b>protected</b> <b>class</b> InnerRole {</pre></td>
</tr>
<tr class="line even">
<td class="ln">16</td>
<td><pre> Runnable foo() {</pre></td>
</tr>
<tr class="line odd">
<td class="ln">17</td>
<td><pre> <b>class</b> Local {};</pre></td>
</tr>
<tr class="line even">
<td class="ln">18</td>
<td><pre> <b>return</b> <b>new</b> Runnable() { <span class="comment">// anonymous class definition</span></pre></td>
</tr>
<tr class="line odd">
<td class="ln">19</td>
<td><pre> <b>public</b> <b>void</b> run() {}</pre></td>
</tr>
<tr class="line even">
<td class="ln">20</td>
<td><pre> };</pre></td>
</tr>
<tr class="line odd">
<td class="ln">21</td>
<td><pre> }</pre></td>
</tr>
<tr class="line even">
<td class="ln">22</td>
<td><pre> <span class="comment">// <span class="error">class IllegalMember {}</span></span></pre></td>
</tr>
<tr class="line odd">
<td class="ln">23</td>
<td><pre> }</pre></td>
</tr>
<tr class="line even">
<td class="ln">24</td>
<td><pre> }</pre></td>
</tr>
<tr class="line odd">
<td class="ln">25</td>
<td><pre> <b>public</b> <em><b>team</b> <b>class</b> RoleAndTeamSub</em> {</pre></td>
</tr>
<tr class="line even">
<td class="ln">26</td>
<td><pre> <b>protected</b> <b>class</b> InnerRole {</pre></td>
</tr>
<tr class="line odd">
<td class="ln">27</td>
<td><pre> Runnable foo() {</pre></td>
</tr>
<tr class="line even">
<td class="ln">28</td>
<td><pre> <em>RoleAndTeamSub.tsuper</em>.foo();</pre></td>
</tr>
<tr class="line odd">
<td class="ln">29</td>
<td><pre> <b>return</b> <em>OuterTeam.tsuper</em>.foo();</pre></td>
</tr>
<tr class="line even">
<td class="ln">30</td>
<td><pre> };</pre></td>
</tr>
<tr class="line odd">
<td class="ln">31</td>
<td><pre> }</pre></td>
</tr>
<tr class="line even">
<td class="ln">32</td>
<td><pre> }</pre></td>
</tr>
<tr class="line odd">
<td class="ln">33</td>
<td><pre>}</pre></td>
</tr>
</table>
</div>
<div class="subsect depth3" id="s1.5.a">
<h4 class="subsect">(a)&nbsp;<span class="title">Nested teams</span><a class="img" href="s1.5.a.html" title="PermaLink to (a)&nbsp;Nested teams"><img style="vertical-align:text-top;margin-left:5px;" src="../images/permalink.png"
alt="" /></a></h4>
<p>If a role class is also marked using the <code>team</code> modifier,
it may contain roles at the next level of nesting.
</p>
<div class="codecomment">
<ul>
<li>In the above example (<a href="#l1.5" class="listing">Listing 1.5</a>) class <code>RoleAndTeam</code> starting in line 14
is a role of <code>OuterTeam</code> and at the same time a
team containing a further role <code>InnerRole</code></li>
</ul>
</div>
<p>Such a hybrid role-and-team has all properties of both kinds of classes.
</p>
</div>
<div class="subsect depth3" id="s1.5.b">
<h4 class="subsect">(b)&nbsp;<span class="title">Nested classes of roles</span><a class="img" href="s1.5.b.html"
title="PermaLink to (b)&nbsp;Nested classes of roles"><img style="vertical-align:text-top;margin-left:5px;" src="../images/permalink.png"
alt="" /></a></h4>
<p>A regular role class (ie., not marked as <code>team</code>, see above)
may contain local types (see <a href="http://java.sun.com/docs/books/jls/second_edition/html/statements.doc.html#247766"
class="ext">JLS &sect;14.3</a>
- in the example: class <code>Local</code>), anonymous types
(<a href="http://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html#252986"
class="ext">JLS &sect;15.9.5</a>
- in the example: class defined in lines 18-20)
but no member types (<a href="http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html#246026"
class="ext">JLS &sect;8.5</a>
- in the example: illegal class
<code>IllegalMember</code>).
<br />
The effect is, that nested types of a regular role cannot be
used outside the scope of their enclosing role.
</p>
</div>
<div class="subsect depth3" id="s1.5.c">
<h4 class="subsect">(c)&nbsp;<span class="title">Prohibition of cycles</span><a class="img" href="s1.5.c.html"
title="PermaLink to (c)&nbsp;Prohibition of cycles"><img style="vertical-align:text-top;margin-left:5px;" src="../images/permalink.png"
alt="" /></a></h4>
<p>A nested team may not extend its own enclosing team.
</p>
</div>
<div class="subsect depth3" id="s1.5.d">
<h4 class="subsect">(d)&nbsp;<span class="title">Prohibition of name clashes</span><a class="img" href="s1.5.d.html"
title="PermaLink to (d)&nbsp;Prohibition of name clashes"><img style="vertical-align:text-top;margin-left:5px;" src="../images/permalink.png"
alt="" /></a></h4>
<p>A nested team may inherit roles from multiple sources: its explicit super team
and any of its implicit super classes (roles) from different levels of nesting.
If from different sources a team inherits two or more roles of the same name
that are not related by implicit inheritance, this is an illegal name clash.
</p>
</div>
<div class="subsect depth3" id="s1.5.e">
<h4 class="subsect">(e)&nbsp;<span class="title">Precedence among different supers</span><a class="img" href="s1.5.e.html"
title="PermaLink to (e)&nbsp;Precedence among different supers"><img style="vertical-align:text-top;margin-left:5px;" src="../images/permalink.png"
alt="" /></a></h4>
<p>If a role inherits the same feature from several super roles (super and tsuper),
an implicitly inherited version always overrides any explicitly inherited feature,
i.e., a role with the same simple name is closer related than one with a different name.
</p>
<p>
Also implicit inheritance alone may produce several candidate methods inherited by a role class.
This is a result of team-nesting where each level of nesting may add one more tsuper role
if outer teams also participate in an inheritance relationship.
In this case a role inherited from an <em>implicit</em> super team of the enclosing team
is closer related than a role inherited from an <em>explicit</em> super team.
If necessary this rule is applied inside out until a nesting level is found where indeed
explicit team inheritance is involved.<br />
So when comparing classes by their fully qualified names
the longest common suffix will determine the closest relationship.
E.g., <code>SuperOuter.RoleAndTeamSub.InnerRole</code>
is the closest ancestor of <code>SubOuter.RoleAndTeamSub.InnerRole</code>
because both share the name suffix <code>RoleAndTeamSub.InnerRole</code>.
</p>
<div class="codecomment">
<table>
<colgroup span="1">
<col align="left" span="1" />
<col align="center" span="1" />
</colgroup>
<tr>
<td valign="top" rowspan="1" colspan="1">
<p>In the above example (<a href="#l1.5" class="listing">Listing 1.5</a>) role <code class="small">OuterTeam.RoleAndTeamSub.InnerRole</code>
has two direct tsuper roles:
<code class="small">OuterTeam.RoleAndTeam.InnerRole</code>&nbsp;and <code class="small">SuperOuter.RoleAndTeamSub.InnerRole</code>.
Without the method <code>foo</code> defined in lines 27-30, the enclosing class <code class="small">OuterTeam.RoleAndTeamSub.InnerRole</code>
would inherit the method <code>foo</code> defined in <code>SuperOuter.RoleAndTeamSub.InnerRole</code> (line 9),
because the common name suffix <code>RoleAndTeamSub.InnerRole</code>
creates a stronger relationship making that class the closest ancestor.
</p>
</td>
<td rowspan="1" colspan="1"><img src="../images/team_nesting_hor.png" alt="Example diagram team nesting" /></td>
</tr>
</table>
</div>
</div>
<div class="subsect depth3" id="s1.5.f">
<h4 class="subsect">(f)&nbsp;<span class="title">Qualified tsuper</span><a class="img" href="s1.5.f.html" title="PermaLink to (f)&nbsp;Qualified tsuper"><img style="vertical-align:text-top;margin-left:5px;" src="../images/permalink.png"
alt="" /></a></h4>
<p>A role in a nested team may qualify the keyword <code>tsuper</code> (see <a href="s1.3.1.f.html" title="&sect;1.3.1.(f)&nbsp;tsuper" class="sect">&sect;1.3.1.(f)</a> above) by a type name
in order to select among different implicit super classes.
A term <code>OuterTeam.tsuper</code> evaluates to a corresponding implicit super class
within the context of the explicit super-class (here: <code>SuperOuter</code>) of the enclosing team "<code>OuterTeam</code>".
A method call <code>OuterTeam.tsuper.m()</code>
evaluates to the method version within <code>SuperOuter</code> that best corresponds to the current method containing the tsuper-call.
</p>
<div class="codecomment">
<ul>
<li>In the above example (<a href="#l1.5" class="listing">Listing 1.5</a>) line 28 selects the method version
within the superclass of <code>RoleAndTeamSub</code> (i.e., within <code>RoleAndTeam</code>),
resolving to <code>OuterTeam.RoleAndTeam.InnerRole.foo()</code>.
</li>
<li>Line 29 selects a corresponding method from the context of <code>SuperOuter</code> resolving to <code>SuperOuter.RoleAndTeamSub.InnerRole.foo()</code>
which has the same semantics as an unqualified <code>tsuper</code> call would have.
</li>
</ul>
</div>
</div>
</div>
<table class="nav">
<tr>
<td class="back"><a href="s1.4.html" rel="prev">&lt;&lt;&nbsp;&sect;1.4&nbsp;Name clashes</a></td>
<td class="top"><a href="index.html" rel="contents">&uarr;&nbsp;Table of Contents&nbsp;&uarr;</a></td>
<td class="next"></td>
</tr>
</table>
<div class="breadcrumb"><a class="nav" href="s1.html" rel="section">&sect;1&nbsp;Teams and Roles</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>&copy; Stephan Herrmann, Christine Hundt, Marco Mosconi</address>
OT/J version 1.3 &mdash; last modified: 2011-05-15
</div>
</body>
</html>