[
Date Prev][
Date Next][
Thread Prev][
Thread Next][
Date Index][
Thread Index]
[
List Home]
Re: [aspectj-users] Can an around advice for a constructor return a different Class of object?
|
You can return a subtype of expected result from the around-advice,
but not a supertype. In this case, the pointcut
(List+.new(int))
can pick out any subtype of List, e.g., ArrayList. But
NSMutableArray is not a subtype of ArrayList.
You can replace ArrayList with your own subtype of ArrayList:
ArrayList around(int size) : call(ArrayList.new(int))
&& args(size) {
return new MyArrayList(size);
}
ArrayList around(Collection e) : call(ArrayList.new(Collection))
&& args(e) {
return new MyArrayList(size);
}
ArrayList around() : call(ArrayList.new()) {
return new MyArrayList();
}
You might have to work around the costs of ArrayList in your
implementation, e.g., by implementing a dummy size:
MyArrayList(int size) {
super(0);
...
}
Sorry that Java type-safety won't permit what you want.
Wes
> ------------Original Message------------
> From: "Watkins, Garry" <gwatkins@xxxxxxxxxxxxxx>
> To: aspectj-users@xxxxxxxxxxx
> Date: Tue, May-30-2006 12:19 PM
> Subject: [aspectj-users] Can an around advice for a constructor return a different Class of object?
>
> I have a framework that I would like to use, that generates a bunch of
> ArrayList objects, what I would like to do is to trap the constructor
> calls
> and replace the ArrayList with another type of List. Is this possible?
> Basically, I want to create a ListFactory by using aspects.
>
> This is what I have currently:
> Object around(int capacity): call(!NSArray+ && List+.new(int)) &&
> within(org.objectstyle..*) && args(capacity){
> return new
> NSMutableArray(capacity);
> }
>
>
> However, I am getting a ClassCastException. The NSMutableArray
> implements
> the List interface.
>
> Thanks for any help.
> --
> Garry Watkins
>
> Database Administrator
> Motley Rice LLC
> 843-216-9639