// I want to improve the printing of rationals AND complex numbers privileged aspect printing // privileged aspects can access private vars. { /* use this advice to debug pointcut expressions: before(Object R) : call(public String *.toString()) && target(R) { System.out.println(thisJoinPoint.getStaticPart( ).getSignature( )); System.out.println(R.getClass()); } */ // when identifying functions that override built-ins, use execution String around(rational R) : execution(String rational.toString()) && this(R) { if (R.n%R.d==0) return ""+(R.n/R.d); // don't print denominator else return proceed(R); // else proceed with call } String around(complex C) : call(String complex.makestring()) && target(C) { // if the imaginary part is 0, then don't print it if (C.ip==0.0) return C.rp+""; else return proceed(C); } // advice to intercept calls in System.out.println void around(complex C) : call(void *.println(..)) && args(C) && !adviceexecution() { System.out.println(C.makestring()); } // add test case to main for the new feature: after() : execution(public static void *.main(String[])) { System.out.println( new rational(6,2) ); System.out.println( new complex(2,0) ); // no .makestring()!; System.out.flush(); // flush print buffer } }// printing aspect /* What is interesting about this code is that it CROSSCUTS the two classes. */