[
Date Prev][
Date Next][
Thread Prev][
Thread Next][
Date Index][
Thread Index]
[
List Home]
[aspectj-users] Interfaces vs Concrete Classes?
|
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 the
Yahoo! Search weather shortcut.Attachment:
test-code.zip
Description: Zip compressed data