Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [aspectj-users] Behaviours of new constructor added by AspectJ ITD

Hi! thanks for the response.

But it seems you have some misunderstanding about my questions.

So I made some modifications to make it clear.

Thank you for the help :)



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 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 I instantiate the Child with the new constructor:

    new Child("A child", 5)

the member variable **this.name** is not initialized as will be done with
the original constructor.

But, if I call the original constructor:

    new Child("A child") 

the member variable **this.name** will be initialized to "John" as usual

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 class which I'll add a new constructor to:

    public class Child extends Parent{
    
        public String name = "John";
        
        public Child(String desc) {

        }
    } 

**Child** extends **Parent**. **Parent** has a method **init()**

    public class Parent {
    
        public void init() {
            //....
        }
    
    }

I add a new constructor for the **Child** in my aspect.
    public aspect MyTest {
        public Child.new(String desc, int num) {
            super.init();
        }
    }

The above aspect code will trigger an exception.

    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)

My workaround is to define **another method** for my class **Child**, and
indirectly call the super.method() within that method


For example, add a new method that calls **super.init()** for **Child**
        
    public void Child.initState()
    {
        super.init();
    }

Now, I can call initState() in the newly added constructor like below:

    public aspect MyTest {
        public Child.new(String desc, int num) {
            this.initState();
        }
    }

**Is this a limitation of AspectJ? Is this the only way to resolve this
issue?** 

Thank you all for your time :)



--
View this message in context: http://aspectj.2085585.n4.nabble.com/Behaviours-of-new-constructor-added-by-AspectJ-ITD-tp4651015p4651019.html
Sent from the AspectJ - users mailing list archive at Nabble.com.


Back to the top