10.12.2012 Views

The Java Language Specification, Third Edition

The Java Language Specification, Third Edition

The Java Language Specification, Third Edition

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

15.12.4 Runtime Evaluation of Method Invocation EXPRESSIONS<br />

478<br />

DISCUSSION<br />

As an example of such a situation, consider the declarations:<br />

class C { abstract T id(T x); }<br />

class D extends C { String id(String x) { return x; } }<br />

Now, given an invocation<br />

C c = new D();<br />

c.id(new Object()); // fails with a ClassCastException<br />

<strong>The</strong> erasure of the actual method being invoked, D.id(), differs in its signature from<br />

that of the compile-time method declaration, C.id(). <strong>The</strong> former takes an argument of<br />

type String while the latter takes an argument of type Object. <strong>The</strong> invocation fails with a<br />

ClassCastException before the body of the method is executed.<br />

Such situations can only arise if the program gives rise to an unchecked warning<br />

(§5.1.9).<br />

Implementations can enforce these semantics by creating bridge methods. In the<br />

above example, the following bridge method would be created in class D:<br />

Object id(Object x) { return id((String) x); }<br />

This is the method that would actually be invoked by the <strong>Java</strong> virtual machine in<br />

response to the call c.id(new Object()) shown above, and it will execute the cast and<br />

fail, as required.<br />

If the method m is a native method but the necessary native, implementationdependent<br />

binary code has not been loaded or otherwise cannot be dynamically<br />

linked, then an UnsatisfiedLinkError is thrown.<br />

If the method m is not synchronized, control is transferred to the body of the<br />

method m to be invoked.<br />

If the method m is synchronized, then an object must be locked<br />

before the transfer of control. No further progress can be made until<br />

the current thread can obtain the lock. If there is a target reference,<br />

then the target must be locked; otherwise the Class object for class<br />

S, the class of the method m, must be locked. Control is then transferred to the<br />

body of the method m to be invoked. <strong>The</strong> object is automatically unlocked when<br />

execution of the body of the method has completed, whether normally or abruptly.<br />

<strong>The</strong> locking and unlocking behavior is exactly as if the body of the method were<br />

embedded in a synchronized statement (§14.19).<br />

DRAFT

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!