Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » AspectJ » Method with Aspect getting called twice
Method with Aspect getting called twice [message #600681] Thu, 10 September 2009 09:59 Go to next message
LSK  is currently offline LSK Friend
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 #600698 is a reply to message #600681] Sat, 19 September 2009 03:53 Go to previous message
Andrew Clement is currently offline Andrew ClementFriend
Messages: 162
Registered: July 2009
Senior Member
Your use of mailto:'@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:Method with Aspect getting called twice
Next Topic:Scope of Aspect Defined in Fragment
Goto Forum:
  


Current Time: Fri Apr 26 06:51:53 GMT 2024

Powered by FUDForum. Page generated in 0.03532 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top