Replied on stackoverflow.
On Mon, Jul 15, 2013 at 8:03 AM, pai <[email protected]> wrote: > Hi! folk~ I have a question about behaviours of new constructor added by > AspectJ ITD > > > I am currently applying AspectJ to our project, and I found a behavior > which > is a bit strange to me. > > **Q1:** > I added a new constructor to my current class with inter-type declaration, > and found that the class's member variable is not initialized if the new > constructor is used to instantiate my class. > > For example: > > The class which I'll add a new constructor to: > > * public class Child { > > public String name = "John"; > > public Child(String name) { > // TODO Auto-generated constructor stub > } > } * > > The aspectJ code: > > * public aspect MyTest { > public Child.new(String name, int age) { > System.out.println("Child Name:" + this.name); > } > }* > > If I new the Child with the new constructor, the member variable name is > not > initialized as will be done with the original constructor. > > The result: > *> Child Name:null* > > Is this a limitation of AspectJ? Is there anyway to resolve this issue? > > I don't really want to add the code for member variable initialization to > the new constructor. > > **Q2:** > It seems in the newly added constructor, super.method() can not be > correctly > resolved. > > The workaround is to define another method for your class, and indirectly > call the super.method() > > For example, if the Child extends Parent. > > * public class Parent { > > public final void init() { > //.... > } > > }* > > I'll have to add a new metohd, say > > > * public void Child.initState() > { > super.init(); > }* > > Or the following exception will be thrown. > > * Exception in thread "main" java.lang.NoSuchMethodError: > com.test2.Child.ajc$superDispatch$com_test2_Child$init()V > at > MyTest.ajc$postInterConstructor$MyTest$com_test2_Child(MyTest.aj:19) > at com.test2.Child.<init>(Child.java:1) > at MainProgram.main(MainProgram.java:11)* > > Is this a limitation of AspectJ? Is this the only way to resolve this > issue? > > > Thank you all for your time :) > > You can also answer my questions on stackoverflow. Many thanks!! > > > http://stackoverflow.com/questions/17647587/behaviours-about-new-constructor-added-by-aspectj-itd > > > > -- > View this message in context: > http://aspectj.2085585.n4.nabble.com/Behaviours-of-new-constructor-added-by-AspectJ-ITD-tp4651015.html > Sent from the AspectJ - users mailing list archive at Nabble.com. > _______________________________________________ > aspectj-users mailing list > [email protected] > https://dev.eclipse.org/mailman/listinfo/aspectj-users >
_______________________________________________ aspectj-users mailing list [email protected] https://dev.eclipse.org/mailman/listinfo/aspectj-users
