CS 541 Meeting -*- Outline -*- * Examples of AspectJ Use the command line tools and Eclipse to show how to program Main reference (esp. for examples): AspectJ Progrmmer's guide See also AspectJ in Action by Ramnivas Laddad (Manning). ** Basic tool use and join point model example *** Command line tools ------------------------------------------ COMMAND LINE TOOLS $ cd joinpoints $ ajc *.java $ java Test ------------------------------------------ The -argfile option can be used to name particular file, in some order ajc -argfile billing.lst CLASSPATH needs to include InstallDir/lib/aspectjrt.jar *** Eclipse IDE ------------------------------------------ USING ECLIPSE $ eclipse ------------------------------------------ show how to start a project, import files build, run Look at the code in the classes of jointpoints/*.java to see the advice markers, etc. *** playing with the example what happens if you replace !within(JoinPointTraceAspect) with if(true)? Look at the javadocs for org.aspectj.lang.JoinPoint, which is the type of thisJoinPoint Q: How would you enforce that the amount passed to credit must be non-negative? use before advice and throw an exception, in new aspect! do same using around advice Q: How would you enforce this for both credit and debit? use a type pattern in the pointcut description ** tracing shape example From AspectJ Programmer's guide chapter 3, tracing section Show the TwoDShape and its subclasses To compile without aspects, use cd examples ajc -argfile tracing/notrace.lst java tracing.ExampleMain *** simple See version1/Trace.java then version1/TraceMyClasses.java is an aspect that puts in calls ajc -argfile tracing/tracev1.lst *** reusable See version2/Trace.java version2/TraceMyClasses.java is a concrete aspect that extends this ajc -argfile tracing/tracev2.lst ** subject/observer example cd examples/observer "The demo consists of the following: A colored label is a renderable object that has - a color that cycles through a set of colors, and - a number that records the number of cycles it has been through. A button is an action item that records when it is clicked." "With these two kinds of objects, we can build up a Subject/Observer relationship in which colored labels observe the clicks of buttons; that is, where colored labels are the observers and buttons are the subjects." see the Subject and Observer interfaces look at the SubjectObserverProtocol.java class Instantiation is in the classes - Button (subjects) - ColorLabel (observers) - and the aspect SubjectObserverProtocolImpl (glue) ajc -argfile observer/files.lst java observer.Demo ** others See also Laddad's chapter 8 for some neat examples