Stephan Herrmann | 0166441 | 2010-04-01 20:28:43 +0000 | [diff] [blame] | 1 | <!DOCTYPE html |
| 2 | PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "../xhtml1-strict.dtd"> |
| 3 | <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> |
| 4 | <head> |
| 5 | <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> |
Stephan Herrmann | 0166441 | 2010-04-01 20:28:43 +0000 | [diff] [blame] | 6 | <link rel="stylesheet" type="text/css" href="../css/ot.css" /> |
| 7 | <link rel="stylesheet" type="text/css" href="../css/otjld.css" /> |
| 8 | <title>OT/J Language Definition v1.3</title> |
| 9 | </head> |
| 10 | <body class="otdt"> |
| 11 | <div id="content"> |
| 12 | <table class="nav"> |
| 13 | <tr> |
| 14 | <td class="back"><a id="top"></a><a href="s2.html" rel="prev"><< §2 Role Binding</a></td> |
| 15 | <td class="top"><a href="index.html" rel="contents">↑ Table of Contents ↑</a></td> |
| 16 | <td class="next"><a href="s4.html" rel="next">§4 Callin Binding >></a></td> |
| 17 | </tr> |
| 18 | </table> |
| 19 | <div class="breadcrumb"></div> |
| 20 | <div class="chapter" id="s3"> |
| 21 | <div class="headl"> |
| 22 | <div class="headr"> |
| 23 | <h1>§3 Callout Binding</h1> |
| 24 | </div> |
| 25 | </div> |
| 26 | <div id="toc-box"> |
| 27 | <ul class="toc-box"> |
| 28 | <li><a href="s3.html">§3 Callout Binding</a></li> |
| 29 | <li><a href="#s3.1">§3.1 Callout method binding</a></li> |
| 30 | <li><a href="#s3.2">§3.2 Callout parameter mapping</a></li> |
| 31 | <li><a href="#s3.3">§3.3 Lifting and lowering</a></li> |
| 32 | <li><a href="#s3.4">§3.4 Overriding access restrictions</a></li> |
| 33 | <li><a href="#s3.5">§3.5 Callout to field</a></li> |
| 34 | </ul> |
| 35 | </div> |
| 36 | <div class="intro"> |
| 37 | <h3>Notion of callout binding</h3> |
| 38 | <div class="line"></div> |
| 39 | <div class="term">callout binding</div> |
| 40 | <div class="termdesc">A callout binding declares that a method call to a role |
| 41 | object may be <strong>forwarded</strong> to a base method of the associated |
| 42 | base object <em>(the role object "calls out" to the base)</em>. |
| 43 | </div> |
| 44 | <div class="line"></div> |
| 45 | <div class="term">declarative completeness</div> |
| 46 | <div class="termdesc"> Even if a role class does not implement all needed methods, |
| 47 | but forwards some to its base, also these methods must be declared |
| 48 | within the role. |
| 49 | Secondly, no forwarding occurs, unless explicitly declared by a callout binding. |
| 50 | </div> |
| 51 | <div class="line"></div> |
| 52 | <div class="term">expected/provided</div> |
| 53 | <div class="termdesc"> A callout binding binds an <strong>expected</strong> method of the role |
| 54 | class (needed but not implemented here) to a <strong>provided</strong> |
| 55 | method of the base class. |
| 56 | </div> |
| 57 | <div class="line"></div> |
| 58 | </div> |
| 59 | <div class="sect depth2" id="s3.1"> |
Stephan Herrmann | 109893e | 2010-05-18 22:02:53 +0000 | [diff] [blame] | 60 | <h2 class="sect">§3.1 Callout method binding<a class="img" href="s3.1.html" |
| 61 | title="PermaLink to §3.1 Callout method binding"><img style="vertical-align:text-top;margin-left:5px;" src="../images/permalink.png" |
| 62 | alt="" /></a><span class="toplink"><a href="#top">↑ §3</a></span></h2> |
Stephan Herrmann | 0166441 | 2010-04-01 20:28:43 +0000 | [diff] [blame] | 63 | <div class="syntaxlink"><a href="sA.html#sA.3.2" title="§A.3.2 CalloutBinding" |
| 64 | class="syntax">→ Syntax §A.3.2</a></div> |
| 65 | <p>A role class may acquire the implementation for any of its |
| 66 | (expected) methods by declaring a <strong>callout</strong> binding. |
| 67 | |
| 68 | </p> |
| 69 | <div class="subsect depth3" id="s3.1.a"> |
Stephan Herrmann | 109893e | 2010-05-18 22:02:53 +0000 | [diff] [blame] | 70 | <h4 class="subsect">(a) <span class="title">Prerequisite: Class binding</span><a class="img" href="s3.1.a.html" |
| 71 | title="PermaLink to (a) Prerequisite: Class binding"><img style="vertical-align:text-top;margin-left:5px;" src="../images/permalink.png" |
| 72 | alt="" /></a></h4> |
Stephan Herrmann | 0166441 | 2010-04-01 20:28:43 +0000 | [diff] [blame] | 73 | <p>A callout binding requires the enclosing class to be a role class |
| 74 | bound to a base class according to <a href="s2.1.html" title="§2.1 playedBy relation" class="sect">§2.1</a>. |
| 75 | |
| 76 | </p> |
| 77 | </div> |
| 78 | <div class="subsect depth3" id="s3.1.b"> |
Stephan Herrmann | 109893e | 2010-05-18 22:02:53 +0000 | [diff] [blame] | 79 | <h4 class="subsect">(b) <span class="title">Definition</span><a class="img" href="s3.1.b.html" title="PermaLink to (b) Definition"><img style="vertical-align:text-top;margin-left:5px;" src="../images/permalink.png" |
| 80 | alt="" /></a></h4> |
Stephan Herrmann | 0166441 | 2010-04-01 20:28:43 +0000 | [diff] [blame] | 81 | <p>A callout binding maps an abstract role method ("expected method") |
| 82 | to a concrete base method ("provided method"). |
| 83 | It may appear within the role class at any place where feature |
| 84 | declarations are allowed. It is denoted by |
| 85 | |
| 86 | </p> |
| 87 | <div class="listing plain"><pre><i>expected_method_designator</i> <b>-></b> <i>provided_method_designator;</i></pre></div> |
| 88 | <p>The effect is that any call to the role method will be forwarded to the |
| 89 | associated base object using the provided base method. |
| 90 | |
| 91 | </p> |
| 92 | <h5 class="listing">Example code (Callout):</h5> |
| 93 | <div class="listing example frame"> |
| 94 | <table class="listing"> |
| 95 | <tr class="line odd"> |
| 96 | <td class="ln">1</td> |
| 97 | <td><pre><b>team</b> <b>class</b> Company {</pre></td> |
| 98 | </tr> |
| 99 | <tr class="line even"> |
| 100 | <td class="ln">2</td> |
| 101 | <td><pre> <b>public</b> <b>class</b> Employee <b>playedBy</b> Person {</pre></td> |
| 102 | </tr> |
| 103 | <tr class="line odd"> |
| 104 | <td class="ln">3</td> |
| 105 | <td><pre> <b>abstract</b> String getIdentification();</pre></td> |
| 106 | </tr> |
| 107 | <tr class="line even"> |
| 108 | <td class="ln">4</td> |
| 109 | <td><pre> <span class="comment">// callout binding see below...</span></pre></td> |
| 110 | </tr> |
| 111 | <tr class="line odd"> |
| 112 | <td class="ln">5</td> |
| 113 | <td><pre> }</pre></td> |
| 114 | </tr> |
| 115 | <tr class="line even"> |
| 116 | <td class="ln">6</td> |
| 117 | <td><pre>}</pre></td> |
| 118 | </tr> |
| 119 | </table> |
| 120 | </div> |
| 121 | </div> |
| 122 | <div class="subsect depth3" id="s3.1.c"> |
Stephan Herrmann | 109893e | 2010-05-18 22:02:53 +0000 | [diff] [blame] | 123 | <h4 class="subsect">(c) <span class="title">Kinds of method designators</span><a class="img" href="s3.1.c.html" |
| 124 | title="PermaLink to (c) Kinds of method designators"><img style="vertical-align:text-top;margin-left:5px;" src="../images/permalink.png" |
| 125 | alt="" /></a></h4> |
Stephan Herrmann | 0166441 | 2010-04-01 20:28:43 +0000 | [diff] [blame] | 126 | <p>A method designator may either be a method name |
| 127 | |
| 128 | </p> |
| 129 | <div class="listing example frame"> |
| 130 | <table class="listing"> |
| 131 | <tr class="line odd"> |
| 132 | <td class="ln">4</td> |
| 133 | <td><pre>getIdentification <em>-></em> getName;</pre></td> |
| 134 | </tr> |
| 135 | </table> |
| 136 | </div> |
| 137 | <p><strong>or</strong> |
| 138 | a complete method signature including parameter declarations and |
| 139 | return type declaration, but excluding any modifiers and declared exceptions. |
| 140 | |
| 141 | </p> |
| 142 | <div class="listing example frame"> |
| 143 | <table class="listing"> |
| 144 | <tr class="line odd"> |
| 145 | <td class="ln">4</td> |
| 146 | <td><pre>String getIdentification() <em>-></em> String getName();</pre></td> |
| 147 | </tr> |
| 148 | </table> |
| 149 | </div> |
| 150 | <div class="codecomment"> |
| 151 | <h5>Effects:</h5> |
| 152 | <ul> |
| 153 | <li> Line 4 declares a callout binding for the role method <code>getIdentification()</code>, |
| 154 | providing an implementation for the abstract method defined in line 3. |
| 155 | </li> |
| 156 | <li> In combination with the role binding in line 2 this has the following effect:</li> |
| 157 | <li> Any call to <code>Employee.getIdentification</code> |
| 158 | is forwarded to the method <code>Person.getName</code>. |
| 159 | </li> |
| 160 | </ul> |
| 161 | </div> |
| 162 | <p>Both sides of a callout binding must use the same kind of |
| 163 | designators, i.e., designators with and without signature may not be mixed. |
| 164 | <br /> |
| 165 | Each method designator must uniquely select one method. |
| 166 | If a method designator contains a signature this signature must match exactly with the signature |
| 167 | of an existing method, i.e., no implicit conversions are applied for this matching. |
| 168 | If overloading is involved, signatures <em>must</em> be used to disambiguate. |
| 169 | |
| 170 | </p> |
| 171 | </div> |
| 172 | <div class="subsect depth3" id="s3.1.d"> |
Stephan Herrmann | 109893e | 2010-05-18 22:02:53 +0000 | [diff] [blame] | 173 | <h4 class="subsect">(d) <span class="title">Inheritance of role method declarations</span><a class="img" href="s3.1.d.html" |
| 174 | title="PermaLink to (d) Inheritance of role method declarations"><img style="vertical-align:text-top;margin-left:5px;" src="../images/permalink.png" |
| 175 | alt="" /></a></h4> |
Stephan Herrmann | 0166441 | 2010-04-01 20:28:43 +0000 | [diff] [blame] | 176 | <p>The role method being bound by a callout may be declared in the same |
| 177 | class as the binding or it may be inherited from a super class or |
| 178 | super interface. |
| 179 | |
| 180 | </p> |
| 181 | </div> |
| 182 | <div class="subsect depth3" id="s3.1.e"> |
Stephan Herrmann | 109893e | 2010-05-18 22:02:53 +0000 | [diff] [blame] | 183 | <h4 class="subsect">(e) <span class="title">Callout override</span><a class="img" href="s3.1.e.html" title="PermaLink to (e) Callout override"><img style="vertical-align:text-top;margin-left:5px;" src="../images/permalink.png" |
| 184 | alt="" /></a></h4> |
Stephan Herrmann | 0166441 | 2010-04-01 20:28:43 +0000 | [diff] [blame] | 185 | <p>If an inherited role method is concrete, callout binding regarding this |
| 186 | method must use the token "<code>=></code>" instead of "<code>-></code>" |
| 187 | in order to declare that this binding overrides an existing implementation. |
| 188 | <br /> |
| 189 | |
| 190 | Using the "<code>=></code>" operator for an abstract method is an error. |
| 191 | <br /> |
| 192 | It is also an error (and not useful anyway) to callout-bind a method that is |
| 193 | implemented in the same class as the binding. |
| 194 | |
| 195 | </p> |
| 196 | </div> |
| 197 | <div class="subsect depth3" id="s3.1.f"> |
Stephan Herrmann | 109893e | 2010-05-18 22:02:53 +0000 | [diff] [blame] | 198 | <h4 class="subsect">(f) <span class="title">Inheritance of callout bindings</span><a class="img" href="s3.1.f.html" |
| 199 | title="PermaLink to (f) Inheritance of callout bindings"><img style="vertical-align:text-top;margin-left:5px;" src="../images/permalink.png" |
| 200 | alt="" /></a></h4> |
Stephan Herrmann | 0166441 | 2010-04-01 20:28:43 +0000 | [diff] [blame] | 201 | <p> Callout bindings are inherited along explicit and implicit inheritance. |
| 202 | Inherited callout bindings can be overridden using "<code>=></code>". |
| 203 | |
| 204 | </p> |
| 205 | </div> |
| 206 | <div class="subsect depth3" id="s3.1.g"> |
Stephan Herrmann | 109893e | 2010-05-18 22:02:53 +0000 | [diff] [blame] | 207 | <h4 class="subsect">(g) <span class="title">Duplicate bindings</span><a class="img" href="s3.1.g.html" |
| 208 | title="PermaLink to (g) Duplicate bindings"><img style="vertical-align:text-top;margin-left:5px;" src="../images/permalink.png" |
| 209 | alt="" /></a></h4> |
Stephan Herrmann | 0166441 | 2010-04-01 20:28:43 +0000 | [diff] [blame] | 210 | <p>It is an error if a role class has multiple callout bindings for the |
| 211 | same role method. |
| 212 | |
| 213 | </p> |
| 214 | </div> |
| 215 | <div class="subsect depth3" id="s3.1.h"> |
Stephan Herrmann | 109893e | 2010-05-18 22:02:53 +0000 | [diff] [blame] | 216 | <h4 class="subsect">(h) <span class="title">Declared exceptions</span><a class="img" href="s3.1.h.html" |
| 217 | title="PermaLink to (h) Declared exceptions"><img style="vertical-align:text-top;margin-left:5px;" src="../images/permalink.png" |
| 218 | alt="" /></a></h4> |
Stephan Herrmann | 0166441 | 2010-04-01 20:28:43 +0000 | [diff] [blame] | 219 | <p>It is an error if a base method to be bound by <strong>callout</strong> |
| 220 | declares in its <code>throws</code> clause any exceptions that |
| 221 | are not declared by the corresponding role method. |
| 222 | |
| 223 | </p> |
| 224 | </div> |
| 225 | <div class="subsect depth3" id="s3.1.i"> |
Stephan Herrmann | 109893e | 2010-05-18 22:02:53 +0000 | [diff] [blame] | 226 | <h4 class="subsect">(i) <span class="title">Shorthand definition</span><a class="img" href="s3.1.i.html" |
| 227 | title="PermaLink to (i) Shorthand definition"><img style="vertical-align:text-top;margin-left:5px;" src="../images/permalink.png" |
| 228 | alt="" /></a></h4> |
Stephan Herrmann | 0166441 | 2010-04-01 20:28:43 +0000 | [diff] [blame] | 229 | <p> A callout binding whose method designators specify |
| 230 | full method signatures does not require an existing role method. |
| 231 | If no role method is found matching the expected method of |
| 232 | such a callout binding, a new method is implicitly generated. |
| 233 | The new method is static iff the bound base method is static, |
| 234 | and it declares the same exceptions as the bound base method. |
| 235 | |
| 236 | </p> |
| 237 | <p> |
| 238 | A shorthand callout may optionally declare a <strong>visibility modifier</strong>, |
| 239 | otherwise the generated method inherits the visibility modifier of the bound base method. |
| 240 | No further modifiers are set. |
| 241 | If a callout overrides an inherited method or callout, |
| 242 | it must not reduce the visibility of the inherited method/callout. |
| 243 | |
| 244 | </p> |
| 245 | </div> |
| 246 | <div class="subsect depth3" id="s3.1.j"> |
Stephan Herrmann | 109893e | 2010-05-18 22:02:53 +0000 | [diff] [blame] | 247 | <h4 class="subsect">(j) <span class="title">Inferred callout</span><a class="img" href="s3.1.j.html" title="PermaLink to (j) Inferred callout"><img style="vertical-align:text-top;margin-left:5px;" src="../images/permalink.png" |
| 248 | alt="" /></a></h4> |
Stephan Herrmann | 0166441 | 2010-04-01 20:28:43 +0000 | [diff] [blame] | 249 | <p> If a non-abstract role class inherits an abstract method the compiler |
| 250 | tries to infer a callout binding for implementing the abstract method. |
| 251 | Similarly, if a self-call in a role class cannot be resolved, the compiler |
| 252 | tries to infer a callout to resolve the self-call.<br /> |
| 253 | Inference searches for a method in the bound base class such that |
| 254 | |
| 255 | </p> |
| 256 | <ol> |
| 257 | <li>both methods have the same name</li> |
| 258 | <li>both methods have the same number of arguments</li> |
| 259 | <li>each argument of the abstract role method is compatible to the |
| 260 | corresponding argument of the base method directly, or using |
| 261 | boxing/unboxing or lowering. |
| 262 | </li> |
| 263 | </ol> |
| 264 | <p> |
| 265 | Callouts inferred from an interface have <code>public</code> visibility, |
| 266 | callouts inferred from a self-call have <code>private</code> visibility. |
| 267 | |
| 268 | </p> |
| 269 | <p> |
| 270 | Per default inferred callout bindings are disabled, i.e., a compiler |
| 271 | must report these as an error. However, a compiler should allow to |
| 272 | configure reporting to produce a warning only (which can be suppressed |
| 273 | using a <code>@SuppressWarnings("inferredcallout")</code> annotation), |
| 274 | or to completely ignore the diagnostic. |
| 275 | |
| 276 | </p> |
| 277 | </div> |
| 278 | <div class="subsect depth3" id="s3.1.k"> |
Stephan Herrmann | 109893e | 2010-05-18 22:02:53 +0000 | [diff] [blame] | 279 | <h4 class="subsect">(k) <span class="title">Callout to generic method</span><a class="img" href="s3.1.k.html" |
| 280 | title="PermaLink to (k) Callout to generic method"><img style="vertical-align:text-top;margin-left:5px;" src="../images/permalink.png" |
| 281 | alt="" /></a></h4> |
Stephan Herrmann | 0166441 | 2010-04-01 20:28:43 +0000 | [diff] [blame] | 282 | <p>When referring to a generic base method</p> |
| 283 | <div class="listing example frame"> |
| 284 | <table class="listing"> |
| 285 | <tr class="line odd"> |
| 286 | <td class="ln">1</td> |
| 287 | <td><pre><T> T bm(T a)</pre></td> |
| 288 | </tr> |
| 289 | </table> |
| 290 | </div> |
| 291 | <p>a callout binding may either propagate the method's genericity as in</p> |
| 292 | <div class="listing example frame"> |
| 293 | <table class="listing"> |
| 294 | <tr class="line odd"> |
| 295 | <td class="ln">2</td> |
| 296 | <td><pre><T> T rm(T a) <b>-></b> T bm(T a);</pre></td> |
| 297 | </tr> |
| 298 | </table> |
| 299 | </div> |
| 300 | <p>or it may supply a valid substitution for the type parameter as in</p> |
| 301 | <div class="listing example frame"> |
| 302 | <table class="listing"> |
| 303 | <tr class="line odd"> |
| 304 | <td class="ln">2</td> |
| 305 | <td><pre>String rm(String a) <b>-></b> String bm(String a);</pre></td> |
| 306 | </tr> |
| 307 | </table> |
| 308 | </div> |
| 309 | </div> |
| 310 | <p>A callout binding either attaches an implementation to a previously declared method |
| 311 | or adds (<a href="#s3.1.i" title="§3.1.(i) Shorthand definition" class="sect">§3.1.(i)</a> above) a forwarding method to a role class. |
| 312 | Apart from this implementation, callout-bound methods do not differ from regular methods. |
| 313 | |
| 314 | </p> |
| 315 | <p>When we say, a callout binding defines <strong>forwarding</strong> this means that |
| 316 | control is passed to the base object. In contrast, by a <strong>delegation</strong> |
| 317 | semantics control <em>would</em> remain at the role object, such that self-calls |
| 318 | would again be dispatched starting at the role. Callout bindings on |
| 319 | their own do not support delegation. However, in conjunction with method |
| 320 | overriding by means of callin bindings (see <a href="s4.html" title="§4 Callin Binding" class="sect">§4</a>) |
| 321 | the effect of delegation can easily be achieved. |
| 322 | |
| 323 | </p> |
| 324 | </div> |
| 325 | <div class="sect depth2" id="s3.2"> |
Stephan Herrmann | 109893e | 2010-05-18 22:02:53 +0000 | [diff] [blame] | 326 | <h2 class="sect">§3.2 Callout parameter mapping<a class="img" href="s3.2.html" |
| 327 | title="PermaLink to §3.2 Callout parameter mapping"><img style="vertical-align:text-top;margin-left:5px;" src="../images/permalink.png" |
| 328 | alt="" /></a><span class="toplink"><a href="#top">↑ §3</a></span></h2> |
Stephan Herrmann | 0166441 | 2010-04-01 20:28:43 +0000 | [diff] [blame] | 329 | <div class="syntaxlink"><a href="sA.html#sA.4.1" title="§A.4.1 CalloutParameterMappings" |
| 330 | class="syntax">→ Syntax §A.4.1</a></div> |
| 331 | <div class="subsect depth3" id="s3.2.a"> |
Stephan Herrmann | 109893e | 2010-05-18 22:02:53 +0000 | [diff] [blame] | 332 | <h4 class="subsect">(a) <span class="title">with clause</span><a class="img" href="s3.2.a.html" title="PermaLink to (a) with clause"><img style="vertical-align:text-top;margin-left:5px;" src="../images/permalink.png" |
| 333 | alt="" /></a></h4> |
Stephan Herrmann | 0166441 | 2010-04-01 20:28:43 +0000 | [diff] [blame] | 334 | <p> If the method designators in a callout binding are signatures |
| 335 | (not just method names), parameters and return value may |
| 336 | be mapped by a <code>with{...}</code> sub-clause. Parameter mappings may only occur if the enclosing role is a class, not an interface. |
| 337 | |
| 338 | </p> |
| 339 | </div> |
| 340 | <div class="subsect depth3" id="s3.2.b"> |
Stephan Herrmann | 109893e | 2010-05-18 22:02:53 +0000 | [diff] [blame] | 341 | <h4 class="subsect">(b) <span class="title">Mapping one parameter</span><a class="img" href="s3.2.b.html" |
| 342 | title="PermaLink to (b) Mapping one parameter"><img style="vertical-align:text-top;margin-left:5px;" src="../images/permalink.png" |
| 343 | alt="" /></a></h4> |
Stephan Herrmann | 0166441 | 2010-04-01 20:28:43 +0000 | [diff] [blame] | 344 | <p> For each parameter of the provided base method, exactly one parameter |
| 345 | mapping defines, which value will actually be passed to |
| 346 | the base method. Callout parameter mappings have this form: |
| 347 | |
| 348 | </p> |
| 349 | <div class="listing plain"><pre><i>expression</i> <b>-></b> <i>base_method_parameter_name</i></pre></div> |
| 350 | </div> |
| 351 | <div class="subsect depth3" id="s3.2.c"> |
Stephan Herrmann | 109893e | 2010-05-18 22:02:53 +0000 | [diff] [blame] | 352 | <h4 class="subsect">(c) <span class="title">Result mapping</span><a class="img" href="s3.2.c.html" title="PermaLink to (c) Result mapping"><img style="vertical-align:text-top;margin-left:5px;" src="../images/permalink.png" |
| 353 | alt="" /></a></h4> |
Stephan Herrmann | 0166441 | 2010-04-01 20:28:43 +0000 | [diff] [blame] | 354 | <p>The return value of a callout method may be provided by a result mapping: |
| 355 | |
| 356 | </p> |
| 357 | <div class="listing plain"><pre>result <b><-</b> <i>expression</i></pre></div> |
| 358 | <p>The right hand side expression of a result mapping may use the special identifier |
| 359 | <code>result</code> to refer to the value returned by the base method. |
| 360 | <br /> |
| 361 | In a method binding with parameter mappings, it is an error to use |
| 362 | <code>result</code> as the name of a regular method argument. |
| 363 | |
| 364 | </p> |
| 365 | <h5 class="listing">Example code (Callout Parameter Mapping):</h5> |
| 366 | <div class="listing example frame"> |
| 367 | <table class="listing"> |
| 368 | <tr class="line odd"> |
| 369 | <td class="ln">1</td> |
| 370 | <td><pre>Integer absoluteValue(Integer integer) <b>-></b> <b>int</b> abs(<b>int</b> i) <em><b>with</b> {</em></pre></td> |
| 371 | </tr> |
| 372 | <tr class="line even"> |
| 373 | <td class="ln">2</td> |
| 374 | <td><pre> integer.intValue() <b>-></b> i,</pre></td> |
| 375 | </tr> |
| 376 | <tr class="line odd"> |
| 377 | <td class="ln">3</td> |
| 378 | <td><pre> <em>result</em> <b><-</b> <b>new</b> Integer(<em>result</em>)</pre></td> |
| 379 | </tr> |
| 380 | <tr class="line even"> |
| 381 | <td class="ln">4</td> |
| 382 | <td><pre><em>}</em></pre></td> |
| 383 | </tr> |
| 384 | </table> |
| 385 | </div> |
| 386 | </div> |
| 387 | <div class="subsect depth3" id="s3.2.d"> |
Stephan Herrmann | 109893e | 2010-05-18 22:02:53 +0000 | [diff] [blame] | 388 | <h4 class="subsect">(d) <span class="title">Visible names</span><a class="img" href="s3.2.d.html" title="PermaLink to (d) Visible names"><img style="vertical-align:text-top;margin-left:5px;" src="../images/permalink.png" |
| 389 | alt="" /></a></h4> |
Stephan Herrmann | 0166441 | 2010-04-01 20:28:43 +0000 | [diff] [blame] | 390 | <p>Each identifier that appears within the expressions of a parameter |
| 391 | mapping must be either: |
| 392 | |
| 393 | </p> |
| 394 | <ul> |
| 395 | <li>a feature visible in the scope of the role instance.</li> |
| 396 | <li>a parameter of the role method (for parameter mappings).</li> |
| 397 | <li>the special name <code>result</code> (for result mappings). |
| 398 | </li> |
| 399 | <li>in a result mapping also the special name <code>base</code> can be used |
| 400 | in order to refer to the bound base instance (provided the method being |
| 401 | bound is not static). |
| 402 | </li> |
| 403 | </ul> |
| 404 | <p>The names of base method arguments (i.e., names after mapping) are only |
| 405 | legal in the position given in <a href="#s3.2.b" title="§3.2.(b) Mapping one parameter" |
| 406 | class="sect">§3.2.(b)</a>. |
| 407 | |
| 408 | </p> |
| 409 | </div> |
| 410 | <div class="subsect depth3" id="s3.2.e"> |
Stephan Herrmann | 109893e | 2010-05-18 22:02:53 +0000 | [diff] [blame] | 411 | <h4 class="subsect">(e) <span class="title">Implicit parameter mappings</span><a class="img" href="s3.2.e.html" |
| 412 | title="PermaLink to (e) Implicit parameter mappings"><img style="vertical-align:text-top;margin-left:5px;" src="../images/permalink.png" |
| 413 | alt="" /></a></h4> |
Stephan Herrmann | 0166441 | 2010-04-01 20:28:43 +0000 | [diff] [blame] | 414 | <p>If parameter mappings should be omitted the following conditions must hold: |
| 415 | |
| 416 | </p> |
| 417 | <ol> |
| 418 | <li>each method parameter of the role method must conform to the |
| 419 | corresponding parameter of the base method, <em>and</em></li> |
| 420 | <li>the result type of the base method must conform to the result type |
| 421 | of the role method. |
| 422 | </li> |
| 423 | </ol> |
| 424 | <p>Here conformance includes translation polymorphism (cf. <a href="#s3.3.d" title="§3.3.(d) Typing rules" class="sect">§3.3.(d)</a>). |
| 425 | <br /> |
| 426 | Parameter correspondence without parameter mapping is determined by declaration order not by names. |
| 427 | <br /> |
| 428 | Two adjustments can, however, be performed implicitly: |
| 429 | |
| 430 | </p> |
| 431 | <ul> |
| 432 | <li>If the role method has more parameters than the base method, |
| 433 | unused trailing parameters may be silently ignored. |
| 434 | </li> |
| 435 | <li>If the role method returns <code>void</code>, any result from |
| 436 | the base method may be silently ignored. |
| 437 | </li> |
| 438 | </ul> |
| 439 | </div> |
| 440 | <h5 class="listing">Example code (Callout with Parameter Mapping):</h5> |
| 441 | <div class="listing example frame"> |
| 442 | <table class="listing"> |
| 443 | <tr class="line odd"> |
| 444 | <td class="ln">1</td> |
| 445 | <td><pre><b>public</b> <b>team</b> <b>class</b> MyTeamA {</pre></td> |
| 446 | </tr> |
| 447 | <tr class="line even"> |
| 448 | <td class="ln">2</td> |
| 449 | <td><pre> <b>public</b> <b>abstract</b> <b>class</b> Role1 {</pre></td> |
| 450 | </tr> |
| 451 | <tr class="line odd"> |
| 452 | <td class="ln">3</td> |
| 453 | <td><pre> <b>abstract</b> <b>void</b> payEuro(float euro);</pre></td> |
| 454 | </tr> |
| 455 | <tr class="line even"> |
| 456 | <td class="ln">4</td> |
| 457 | <td><pre> <b>abstract</b> float earnEuro();</pre></td> |
| 458 | </tr> |
| 459 | <tr class="line odd"> |
| 460 | <td class="ln">5</td> |
| 461 | <td><pre> <b>void</b> idle(<b>int</b> seconds) { <span class="comment">/* do nothing */</span> };</pre></td> |
| 462 | </tr> |
| 463 | <tr class="line even"> |
| 464 | <td class="ln">6</td> |
| 465 | <td><pre> }</pre></td> |
| 466 | </tr> |
| 467 | <tr class="line odd"> |
| 468 | <td class="ln">7</td> |
| 469 | <td><pre> Role1 boss, worker = <span class="comment">// initialization omitted</span></pre></td> |
| 470 | </tr> |
| 471 | <tr class="line even"> |
| 472 | <td class="ln">8</td> |
| 473 | <td><pre> <b>public</b> <b>void</b> transaction () {</pre></td> |
| 474 | </tr> |
| 475 | <tr class="line odd"> |
| 476 | <td class="ln">9</td> |
| 477 | <td><pre> boss.payEuro(worker.earnEuro());</pre></td> |
| 478 | </tr> |
| 479 | <tr class="line even"> |
| 480 | <td class="ln">10</td> |
| 481 | <td><pre> boss.idle(123);</pre></td> |
| 482 | </tr> |
| 483 | <tr class="line odd"> |
| 484 | <td class="ln">11</td> |
| 485 | <td><pre> }</pre></td> |
| 486 | </tr> |
| 487 | <tr class="line even"> |
| 488 | <td class="ln">12</td> |
| 489 | <td><pre>}</pre></td> |
| 490 | </tr> |
| 491 | <tr class="line odd"> |
| 492 | <td class="ln">13</td> |
| 493 | <td><pre><b>public</b> <b>class</b> Staff { <span class="comment">// a base class </span></pre></td> |
| 494 | </tr> |
| 495 | <tr class="line even"> |
| 496 | <td class="ln">14</td> |
| 497 | <td><pre> <b>public</b> <b>void</b> payDM (float dm) { … };</pre></td> |
| 498 | </tr> |
| 499 | <tr class="line odd"> |
| 500 | <td class="ln">15</td> |
| 501 | <td><pre> <b>public</b> float earnDM () { … };</pre></td> |
| 502 | </tr> |
| 503 | <tr class="line even"> |
| 504 | <td class="ln">16</td> |
| 505 | <td><pre> <b>public</b> <b>int</b> doze() { … };</pre></td> |
| 506 | </tr> |
| 507 | <tr class="line odd"> |
| 508 | <td class="ln">17</td> |
| 509 | <td><pre> <span class="comment">// other methods omitted</span></pre></td> |
| 510 | </tr> |
| 511 | <tr class="line even"> |
| 512 | <td class="ln">18</td> |
| 513 | <td><pre>}</pre></td> |
| 514 | </tr> |
| 515 | <tr class="line odd"> |
| 516 | <td class="ln">19</td> |
| 517 | <td><pre><b>public</b> <b>team</b> <b>class</b> MySubTeam <b>extends</b> MyTeamA {</pre></td> |
| 518 | </tr> |
| 519 | <tr class="line even"> |
| 520 | <td class="ln">20</td> |
| 521 | <td><pre> <b>public</b> <b>class</b> Role1 <b>playedBy</b> Staff {</pre></td> |
| 522 | </tr> |
| 523 | <tr class="line odd"> |
| 524 | <td class="ln">21</td> |
| 525 | <td><pre> <b>void</b> payEuro(float euro) <b>-></b> <b>void</b> payDM(float dm) <b>with</b> {</pre></td> |
| 526 | </tr> |
| 527 | <tr class="line even"> |
| 528 | <td class="ln">22</td> |
| 529 | <td><pre> euro * 1.95583f <b>-></b> dm</pre></td> |
| 530 | </tr> |
| 531 | <tr class="line odd"> |
| 532 | <td class="ln">23</td> |
| 533 | <td><pre> }</pre></td> |
| 534 | </tr> |
| 535 | <tr class="line even"> |
| 536 | <td class="ln">24</td> |
| 537 | <td><pre> float earnEuro() <b>-></b> float earnDM () <b>with</b> {</pre></td> |
| 538 | </tr> |
| 539 | <tr class="line odd"> |
| 540 | <td class="ln">25</td> |
| 541 | <td><pre> result <b><-</b> result / 1.95583f</pre></td> |
| 542 | </tr> |
| 543 | <tr class="line even"> |
| 544 | <td class="ln">26</td> |
| 545 | <td><pre> }</pre></td> |
| 546 | </tr> |
| 547 | <tr class="line odd"> |
| 548 | <td class="ln">27</td> |
| 549 | <td><pre> idle <b>=></b> doze; <span class="comment">// override existing implementation of idle()</span></pre></td> |
| 550 | </tr> |
| 551 | <tr class="line even"> |
| 552 | <td class="ln">28</td> |
| 553 | <td><pre> }</pre></td> |
| 554 | </tr> |
| 555 | <tr class="line odd"> |
| 556 | <td class="ln">29</td> |
| 557 | <td><pre> <b>void</b> doit() {</pre></td> |
| 558 | </tr> |
| 559 | <tr class="line even"> |
| 560 | <td class="ln">30</td> |
| 561 | <td><pre> transaction();</pre></td> |
| 562 | </tr> |
| 563 | <tr class="line odd"> |
| 564 | <td class="ln">31</td> |
| 565 | <td><pre> }</pre></td> |
| 566 | </tr> |
| 567 | <tr class="line even"> |
| 568 | <td class="ln">32</td> |
| 569 | <td><pre>}</pre></td> |
| 570 | </tr> |
| 571 | </table> |
| 572 | </div> |
| 573 | <div class="codecomment"> |
| 574 | <h5>Effects:</h5> |
| 575 | <ul> |
| 576 | <li>Class <code>MyTeamA</code> is declaratively complete and can be |
| 577 | type checked because it only uses methods that are visible or declared within this |
| 578 | context. <code>MyTeamA.Role1</code> can, however, not be instantiated, because it is |
| 579 | abstract. |
| 580 | </li> |
| 581 | <li>Line 30 has the normal effect of invoking <code>transaction</code>. |
| 582 | </li> |
| 583 | <li>When executing <code>transaction</code>, the call of <code>worker.earnEuro()</code> |
| 584 | is forwarded to the corresponding base object using method <code>earnDM()</code> |
| 585 | (binding declaration in line 24). |
| 586 | The result is converted by "<code>result / 1.95583f</code>" (line 25). |
| 587 | </li> |
| 588 | <li>Within the same execution of <code>transaction</code>, the call of |
| 589 | <code>boss.payEuro()</code> |
| 590 | is forwarded to the corresponding base object using method <code>payDM()</code> |
| 591 | (binding declaration in line 21). |
| 592 | The parameter <code>euro</code> is converted by "<code>euro * 1.95583f</code>" |
| 593 | (line 22). |
| 594 | </li> |
| 595 | <li>Method <code>idle</code> is forwarded to <code>doze</code> without any |
| 596 | parameter mapping. This requires <code>doze</code> to have a signature |
| 597 | that is conformable to the signature of <code>idle</code>. |
| 598 | In this case a role parameter and a base result are ignored.<br /> |
| 599 | Using the <code>=></code> operator, this binding overrides the existing |
| 600 | implementation of <code>idle</code>. |
| 601 | </li> |
| 602 | </ul> |
| 603 | </div> |
| 604 | </div> |
| 605 | <div class="sect depth2" id="s3.3"> |
Stephan Herrmann | 109893e | 2010-05-18 22:02:53 +0000 | [diff] [blame] | 606 | <h2 class="sect">§3.3 Lifting and lowering<a class="img" href="s3.3.html" |
| 607 | title="PermaLink to §3.3 Lifting and lowering"><img style="vertical-align:text-top;margin-left:5px;" src="../images/permalink.png" |
| 608 | alt="" /></a><span class="toplink"><a href="#top">↑ §3</a></span></h2> |
Stephan Herrmann | 0166441 | 2010-04-01 20:28:43 +0000 | [diff] [blame] | 609 | <p> (For basic definitions see <a href="s2.2.html" title="§2.2 Lowering" class="sect">§2.2</a> and <a href="s2.3.html" title="§2.3 Lifting" class="sect">§2.3</a>) |
| 610 | |
| 611 | </p> |
| 612 | <div class="subsect depth3" id="s3.3.a"> |
Stephan Herrmann | 109893e | 2010-05-18 22:02:53 +0000 | [diff] [blame] | 613 | <h4 class="subsect">(a) <span class="title">Call target translation</span><a class="img" href="s3.3.a.html" |
| 614 | title="PermaLink to (a) Call target translation"><img style="vertical-align:text-top;margin-left:5px;" src="../images/permalink.png" |
| 615 | alt="" /></a></h4> |
Stephan Herrmann | 0166441 | 2010-04-01 20:28:43 +0000 | [diff] [blame] | 616 | <p>Invoking a base method due to a callout binding first |
| 617 | <strong>lowers</strong> the role object in order to obtain the effective call target. |
| 618 | |
| 619 | </p> |
| 620 | </div> |
| 621 | <div class="subsect depth3" id="s3.3.b"> |
Stephan Herrmann | 109893e | 2010-05-18 22:02:53 +0000 | [diff] [blame] | 622 | <h4 class="subsect">(b) <span class="title">Parameter translation</span><a class="img" href="s3.3.b.html" |
| 623 | title="PermaLink to (b) Parameter translation"><img style="vertical-align:text-top;margin-left:5px;" src="../images/permalink.png" |
| 624 | alt="" /></a></h4> |
Stephan Herrmann | 0166441 | 2010-04-01 20:28:43 +0000 | [diff] [blame] | 625 | <p>Passing a role object as parameter to a callout method implicitly <strong>lowers</strong> |
| 626 | this parameter, if the base method declares a corresponding base type parameter. |
| 627 | <br /> |
| 628 | Lifting of callout parameters is not possible. |
| 629 | |
| 630 | </p> |
| 631 | </div> |
| 632 | <div class="subsect depth3" id="s3.3.c"> |
Stephan Herrmann | 109893e | 2010-05-18 22:02:53 +0000 | [diff] [blame] | 633 | <h4 class="subsect">(c) <span class="title">Result translation</span><a class="img" href="s3.3.c.html" |
| 634 | title="PermaLink to (c) Result translation"><img style="vertical-align:text-top;margin-left:5px;" src="../images/permalink.png" |
| 635 | alt="" /></a></h4> |
Stephan Herrmann | 0166441 | 2010-04-01 20:28:43 +0000 | [diff] [blame] | 636 | <p> When returning a base object from a callout method where the role |
| 637 | method declares the result to be of a role class, |
| 638 | this object is implicitly <strong>lifted</strong> to the appropriate role. |
| 639 | <br /> |
| 640 | Lowering the result of a callout binding is not possible. |
| 641 | |
| 642 | </p> |
| 643 | </div> |
| 644 | <div class="subsect depth3" id="s3.3.d"> |
Stephan Herrmann | 109893e | 2010-05-18 22:02:53 +0000 | [diff] [blame] | 645 | <h4 class="subsect">(d) <span class="title">Typing rules</span><a class="img" href="s3.3.d.html" title="PermaLink to (d) Typing rules"><img style="vertical-align:text-top;margin-left:5px;" src="../images/permalink.png" |
| 646 | alt="" /></a></h4> |
Stephan Herrmann | 0166441 | 2010-04-01 20:28:43 +0000 | [diff] [blame] | 647 | <p>A parameter mapping (implicit by parameter position or explicit |
| 648 | by a <code>with</code> clause) is <strong>well typed</strong> if |
| 649 | the left hand side conforms to the right hand side, either by |
| 650 | |
| 651 | </p> |
| 652 | <ul> |
| 653 | <li>type equality</li> |
| 654 | <li>implicit primitive type conversion</li> |
| 655 | <li>subtype polymorphism</li> |
| 656 | <li>translation polymorphism, here: <em>lowering</em>, |
| 657 | </li> |
| 658 | <li><em>or</em> by a combination of the above. |
| 659 | </li> |
| 660 | </ul> |
| 661 | <p>A result mapping (implicit or explicit by a <code>with</code> clause) |
| 662 | is well typed, if the value at the right hand side conforms to the |
| 663 | left hand side according to the rules given above, except that |
| 664 | translation polymorphism here applies <em>lifting</em> instead of |
| 665 | lowering. |
| 666 | |
| 667 | </p> |
| 668 | </div> |
| 669 | <div class="subsect depth3" id="s3.3.e"> |
Stephan Herrmann | 109893e | 2010-05-18 22:02:53 +0000 | [diff] [blame] | 670 | <h4 class="subsect">(e) <span class="title">Role arrays</span><a class="img" href="s3.3.e.html" title="PermaLink to (e) Role arrays"><img style="vertical-align:text-top;margin-left:5px;" src="../images/permalink.png" |
| 671 | alt="" /></a></h4> |
Stephan Herrmann | 0166441 | 2010-04-01 20:28:43 +0000 | [diff] [blame] | 672 | <p>For arrays of roles as parameters <a href="s2.2.e.html" title="§2.2.(e) Lowering of arrays" |
| 673 | class="sect">§2.2.(e)</a> applies accordingly. |
| 674 | For arrays as a return value <a href="s2.3.d.html" title="§2.3.(d) Lifting of arrays" |
| 675 | class="sect">§2.3.(d)</a> applies. |
| 676 | |
| 677 | </p> |
| 678 | </div> |
| 679 | </div> |
| 680 | <div class="sect depth2" id="s3.4"> |
Stephan Herrmann | 109893e | 2010-05-18 22:02:53 +0000 | [diff] [blame] | 681 | <h2 class="sect">§3.4 Overriding access restrictions<a class="img" href="s3.4.html" |
| 682 | title="PermaLink to §3.4 Overriding access restrictions"><img style="vertical-align:text-top;margin-left:5px;" src="../images/permalink.png" |
| 683 | alt="" /></a><span class="toplink"><a href="#top">↑ §3</a></span></h2> |
Stephan Herrmann | 0166441 | 2010-04-01 20:28:43 +0000 | [diff] [blame] | 684 | <p>In contrast to normal access restrictions, method bindings |
| 685 | may refer to hidden base methods. |
| 686 | This concept is the inverse of encapsulation, hence it is called |
| 687 | <strong>decapsulation</strong>. |
| 688 | <br /> |
| 689 | Decapsulation may occur in these positions: |
| 690 | |
| 691 | </p> |
| 692 | <ul> |
| 693 | <li><code>playedBy</code> declaration (see <a href="s2.1.2.c.html" title="§2.1.2.(c) Base class decapsulation" |
| 694 | class="sect">§2.1.2.(c)</a>) |
| 695 | </li> |
| 696 | <li>base constructor call (see <a href="s2.4.2.b.html" title="§2.4.2.(b) Bound roles" class="sect">§2.4.2.(b)</a>). |
| 697 | </li> |
| 698 | <li>callout bindings (see next)</li> |
| 699 | <li>callout to field (see <a href="#s3.5.e" title="§3.5.(e) Access control" class="sect">§3.5.(e)</a>) |
| 700 | </li> |
| 701 | <li>base call within a callin method (see <a href="s4.6.html" title="§4.6 Overriding access restrictions" |
| 702 | class="sect">§4.6</a>) |
| 703 | </li> |
| 704 | </ul> |
| 705 | <div class="subsect depth3" id="s3.4.a"> |
Stephan Herrmann | 109893e | 2010-05-18 22:02:53 +0000 | [diff] [blame] | 706 | <h4 class="subsect">(a) <span class="title">Callout to inaccessible base method</span><a class="img" href="s3.4.a.html" |
| 707 | title="PermaLink to (a) Callout to inaccessible base method"><img style="vertical-align:text-top;margin-left:5px;" src="../images/permalink.png" |
| 708 | alt="" /></a></h4> |
Stephan Herrmann | 0166441 | 2010-04-01 20:28:43 +0000 | [diff] [blame] | 709 | <p>By means of <strong>callout</strong> bindings it is possible to access |
| 710 | methods of a base class regardless of their access modifiers. |
| 711 | Method bindings are the only place in a program which may mention otherwise |
| 712 | inaccessible methods. |
| 713 | Access to the callout method at the role side is controlled by regular mechanisms, |
| 714 | based on the declaration of the role method. |
| 715 | |
| 716 | </p> |
| 717 | </div> |
| 718 | <div class="subsect depth3" id="s3.4.b"> |
Stephan Herrmann | 109893e | 2010-05-18 22:02:53 +0000 | [diff] [blame] | 719 | <h4 class="subsect">(b) <span class="title">Sealing against decapsulation</span><a class="img" href="s3.4.b.html" |
| 720 | title="PermaLink to (b) Sealing against decapsulation"><img style="vertical-align:text-top;margin-left:5px;" src="../images/permalink.png" |
| 721 | alt="" /></a></h4> |
Stephan Herrmann | 0166441 | 2010-04-01 20:28:43 +0000 | [diff] [blame] | 722 | <p>A base package may be "sealed" which re-establishes the standard Java visibility rules. |
| 723 | <br /> |
| 724 | Sealing is achieved by the corresponding capability of Jar files. |
| 725 | |
| 726 | </p> |
| 727 | </div> |
| 728 | <div class="subsect depth3" id="s3.4.c"> |
Stephan Herrmann | 109893e | 2010-05-18 22:02:53 +0000 | [diff] [blame] | 729 | <h4 class="subsect">(c) <span class="title">Warning levels</span><a class="img" href="s3.4.c.html" title="PermaLink to (c) Warning levels"><img style="vertical-align:text-top;margin-left:5px;" src="../images/permalink.png" |
| 730 | alt="" /></a></h4> |
Stephan Herrmann | 0166441 | 2010-04-01 20:28:43 +0000 | [diff] [blame] | 731 | <p>A compiler should signal any occurrence of decapsulation. |
| 732 | If a compiler supports to configure warnings this may be used to let the user choose to |
| 733 | (a) ignore base class decapsulation, (b) treat it as a warning or even |
| 734 | (c) treat it as an error (cf. <a href="s2.1.2.c.html" title="§2.1.2.(c) Base class decapsulation" |
| 735 | class="sect">§2.1.2.(c)</a>). |
| 736 | <br /> |
| 737 | Optionally, a batch compiler may support three levels of verbosity with respect to decapsulation: |
| 738 | |
| 739 | </p> |
| 740 | <table border="1"> |
| 741 | <tr> |
| 742 | <td rowspan="1" colspan="1"><tt>-nodecapsulation</tt></td> |
| 743 | <td rowspan="1" colspan="1">No warnings.</td> |
| 744 | </tr> |
| 745 | <tr> |
| 746 | <td rowspan="1" colspan="1"><em>default</em></td> |
| 747 | <td rowspan="1" colspan="1">Warn only if/that access restrictions are overridden.</td> |
| 748 | </tr> |
| 749 | <tr> |
| 750 | <td rowspan="1" colspan="1"><tt>-decapsulation</tt></td> |
| 751 | <td rowspan="1" colspan="1">Detailed messages containing the binding and the hidden base method.</td> |
| 752 | </tr> |
| 753 | </table> |
| 754 | </div> |
| 755 | <div class="subsect depth3" id="s3.4.d"> |
Stephan Herrmann | 109893e | 2010-05-18 22:02:53 +0000 | [diff] [blame] | 756 | <h4 class="subsect">(d) <span class="title">Private methods from super classes</span><a class="img" href="s3.4.d.html" |
| 757 | title="PermaLink to (d) Private methods from super classes"><img style="vertical-align:text-top;margin-left:5px;" src="../images/permalink.png" |
| 758 | alt="" /></a></h4> |
Stephan Herrmann | 0166441 | 2010-04-01 20:28:43 +0000 | [diff] [blame] | 759 | <p>If a callout binding shall bind to a private base method, that method |
| 760 | must be defined in the exact base class to which the current role |
| 761 | class is bound using <code>playedBy</code>. I.e., for private methods |
| 762 | <a href="#s3.1.d" |
| 763 | title="§3.1.(d) Inheritance of role method declarations" |
| 764 | class="sect">§3.1.(d)</a> does not hold. |
| 765 | <br /> |
| 766 | The same holds for private base fields (see below). |
| 767 | <br /> |
| 768 | If a private base feature must indeed be callout-bound, a role class |
| 769 | must be defined that is played by the exact base class defining the |
| 770 | private feature. Another role bound to a sub-base-class can then |
| 771 | be defined as a sub class of the first role. It will inherit the |
| 772 | callout binding and through this it can access the desired feature. |
| 773 | |
| 774 | </p> |
| 775 | <div class="listing example frame"> |
| 776 | <table class="listing"> |
| 777 | <tr class="line odd"> |
| 778 | <td class="ln">1</td> |
| 779 | <td><pre><b>public</b> <b>class</b> SuperBase {</pre></td> |
| 780 | </tr> |
| 781 | <tr class="line even"> |
| 782 | <td class="ln">2</td> |
| 783 | <td><pre> <em><b>private</b> <b>int</b> secret;</em></pre></td> |
| 784 | </tr> |
| 785 | <tr class="line odd"> |
| 786 | <td class="ln">3</td> |
| 787 | <td><pre>}</pre></td> |
| 788 | </tr> |
| 789 | <tr class="line even"> |
| 790 | <td class="ln">4</td> |
| 791 | <td><pre><b>public</b> <b>class</b> SubBase <b>extends</b> SuperBase { <span class="comment">/* details omitted */</span> }</pre></td> |
| 792 | </tr> |
| 793 | <tr class="line odd"> |
| 794 | <td class="ln">5</td> |
| 795 | <td><pre><b>public</b> <b>team</b> <b>class</b> MyTeam {</pre></td> |
| 796 | </tr> |
| 797 | <tr class="line even"> |
| 798 | <td class="ln">6</td> |
| 799 | <td><pre> <b>protected</b> <b>class</b> SuperRole <b>playedBy</b> SuperBase {</pre></td> |
| 800 | </tr> |
| 801 | <tr class="line odd"> |
| 802 | <td class="ln">7</td> |
| 803 | <td><pre> <b>int</b> steal() <b>-></b> <b>get</b> <b>int</b> <em>secret</em>; <span class="comment">// <span class="green"><strong>OK</strong></span></span></pre></td> |
| 804 | </tr> |
| 805 | <tr class="line even"> |
| 806 | <td class="ln">8</td> |
| 807 | <td><pre> }</pre></td> |
| 808 | </tr> |
| 809 | <tr class="line odd"> |
| 810 | <td class="ln">9</td> |
| 811 | <td><pre> <b>protected</b> <b>class</b> SubRole <b>extends</b> SuperRole <b>playedBy</b> SubBase {</pre></td> |
| 812 | </tr> |
| 813 | <tr class="line even"> |
| 814 | <td class="ln">10</td> |
| 815 | <td><pre> <b>int</b> steal() <b>-></b> <b>get</b> <b>int</b> <em>secret</em>; <span class="comment">// <span class="error"><strong>illegal!</strong></span></span></pre></td> |
| 816 | </tr> |
| 817 | <tr class="line odd"> |
| 818 | <td class="ln">11</td> |
| 819 | <td><pre> }</pre></td> |
| 820 | </tr> |
| 821 | <tr class="line even"> |
| 822 | <td class="ln">12</td> |
| 823 | <td><pre>}</pre></td> |
| 824 | </tr> |
| 825 | </table> |
| 826 | </div> |
| 827 | </div> |
| 828 | </div> |
| 829 | <div class="sect depth2" id="s3.5"> |
Stephan Herrmann | 109893e | 2010-05-18 22:02:53 +0000 | [diff] [blame] | 830 | <h2 class="sect">§3.5 Callout to field<a class="img" href="s3.5.html" |
| 831 | title="PermaLink to §3.5 Callout to field"><img style="vertical-align:text-top;margin-left:5px;" src="../images/permalink.png" |
| 832 | alt="" /></a><span class="toplink"><a href="#top">↑ §3</a></span></h2> |
Stephan Herrmann | 0166441 | 2010-04-01 20:28:43 +0000 | [diff] [blame] | 833 | <p>Also fields of a base class can be made accessible using a callout binding. |
| 834 | |
| 835 | </p> |
| 836 | <div class="subsect depth3" id="s3.5.a"> |
Stephan Herrmann | 109893e | 2010-05-18 22:02:53 +0000 | [diff] [blame] | 837 | <h4 class="subsect">(a) <span class="title">Syntax</span><a class="img" href="s3.5.a.html" title="PermaLink to (a) Syntax"><img style="vertical-align:text-top;margin-left:5px;" src="../images/permalink.png" |
| 838 | alt="" /></a></h4> |
Stephan Herrmann | 0166441 | 2010-04-01 20:28:43 +0000 | [diff] [blame] | 839 | <p>Using one of the callout modifiers <code>get</code> or <code>set</code> a role method |
| 840 | can be bound to a field of the role's base class: |
| 841 | |
| 842 | </p> |
| 843 | <div class="listing example frame"> |
| 844 | <table class="listing"> |
| 845 | <tr class="line odd"> |
| 846 | <td class="ln">1</td> |
| 847 | <td><pre>getValue <b>-></b> <em>get</em> value;</pre></td> |
| 848 | </tr> |
| 849 | <tr class="line even"> |
| 850 | <td class="ln">2</td> |
| 851 | <td><pre>setValue <b>-></b> <em>set</em> value;</pre></td> |
| 852 | </tr> |
| 853 | <tr class="line odd"> |
| 854 | <td class="ln">3</td> |
| 855 | <td><pre><b>int</b> getValue() <b>-></b> <em>get</em> <b>int</b> value;</pre></td> |
| 856 | </tr> |
| 857 | </table> |
| 858 | </div> |
| 859 | <p>where <code>getValue</code>, <code>setValue</code> are abstract role methods of |
| 860 | appropriate signatures and <code>value</code> is a field of the bound base class. |
| 861 | <br /> |
| 862 | A longer syntax is available, too (see line 3 above), which uses complete signatures. |
| 863 | For the left hand side <a href="#s3.1.c" title="§3.1.(c) Kinds of method designators" |
| 864 | class="sect">§3.1.(c)</a> applies, for the |
| 865 | right hand side, this longer version prepends the field type to the field name. |
| 866 | |
| 867 | </p> |
| 868 | </div> |
| 869 | <div class="subsect depth3" id="s3.5.b"> |
Stephan Herrmann | 109893e | 2010-05-18 22:02:53 +0000 | [diff] [blame] | 870 | <h4 class="subsect">(b) <span class="title">Compatibility</span><a class="img" href="s3.5.b.html" title="PermaLink to (b) Compatibility"><img style="vertical-align:text-top;margin-left:5px;" src="../images/permalink.png" |
| 871 | alt="" /></a></h4> |
Stephan Herrmann | 0166441 | 2010-04-01 20:28:43 +0000 | [diff] [blame] | 872 | <p>A role method bound with the modifier <code>get</code> |
| 873 | should have no arguments |
| 874 | (it <em>may</em> have arbitrary arguments, which are silently ignored) |
| 875 | and should have a return type to which the base field is compatible. |
| 876 | A role method returning void will ignore the given value and thus has |
| 877 | no effect at all, which will be signaled by a compiler warning. |
| 878 | <br /> |
| 879 | A role method bound with the modifier <code>set</code> must have a first argument |
| 880 | that is compatible to the base field's type (additional arguments - if present - |
| 881 | are silently ignored) and must not declare a return type. |
| 882 | |
| 883 | </p> |
| 884 | </div> |
| 885 | <div class="subsect depth3" id="s3.5.c"> |
Stephan Herrmann | 109893e | 2010-05-18 22:02:53 +0000 | [diff] [blame] | 886 | <h4 class="subsect">(c) <span class="title">Value mapping</span><a class="img" href="s3.5.c.html" title="PermaLink to (c) Value mapping"><img style="vertical-align:text-top;margin-left:5px;" src="../images/permalink.png" |
| 887 | alt="" /></a></h4> |
Stephan Herrmann | 0166441 | 2010-04-01 20:28:43 +0000 | [diff] [blame] | 888 | <p>Values can be mapped similar to parameter mappings in pure method bindings |
| 889 | (<a href="#s3.2" title="§3.2 Callout parameter mapping" class="sect">§3.2</a>). Such mappings can be used to establish compatibility as required above. |
| 890 | <br /> |
| 891 | In both <code>get</code> and <code>set</code> bindings, the base side value is denoted |
| 892 | by the field's name (lines 2 and 4 below). |
| 893 | |
| 894 | </p> |
| 895 | <div class="listing example frame"> |
| 896 | <table class="listing"> |
| 897 | <tr class="line odd"> |
| 898 | <td class="ln">1</td> |
| 899 | <td><pre>Integer getValue() -> <em>get</em> <b>int</b> val</pre></td> |
| 900 | </tr> |
| 901 | <tr class="line even"> |
| 902 | <td class="ln">2</td> |
| 903 | <td><pre> <em>with</em> { result <- <b>new</b> Integer(val) }</pre></td> |
| 904 | </tr> |
| 905 | <tr class="line odd"> |
| 906 | <td class="ln">3</td> |
| 907 | <td><pre><b>void</b> setValue(Integer i) -> <em>set</em> <b>int</b> val</pre></td> |
| 908 | </tr> |
| 909 | <tr class="line even"> |
| 910 | <td class="ln">4</td> |
| 911 | <td><pre> <em>with</em> { i.intValue() -> val }</pre></td> |
| 912 | </tr> |
| 913 | </table> |
| 914 | </div> |
| 915 | </div> |
| 916 | <div class="subsect depth3" id="s3.5.d"> |
Stephan Herrmann | 109893e | 2010-05-18 22:02:53 +0000 | [diff] [blame] | 917 | <h4 class="subsect">(d) <span class="title">Effect</span><a class="img" href="s3.5.d.html" title="PermaLink to (d) Effect"><img style="vertical-align:text-top;margin-left:5px;" src="../images/permalink.png" |
| 918 | alt="" /></a></h4> |
Stephan Herrmann | 0166441 | 2010-04-01 20:28:43 +0000 | [diff] [blame] | 919 | <p>Callout-binding a role method to a base field generates an implementation for |
| 920 | this role method, by which it acts as a getter or setter for the given |
| 921 | field of the associated base object. |
| 922 | |
| 923 | </p> |
| 924 | </div> |
| 925 | <div class="subsect depth3" id="s3.5.e"> |
Stephan Herrmann | 109893e | 2010-05-18 22:02:53 +0000 | [diff] [blame] | 926 | <h4 class="subsect">(e) <span class="title">Access control</span><a class="img" href="s3.5.e.html" title="PermaLink to (e) Access control"><img style="vertical-align:text-top;margin-left:5px;" src="../images/permalink.png" |
| 927 | alt="" /></a></h4> |
Stephan Herrmann | 0166441 | 2010-04-01 20:28:43 +0000 | [diff] [blame] | 928 | <p>For accessing an otherwise invisible field, the rules for |
| 929 | <a href="#s3.4" title="§3.4 Overriding access restrictions" |
| 930 | class="sect">decapsulation (§3.4)</a> apply accordingly. |
| 931 | <br /> |
| 932 | Recall, that according to |
| 933 | <a href="http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html#40898" |
| 934 | class="ext">JLS §8.3</a> |
| 935 | fields may be hidden in sub-classes of a given base class. Therefore, it is relevant to know that a callout to |
| 936 | a field will always access the field that is visible in the exact base |
| 937 | class of the role class defining the callout. This is especially relevant |
| 938 | for accessing private fields. |
| 939 | |
| 940 | </p> |
| 941 | </div> |
| 942 | <div class="subsect depth3" id="s3.5.f"> |
Stephan Herrmann | 109893e | 2010-05-18 22:02:53 +0000 | [diff] [blame] | 943 | <h4 class="subsect">(f) <span class="title">Shorthand definition</span><a class="img" href="s3.5.f.html" |
| 944 | title="PermaLink to (f) Shorthand definition"><img style="vertical-align:text-top;margin-left:5px;" src="../images/permalink.png" |
| 945 | alt="" /></a></h4> |
Stephan Herrmann | 0166441 | 2010-04-01 20:28:43 +0000 | [diff] [blame] | 946 | <p>Just as in <a href="#s3.1.i" title="§3.1.(i) Shorthand definition" class="sect">§3.1.(i)</a> a shorthand definition allows to introduce |
| 947 | a callout field access method without prior abstract declaration. |
| 948 | This requires the callout field binding to specify types as in line 3 of <a href="#s3.5.a" title="§3.5.(a) Syntax" class="sect">§3.5.(a)</a> above. |
| 949 | The generated access method is static iff the bound base field is static. |
| 950 | |
| 951 | </p> |
| 952 | <p> |
| 953 | A shorthand callout to field may optionally declare a <strong>visibility modifier</strong>, |
| 954 | otherwise the generated method inherits the visibility modifier of the bound base field. |
| 955 | No further modifiers are set. |
| 956 | If a callout to field overrides an inherited method or callout, |
| 957 | it must not reduce the visibility of the inherited method/callout. |
| 958 | |
| 959 | </p> |
| 960 | </div> |
| 961 | <div class="subsect depth3" id="s3.5.g"> |
Stephan Herrmann | 109893e | 2010-05-18 22:02:53 +0000 | [diff] [blame] | 962 | <h4 class="subsect">(g) <span class="title">Callout override</span><a class="img" href="s3.5.g.html" title="PermaLink to (g) Callout override"><img style="vertical-align:text-top;margin-left:5px;" src="../images/permalink.png" |
| 963 | alt="" /></a></h4> |
Stephan Herrmann | 0166441 | 2010-04-01 20:28:43 +0000 | [diff] [blame] | 964 | <p>Similar to method callouts a callout to field may override an existing role |
| 965 | method if and only if the token <code>=></code> is used instead of <code>-></code> |
| 966 | (see <a href="#s3.1.e" title="§3.1.(e) Callout override" class="sect">§3.1.(e)</a> and <a href="#s3.1.f" title="§3.1.(f) Inheritance of callout bindings" |
| 967 | class="sect">§3.1.(f)</a>). |
| 968 | |
| 969 | </p> |
| 970 | </div> |
| 971 | <div class="subsect depth3" id="s3.5.h"> |
Stephan Herrmann | 109893e | 2010-05-18 22:02:53 +0000 | [diff] [blame] | 972 | <h4 class="subsect">(h) <span class="title">Inferred callout</span><a class="img" href="s3.5.h.html" title="PermaLink to (h) Inferred callout"><img style="vertical-align:text-top;margin-left:5px;" src="../images/permalink.png" |
| 973 | alt="" /></a></h4> |
Stephan Herrmann | 0166441 | 2010-04-01 20:28:43 +0000 | [diff] [blame] | 974 | <p> If a statement or expression within the body of a bound role class |
| 975 | uses a simple name or a name qualified by <code>this</code> |
| 976 | which can not be resolved using normal rules, |
| 977 | the compiler may infer to use a callout to field instead, |
| 978 | given that a field of the required name can be found in the role's declared baseclass. |
| 979 | |
| 980 | </p> |
| 981 | <p> If a callout to field has explicitly been declared it is used for the otherwise |
| 982 | unresolved name, if and only if: |
| 983 | |
| 984 | </p> |
| 985 | <ul> |
| 986 | <li>the callout declares a role method name the is constructed from the token "set" for |
| 987 | a setter or "get" for a getter plus the field name with capital first letter, |
| 988 | </li> |
| 989 | <li>the base field referenced by the callout has exactly the required name, and</li> |
| 990 | <li>the callout kind (set/get) matches the application of the unresolved name as either |
| 991 | the left-hand side of an assignment (set) or as an expression (get). |
| 992 | </li> |
| 993 | </ul> |
| 994 | <p> |
| 995 | If a matching callout to field is not found, the compiler generates one automatically, |
| 996 | which has <code>private</code> visibility. |
| 997 | |
| 998 | </p> |
| 999 | <p> |
| 1000 | If a callout to field has been inferred it is an error to directly invoke the |
| 1001 | implicitly generated callout accessor that is formed according to the above rules. |
| 1002 | |
| 1003 | </p> |
| 1004 | <p> |
| 1005 | Per default inferred callout bindings are disabled, i.e., a compiler |
| 1006 | must report these as an error. However, a compiler should allow to |
| 1007 | configure reporting to produce a warning only (which can be suppressed |
| 1008 | using a <code>@SuppressWarnings("inferredcallout")</code> annotation), |
| 1009 | or to completely ignore the diagnostic. See also <a href="#s3.1.j" title="§3.1.(j) Inferred callout" class="sect">§3.1.(j)</a>. |
| 1010 | |
| 1011 | </p> |
| 1012 | </div> |
| 1013 | </div> |
| 1014 | </div> |
| 1015 | <table class="nav"> |
| 1016 | <tr> |
| 1017 | <td class="back"><a href="s2.html" rel="prev"><< §2 Role Binding</a></td> |
| 1018 | <td class="top"><a href="index.html" rel="contents">↑ Table of Contents ↑</a></td> |
| 1019 | <td class="next"><a href="s4.html" rel="next">§4 Callin Binding >></a></td> |
| 1020 | </tr> |
| 1021 | </table> |
| 1022 | <div class="breadcrumb"></div> |
| 1023 | </div> |
| 1024 | <div id="footer"> |
Stephan Herrmann | 109893e | 2010-05-18 22:02:53 +0000 | [diff] [blame] | 1025 | <hr /><a class="w3c img" href="http://jigsaw.w3.org/css-validator/check/referer" |
| 1026 | 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" |
| 1027 | width="88" /></a><address>© Stephan Herrmann, Christine Hundt, Marco Mosconi</address> |
Stephan Herrmann | 8f0fa31 | 2010-06-08 18:24:55 +0000 | [diff] [blame^] | 1028 | OT/J version 1.3 — last modified: 2010-06-08 |
Stephan Herrmann | 0166441 | 2010-04-01 20:28:43 +0000 | [diff] [blame] | 1029 | </div> |
| 1030 | </body> |
| 1031 | </html> |