[otj-users] Problem with inner class

Stephan Herrmann stephan at cs.tu-berlin.de
Thu Mar 22 19:21:11 CET 2007


Hello,

> I have a question regarding inner classes...
An interesting one, again ;-)

> Scenario:
> I want to replace a method of a class C, thus I created a role which
> implements a callin method.
> So far all is fine.
> But in this method there has to be instantiated a class D, which is an
> inner (not static) class of C! My solution for this issue was to
> decapsulate class D by another role inside the same team.
> 
> The concrete problem is, that if I want to call the base constructor via
> base(...), OTDT shows up an error marker:
> "No enclosing instance of type C is accessible. Must qualify the
> allocation with an enclosing instance of type C (e.g. x.new A() where x
> is an instance of C)."
> 
> Here is a snippet of the team class:
> public team class T {
> 
>   public class RoleA playedBy C {
> 
>     callin void methodXY(){
>       // some code
>       new RoleB(...);
>       // some code
>     }
>     methodXY <- replace methodXY;
>   }
> 
>   public class RoleB playedBy D {
>     public RoleB(...) {
>       base(...);  // *error marker*
>     }
>   }
> }
This error marker is actually correct. For creating a D outside C one MUST
write something like 
	C c = ...
	D d = c.new D();
In the constructor of RoleB such an instance of C is not available.

But with a slight modification this can be solved: just make RoleB an
inner role of RoleA (requires to declare RoleA as a nested team).

Still, 0.9.28 cannot cope with this situation but here is a preview of
how 1.0.0 handles this:
	http://www.objectteams.org/distrib/jacks/1.0.0/results/2.4.1-otjld-visible-base-class-19.html

Behind the scenes the base() statement in RI is translated to something like:
	this._OT$base = RM.this._OT$base.new InnerBase();
The path "RM.this._OT$base" indeed provides an instance of T241vbc19, the outer base class.
(I think you should appreciate that you don't have to manually write that weird
instantiation ;-)

From this test case one might generalize that the containment structures of base
and role sides should mirror each other.

Is this a workable approach for your application?

Stephan




More information about the otj-users mailing list