[
Date Prev][
Date Next][
Thread Prev][
Thread Next][
Date Index][
Thread Index]
[
List Home]
RE: [aspectj-users] Modifying constructors
|
Thanks for the advice Eric, was very helpful in understanding the
approach I need to take.
I tried doing as you suggested but came across another issue:
I tried the following:
1. Car around(String t, int my): CarConstructor(t,my)
2. {
3. String Color = getColor(); //Obtained this using a simple user input
4. return new Car(t,color,my);
5. }
The problem I faced in this was the compiler was giving error on Line
4 that the Constructor Car(String, String , int) was not defined.
So I created another method Car(String t, String Color, int my) with a
return type of Car (I had to specify a return type, otherwise it gives
an error)
As a result I ended up having another method which was something like this:
public Car Car(String t, String Color, int my)
{
my_Type = t; //Locally defined variables
my_Color = Color;
my_Model = my;
return Car(my_Type, my_Color, my_Model);
}
I don't think I should be returning anything when I am creating a
constructor but am forced to return that type.
Even with this setup in place, I get the same error message as earlier
that "Constructor Car(String, String, int) is undefined"
The only way I have been able to compile without errors is by
modifying Line 4 of the first method to be:
return Car(t, color, my);
instead of "return new Car(t, color, my)" which fixes the compilation
issues but creates StackOverflow error during runtime (recursive loop
maybe?)
Again I think I am in a pickle because of a conceptual mistake in
creating and using constructors.
I think I must've made this as clear as mud so just a brief overview
of what I am trying to achieve here :
- I originally had a Car constructor with 2 parameters.
- A hardcoded file output stream in the lines of
objOut.writeObject(new Car("Sedan", 2004); creates these objects and
writes them to a file
- Now I am trying to add a 3rd attribute to this object for which I
need to modify the original constructor and send a new outputstream
such as objOut.writeObject(new Car("Sedan", "Red", 2004);
Thanks again for your time and feedback
Regards,
Sarthak
==================
Date: Mon, 20 Nov 2006 21:09:43 -0500
From: "Eric Bodden" <eric.bodden@xxxxxxxxxxxxxx>
Subject: RE: [aspectj-users] Modifying constructors
To: <aspectj-users@xxxxxxxxxxx>
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
==================