Featherweight Wrap Java: wrapping objects and methods
Lorenzo Bettini, Sara Capecchi, Elena Giachino
Journal of Object Technology
We present a language extension, which integrates in a Java like language a mechanism for dynamically extending object behaviors without changing their type. Our approach consists in moving the addition of new features from class (static) level to object (dynamic) level: the basic features of entities (representing their structure) are separated from the additional ones (wrapper classes whose instances represent run-time added behaviors). At run-time, these entities can be dynamically composed
... y instantiating wrapper objects which are attached to basic entities. Wrappers permit specializing (wrapping) methods at run-time: the method of the wrapper will be automatically executed after the method of the wrapped objects, using a delegation mechanism. Furthermore, wrapped methods can return values and values returned by the wrapped methods are transparently made available in the wrapper methods. We formalize our extension by adding the new constructs to Featherweight Java and we prove that the core language so extended (Featherweight Wrap Java) is type safe. 6 JOURNAL OF OBJECT TECHNOLOGY VOL 7, NO. 2 Proof. Straightforward induction on the derivation of Γ e : T. Lemma 3.2 (mType) If mType(m, T 1 ) = T → T then mType(m, T 2 ) = T → T for all T 2 <: T 1 . Proof. Straightforward induction on the derivation of T 2 <: T 1 . Lemma 3.3 (Substitution) If Γ, x : T e : T and Γ ι : T where T <: T, then Γ e[x ← ι] : T for some T <: T. Proof. By induction on the derivation of Γ, x : T e : T. The interesting case is method invocation (the other cases are straightforward). Case e.m(e). Since Γ, x : T e.m(e) : T for some T, we have that Γ, x : T e : T 0 mType(T 0 , m) = T → T Γ, x : T e : T 0 T 0 <: T By induction hypothesis on e and e we have that Γ e[x ← ι] : T 0 for some T 0 <: T 0 , and Γ e[x ← ι] : T 0 for some T 0 <: T 0 . From T 0 <: T 0 and Lemma 3.2 we have that mType(T 0 , m) = T → T, and from transitivity of subtyping T 0 <: T . Applying rule (T-INVK) we get that Γ (e.m(e))[x ← ι] : T which proves the result. Lemma 3.4 (Substitution this ⇐ I ι) If Γ, this : E e : T and Γ ι : D where wrap(D) = I and E <: I, then Γ, this : E e[this ⇐ I ι] : T.