05.08.2014 Views

here - Stefan-Marr.de

here - Stefan-Marr.de

here - Stefan-Marr.de

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

3.3. Common Problems for the Implementation of Concurrency Abstractions<br />

3.3.5. Reflection<br />

Reflection, i. e., metaprogramming is used to circumvent the restrictions of<br />

a particular language or to modify and extend its language semantics. When<br />

reflection is used, language guarantees are no longer enforced and <strong>de</strong>velopers<br />

have to take care not to violate in<strong>here</strong>nt assumptions ma<strong>de</strong> by the rest of the<br />

program. In sequential programs this is common practice and used wi<strong>de</strong>ly<br />

for a variety of use cases.<br />

Bypassing Language Restrictions Many common use cases bypass restrictions<br />

imposed by language semantics. Examples are the modification of supposedly<br />

immutable objects or bypassing restrictions to access protected fields.<br />

These capabilities are often <strong>de</strong>sirable to enable the implementation of frameworks<br />

that work on any given object by dynamically reflecting over it. Wi<strong>de</strong>ly<br />

used examples are unit testing frameworks reflecting over classes to execute<br />

tests, mockup generators to facilitate testing without the need to explicitly<br />

<strong>de</strong>fine test classes, object-relational mappers (ORM) to bridge between application<br />

and database for persistence, and other frameworks for general marshalling<br />

and serialization.<br />

Note that the characteristics of these use cases are very similar. The main reflective<br />

features used inclu<strong>de</strong> inspecting objects, invoking methods, or changing<br />

fields reflectively. While language restrictions such as modifiers for private<br />

and protected fields need to be circumvented, concurrency-related language<br />

properties should not be circumvented in these use cases. Instead, in most<br />

situations reflection should be able to respect these language guarantees.<br />

Concurrency properties need to be maintained during reflection. Since the<br />

<strong>de</strong>scribed use cases are ubiquitous, it is impractical to ban the use of reflection<br />

to guarantee the semantics of a particular concurrent programming concept.<br />

Instead, a VM needs to provi<strong>de</strong> a mechanism that makes it safe to use reflection<br />

for application purposes while maintaining the <strong>de</strong>sired part of the<br />

language guarantees, in this case the concurrency properties.<br />

Imagine an application implemented in an actor language that uses a reflective<br />

object-relational mapping (ORM) system such as Hibernate [Bauer<br />

and King, 2005], which by itself is not actor-aware. One actor tries to persist<br />

an object owned by another actor. Hibernate would use reflection to<br />

read the state. Since the Java reflection API 22 does not preserve concurrency-<br />

22 http://docs.oracle.com/javase/6/docs/api/java/lang/reflect/package-summary.h<br />

tml<br />

79

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

Saved successfully!

Ooh no, something went wrong!