i applying aspectj our project, , found behavior bit strange me.
q1: added new constructor current class inter-type declaration, , found class's member variable not initialized if new constructor used instantiate class.
for example:
the class i'll add new constructor to:
public class child { public string name = "john"; public child(string desc) { // todo auto-generated constructor stub } }
the aspectj code:
public aspect mytest { public child.new(string desc, int num) { system.out.println("child name:" + this.name); } }
if instantiate child new constructor:
new child("a child", 5)
the member variable this.name not initialized done original constructor.
but, if call original constructor:
new child("a child")
the member variable this.name initialized "john" usual
the result:
child name:null
is limitation of aspectj? there anyway resolve issue?
i don't want add code member variable initialization new constructor.
q2: seems in newly added constructor, super.method() can not correctly resolved.
the class i'll add new constructor to:
public class child extends parent{ public string name = "john"; public child(string desc) { } }
child extends parent. parent has method init()
public class parent { public void init() { //.... } }
i add new constructor child in aspect.
public aspect mytest { public child.new(string desc, int num) { super.init(); } }
the above aspect code trigger exception.
exception in thread "main" java.lang.nosuchmethoderror: com.test2.child.ajc$superdispatch$com_test2_child$init()v @ mytest.ajc$postinterconstructor$mytest$com_test2_child(mytest.aj:19) @ com.test2.child.<init>(child.java:1) @ mainprogram.main(mainprogram.java:11)
my workaround define another method class child, , indirectly call super.method() within method
for example, add new method calls super.init() child
public void child.initstate() { super.init(); }
now, can call initstate() in newly added constructor below:
public aspect mytest { public child.new(string desc, int num) { this.initstate(); } }
is limitation of aspectj? way resolve issue?
thank time :)
for second question, think it's bug of aspectj. decompile woven target byte code find method “com.test2.child.ajc$superdispatch$com_test2_child$init()v” inserted. implies method should generate aspectj, there no such method in byte code.
Comments
Post a Comment