Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [aspectj-users] proxying subclasses

Hi !

If I understand you, you can use perthis. Here is a quick snippet of code. WARNING: Syntaxis not tested, perhaps you'll need to refine the idea too.

/* This creates a FooStub instance per each Foo instance */
public aspect FooStub implements Foo perthis(this(Foo+&&(!FooStub)))
{
   /* Add here implementation for Foo methods */
}

/* This intercepts all calls to Foo objects except FooStub and redirects them to the stub */
public aspect FooInterceptor
{
pointcut interceptSomeMethod(Foo instance, /* parameters of someMethod */): call(Foo+.someMethod(..)) && this (!FooStub) && this(instace) && /* extract parameters of some method */;

void around(Foo instance, /* parameters of someMethod */): interceptSomeMethod(/* etc */)
   {
         /* Get the FooStub instance associated to instance*/
         Foo stub=FooStub.aspectOf(instance);
         stub.someMethod(/*parameters here*/);
} }

Hope this helps you !

Enrique J. Amodeo Rubio.

Jeffrey D Palm wrote:

Wes Isberg wrote:

Normally you can supply a stub/mock/proxy as follows:

  Foo around() : call(Foo.new(..)) {
      return FooStub.create(thisJoinPoint.getArgs());
  }

but this fails to handle subclasses, esp. anonymous ones:

  void someMethod() {
      Foo me = new Foo("foo") {
          public String toString() { return "bar"; }
      };
  }


OK, my last message was pretty convoluted. But why not, instead of creating a proxy via FooStub, just created on with advice. That is, intersept the calls you'd like to replace by using a proxy with around advice, instead of actually creating a proxy object?

Jeff

The only alternative seems to be some combination of advising all the target method-call and field g/set join points, or using declare-parents to interpose the stub:

  declare parents (Foo+ && !Foo) : FooStub;

Both of those have their own drawbacks.

Can anyone think of a good workaround?

Wes

_______________________________________________
aspectj-users mailing list
aspectj-users@xxxxxxxxxxx
http://dev.eclipse.org/mailman/listinfo/aspectj-users







Back to the top