Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [aspectj-users] Bad code smells in AspectJ?

Yes, this works fine. In fact, using this approach means you can change the pointcut to
 
  pointcut myPct() : call(public T.new(..));

and it will match any constructor. The "proceed()" call doesn't require any arguments; it will use the original arguments to the constructor.

Note that you have to declare the aspect as abstract and create concrete subaspects that specify the type. For example,

abstract public aspect X<T> {...}

public aspect FooX extends X<Foo> {}

(Note that the body can be empty...)

dean

On Apr 17, 2008, at 11:59 AM, Simone Gianni wrote:

Hi neil,
maybe I'm wrong, but if you are intercepting a constructor, can't you
simply call proceed() to obtain the correct instance ?

public aspect X<T>  {
   pointcut myPct() : call(public T.new());

   T around() : myPct()  {
       T ret = (T)proceed();
       if(someTest)  {
           // do something with ret
       }
return ret;
   }
}


I can't remember exactly, but I think I already used an around advice on
a costructor to intercept object creation, an I suppose this can work
also on generics as long as rest of the aspect is supported.

Hope this helps,
Simone

neil loughran wrote:
Hi,

I have an issue where I am seeing the same kind of code in several of my
aspects.  For example in the following SomeClass appears at several points
which makes me think generics would be a solution.


public aspect X  {
   pointcut myPct() : call(public SomeClass.new());

   SomeClass around() : myPct()  {
       if(someTest)  {
           SomeClass s = new SomeClass();  
           // do something with s
           return s;
       }
       else proceed();
   }
}


Generic version.

abstract aspect X<T>  {
   pointcut myPct() : call(public T.new());

   T around() : myPct()  {
       if(someTest)  {
           T s = new T();  // error
           // do something with s
           return s;
       }
       else proceed();
   }
}


Unfortunately, it is not possible to instantiate a generic type from within
the aspect.  Just wondering if there was a way around this?   

Regards
Neil

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


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

Dean Wampler, Ph.D.
dean at objectmentor.com
See also:
http://aquarium.rubyforge.org     AOP for Ruby
http://www.contract4j.org         Design by Contract for Java5




Back to the top