Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » AJDT » Method with Aspect getting called twice(Method with Aspect getting called twice)
Method with Aspect getting called twice [message #485024] Thu, 10 September 2009 05:59 Go to next message
LSK  is currently offline LSK
Messages: 2
Registered: September 2009
Junior Member
Hi,

I built a small application with one aspect .
The code for the Aspect is :

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.METHOD })
public @interface Validate {

}


@Aspect
public class ValidationAspect {

    @Before("@annotation(com.Validate)")
    public void invokeMethod(final JoinPoint joinPoint)
        throws Throwable {
    	System.out.println("Aspect Called");
    }
}


I applied this aspect on a class :

package com;

public class Address {
	
	private String city;

	@Validate
	public String getCity() {
		return city;
	}

	public void setCity(String city) {
		this.city = city;
	}
	
}



And I used a test class to invoke Address.getCity()

class Main {
      public static void main(String[] args) {
		Address a =new Address();
		a.getCity();
      }
}



Now when a.getCity() is called , the Aspect is invoked twice . My Eclipse project is AspectJ enabled and when I look at the class of Main.java it looks like this:

    Address a = new Address();
    Address localAddress1 = a;
    JoinPoint localJoinPoint2 = Factory.makeJP(ajc$tjp_1, null, localAddress1);
    ValidationAspect.aspectOf().invokeMethod(localJoinPoint2);
    localAddress1.getCity();


This is why it is getting invoked twice . Why so ?
Re: Method with Aspect getting called twice [message #486788 is a reply to message #485024] Fri, 18 September 2009 23:53 Go to previous message
Andrew Clement is currently offline Andrew Clement
Messages: 162
Registered: July 2009
Senior Member
Your use of '@annotation()' selected all joinpoints in program execution where the subject of the joinpoint has the specified annotation.

When your program runs there is a method-call joinpoint to your annotated method followed immediately by a method-execution joinpoint for your annotated method. If you change the advice to print the joinpoint object, you will see these two joinpoints.

What you need to do is select which joinpoint you are interested in. Probably method-execution in your case:

@Before("execution(* *(..)) && @annotation(com.Validate)")

But if you don't even want the annotation in your advice you could perform a purely static match (currently you are performing a dynamic match). Static match would be with this:

@Before("execution(@com.Validate * *(..))")

Andy

ps. you will get better support on the aspectj/ajdt mailing lists than on the newsgroup.
Previous Topic:Restrict weaver to only certain packages
Next Topic:Method with Aspect getting called twice
Goto Forum:
  


Current Time: Tue Jul 22 09:31:07 EDT 2014

Powered by FUDForum. Page generated in 0.01892 seconds