import java.io.*; import java.net.*; import java.util.*; class student { public final String name; private double gpa; // note private public student(String n) { name=n; gpa = ((int)(Math.random()*401))/100.0; } public String toString() { return "student "+name+" has a "+gpa+" GPA"; } public void run() { System.out.println("huff and puff..."); } } class vehicle { public final String make; public final String model; public final int year; public int price = 30000; public vehicle(String b, String m, int y) {make=b; model=m; year=y;} public String toString() {return year+" "+make+" "+model;} public void run() { System.out.println("zoom zoom"); } }//vehicle class pc { public final String cpu; public int RAM; // in GB public int Storage; // in TB public int price = 1000; public pc(String c, int r, int s) { cpu=c; RAM=r; Storage=s; } public void run() { throw new RuntimeException("Segmentation Fault. Core Dumped"); } } // Cross cutting aspects of the program: aspect trace // trace the construction of each object, plus run function { // after advice after(Object c) : (execution(student+.new(..)) || execution(pc.new(..)) || execution(vehicle.new(..))) && target(c) && !adviceexecution() { System.out.println("Object created: "+c ); } }//trace // aspect Autoretry aspect FaultTolerance { private static int max = 3; public static void setmax(int m) {max=m;} void around(Object c): call(void *.run()) && target(c) { int cx = max; // allow 3 attempts to run while (cx-->0) { try { proceed(c); // proceed with run() on target c cx = 0; // stops while loop } catch (RuntimeException re) { if (cx==0) System.out.println("run failed after "+max+" tries"); } }//while }//around advice } // censorship advice aspect WholesomeOutput { void around(String s):call( void PrintStream+.print*(..)) && args(s,..) { s = s.replaceAll("hell","heck"); s = s.replaceAll("damn","darn"); s = s.replaceAll("Liang is bad","Liang is badass"); proceed(s); } } public class aop17 { public static void main(String[] av) { System.out.println("What the hell is going on!"); student s = new student("Farman Imal"); vehicle v = new vehicle("Honda","Accord",2008); pc yourpc = new pc("intel i7",16,5); // 16gb ram, 5tb disk s.run(); v.run(); yourpc.run(); }//main } // Real extension methods, supporting dynamic dispatch class studentathlete extends student { String sport = "basketball"; public studentathlete(String n) { super(n); } public void run() { System.out.println("whoooosh!"); } } // aspect to add new method to both student and studentahlete interface stuinterface { int tuition(); } privileged aspect extendstudent // privileged can access private fields { declare parents: student implements stuinterface; // now *obligated* to extend student classes public int student.tuition() { if (this.gpa>3.7) return 10000; // scholarship student else return 40000; } public int studentathlete.tuition() { return 0; } // all student athletes are on scholarship // changing main without changing main... after() : execution(public static void *.main(String[])) { student s1 = new studentathlete("Farma Nimal"); System.out.println(s1.tuition()); // dynamic dispatch? YES } }//extendstudent privileged aspect