Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[aspectj-dev] aspect keyword != @Aspect ?

I'm having difficulty understanding the difference between the aspect keyword and the @Aspect annotation. According to the documentation the aspect keyword and the @Aspect annotation are the same. In practice, this seems not to be true; or I am making a giant mistake.

http://www.eclipse.org/aspectj/doc/released/adk15notebook/ataspectj.html

"The use of the @AspectJ annotations means that there are large classes of AspectJ applications that can be compiled by a regular Java 5 compiler, and subsequently woven by the AspectJ weaver (for example, as an additional build stage, or as late as class load-time). In this chapter we introduce the @AspectJ annotations and show how they can be used to declare aspects and aspect members."

The example on the next page reenforces this notion:

"The declaration:
     @Aspect
public class Foo {}
Is equivalent to:
     public aspect Foo {}
"
So that's why I think these two methods should produce equivalent results, below is why I'm finding they do not:

The two simple examples show below do not produce equivalent output. Its easy to see after building each separately and dumping the class signatures with javap, its easy to see there are lots of generated methods added to the TestAspect.class from sample #1, sample #2 on the other hand doesn't contain any of these things.

// Sample #1
// TestAspect.java
public aspect TestAspect {


  @Pointcut("execution(* *(..))")    
  public void pc() {}

  @Before("pc()")
  public void beforePc() {
  }

}

// Sample #2
// TestAspect.java
@Aspect
public class TestAspect {


  @Pointcut("execution(* *(..))")    
  public void pc() {}

  @Before("pc()")
  public void beforePc() {
  }

}

If I take it a step further and actually apply the aspect to a simple HelloWorld example with the compile time weaver. The first version using the keyword runs just fine. Dumping the code with javap I can confirm that the aspect was woven into the class. The second version using the aspect does not work. The HelloWorld program references methods that are not actually generated for the aspect version (TestAspect.aspectOf() for example).

I haven't attempted load time weaving, but I will not be using this in my appliciation.

I am building this all with the iajc ant task.

Can anyone explain what I'm doing wrong?





- Eric


See the all-new, redesigned Yahoo.com. Check it out.

Back to the top