[
Date Prev][
Date Next][
Thread Prev][
Thread Next][
Date Index][
Thread Index]
[
List Home]
Re: [aspectj-users] Interfaces vs Concrete Classes?
|
Kyle,
As you may know the annotations on methods are not inherited by
implementing or overriding methods. You could:
1. Use an execution pointcut designator, and then access the signature
and check for the annotation on the implementation method, and also
use a call pointcut designator for the calls on the interface method
and check on the annotation. You could use a variable to track if the
annotation exists on either in your aspect.
Note: This may not be an option if you cannot use execution pointcut designator.
2 In adition to the code in MethodAnnotationAspect, you could use
thisJoinPoint.getTarget (I think this returns the object implementing
the interface) and then check the annotation.
Note: This is similar to 1 above except that it does not use the
execution pointcut designator.
3. you can use the target pointcut designator
http://www.eclipse.org/aspectj/doc/released/progguide/starting-aspectj.html#pointcuts
Monal
http://www.goi18n.com/
http://goi18n.com/
On 1/28/07, Kyle Lomeli <kyllerss_009@xxxxxxxxx> wrote:
I have run into a problem relating to how AspectJ treats method calls on
interfaces versus concrete classes. I am trying to obtain method-level
annotations that can be defined on either an interface or a concrete class.
The test case that I have provided shows that when a call is made against an
interface method, the method signature of that interface is used instead of
the concrete class that implements it.
Is there a convenient way (or "a" way) of obtaining the annotations of a
method regardless of whether or not the annotations are defined in the
interface or the concrete implementation?
Thanks to anyone with any suggestions!
PS. I have attached a ZIP file since cutting and pasting can be a pain :)
-Kyle
Output:
-------
start ---> void test.MainClass.printMessage()
Method annotations size -> 1
end ---> void test.MainClass.printMessage()
Annotation is visible!
start ---> void test.SomeInterface.printMessage()
Method annotations size -> 0
end ---> void test.SomeInterface.printMessage()
Annotation is not visible!
Concrete Class:
---------------
package test;
import test.annotations.MethodAnnotation;
public class MainClass implements SomeInterface {
private String message;
public void setMessage( String message ) {
this.message = message;
}
@MethodAnnotation
public void printMessage() {
System.out.println( message );
}
public static void main(String[] args) {
MainClass testClass = new MainClass();
testClass.setMessage( "Annotation is visible!" );
testClass.printMessage();
SomeInterface testInterface = new MainClass();
testInterface.setMessage( "Annotation is not visible!" );
testInterface.printMessage();
}
}
Interface:
----------
package test;
public interface SomeInterface {
public void setMessage(String message);
public void printMessage();
}
Aspect:
-------
package test.aop;
import java.lang.annotation.Annotation;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.reflect.MethodSignature;
import test.annotations.MethodAnnotation;
public aspect MethodAnnotationAspect {
pointcut securedInvocation() : call(* test.*.print*(..)) &&
if( hasAnnotation( thisJoinPoint ) ) &&
!within(MethodAnnotationAspect);
public static boolean hasAnnotation( JoinPoint jointPoint ) {
System.out.println( "start ---> " + jointPoint.getSignature() );
// find method annotation
try {
Signature signature = jointPoint.getSignature();
if ( signature instanceof MethodSignature ) {
MethodSignature methodSignature = ( MethodSignature
)signature;
System.out.println( "\tMethod annotations size -> " +
methodSignature.getMethod().getAnnotations().length );
Annotation annotation =
methodSignature.getMethod().getAnnotation(
MethodAnnotation.class );
return annotation != null;
}
return false;
}
finally {
System.out.println( "end ---> " + jointPoint.getSignature() );
}
}
before() : securedInvocation() {
// do something
}
}
________________________________
Don't get soaked. Take a quick peak at the forecast
with theYahoo! Search weather shortcut.
_______________________________________________
aspectj-users mailing list
aspectj-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/aspectj-users