Here is the annotation:
@Target(value = ElementType.TYPE)
@Retention(value = RetentionPolicy.RUNTIME)
@Inherited
public @interface MyAnnotation {
String name();
}
Here is one annotated class:
@MyAnnotation(name="foo")
public class ClassA {
public ClassA() {
// Do something
}
}
Here is a second annotated class:
@MyAnnotation(name="bar")
public class ClassB {
public ClassB(String aString) {
// Do something
}
}
I am looking for an aspectj pointcut that correctly matches the constructors for ClassA and ClassB while not matching any other constructor for any other class NOT annotated by MyAnnotation
.
Here is the working solution from kriegaex in its entirety:
Your pointcut should look like this:
If the annotation is in another package:
Or if you do not want to fully qualify the package:
Edit: Okay, some more info about your question in the comment:
Constructor executions have no return value which you could capture in
This only works for methods. So please use
instead if you do need the constructed object for any purpose.
THE FOLLOWING WORKS, BUT IS NOT RECOMMENDED BY kriegaex. PROVIDED HERE AS POSSIBLE MATERIAL THAT COULD BE REPURPOSED IF THE NEED ARISES.
This was my first working solution to the problem which uses in part the initialization() pointcut primitive.