Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
RE: [aspectj-users] Modifying constructors

Hello.

I think the main problem is that you are using an execution pointcut, plus I don't understand what that is that you write in your advice body. 

So conceptually you want to intercept the creation of a car and create the car by invoking another constructor. How does one invoke a constructor? By using "new". So thy the following, I think this should work for you.

pointcut CarConstructor(String Type, int ModelYear): call(Car.new(..)) && args(Type,ModelYear);

Car around(String t, int my): CarConstructor(t,my)
{
	Color = getColor(); //not even sure where you wanna get the color from
	return new Car(t,color,my);
}

Note that I forward the original arguments. Also note that the around advice does not "proceed", i.e. the original joinpoint (constructor) is never called.

Eric

--
Eric Bodden
Sable Research Group, McGill University
Montréal, Québec, Canada


> -----Original Message-----
> From: aspectj-users-bounces@xxxxxxxxxxx [mailto:aspectj-users-
> bounces@xxxxxxxxxxx] On Behalf Of Sarthak Grover
> Sent: Monday, November 20, 2006 6:56 PM
> To: aspectj-users@xxxxxxxxxxx
> Subject: [aspectj-users] Modifying constructors
> 
> Hello,
> 
> I am fairly new to AspectJ and am stuck at a point and would really
> appreciate your help.
> 
> I am trying a simple program in which I have a constructor method to
> create a "Car" object.
> 
> Example of the constructor:
> 
> public Car(String Type, int ModelYear)
> {
> my_Type = Type;
> my_ModelYear = ModelYear;
> }
> 
> 
> What I am trying to do is that anytime this constructor is called, I
> would like to create a Car object but with different attributes instead
> of the original constructor:
> 
> public Car(String Type, String Color, int ModelYear) { my_Type = Type;
> my_Color = Color; my_ModelYear = ModelYear; }
> 
> 
> For this I createad the following pointcut and advice:
> 
> pointcut CarConstructor()
>  : execution(Car.new(..));
> 
> 
> around(): CarConstructor()
> {
>   Car(String Type, String Color, int ModelYear); }
> 
> String my_Type, my_Color;
> int my_ModelYear;
> 
> public void Car(String Type, String Color, int Model) {
>      my_Type = Type;
>      my_Color = Color;
>      my_ModelYear = ModelYear;
> }
> 
> 
> Should I be capturing the arguements from the original constructor and
> send them to the new constructor along with the exta parameter "Color"
> or can this be achieved with a simpler approach?
> 
> Thanks!
> 
> Sarthak


Back to the top