// // Example Aspect for Java Security Presentation // Chris Durham, May 26th, 2005 // CSCI 297 // This is an example Aspect. As oposed to the provided set of Aspects, // which you use by creating a .jac and .acc file, you can also create and // compile your own Aspects. // All this aspect does is for each invocation of a method called iAmHere in // the Class TestObject, execute the myWrapper method invoke(), which just // prints the number of times the method has been executed import org.objectweb.jac.aspects.tracing.*; import org.aopalliance.intercept.*; import org.objectweb.jac.core.*; // An aspect extends the AspectComponent public class MyAC extends AspectComponent { // counter static int i = 0; // constructor public MyAC() { // declare the Wrapper AWrapper myWrapper = new AWrapper(this); // The pointcut() method declares: // first arg: which instance (ALL) in this case // 2nd arg: of which class (TestObject) // 3rd arg: which method (iAmHere) // 4th arg: what class to invoke (myWrapper) // 5th arg: if there is an exception handler, it goes in this argument // 5th arg: whether this is a one-to-any mapping or used only once // Note: there are other formats for pointcut(). This is only one. // Note that the invoke() method of the class to call (myWrapper) is the // method executed unless otherwise specified // pointcut() can take a modified regular expression syntax. See the doc // at http://jac.objectweb.org pointcut("*","*TestObject*","*iAmHere*", "*myWrapper*",null,false); } public class AWrapper extends Wrapper { public AWrapper(AspectComponent ac) { super(ac); } // Here is the invoke() method. // proceed() allows the initial functionality of the method (iAmHere() in this case) // to be executed, In this case we print out the number of times the method has been // executed, after the original functionality has completed. We can add functionality // before and/or after the proceed(), or we can // not call proceed() at all and bypass all initial functionality public Object invoke(MethodInvocation mi) { Object o; o= proceed(mi); i++; System.out.println("Been here: " + i + " times\n"); return(o); } } }