Homework 5 Enforcing rules // Observing // Security Due Feb 12, 2002 Consider the incomplete banking application in directory bank. Compile with: ajc *.java Run with: java Main PART 1: Write an aspect that checks the rule: all objects should be created by parsing. Objects should not be created outside the parser. Print a message if this happens. Can you write a pointcut designator that detects violations already at compile time? PART 2: Write an aspect that measures and prints out to a log file how long each call to method addBusinessUnit lasts. PART 3: Write a simple security aspect that checks that users who add a BusinessUnit are properly authorized. When a call to addBusinessUnit happens, the caller must know a secret password, such as 673489. Only when the caller knows this secret, will the business unit be added. PART 4: Combining AspectJ and Demeter THIS PART DOES NOT HAVE TO BE COMPLETED THIS WEEK. Do a first growth phase only: From 1 to 2.1. The task is to write a translator from a language L1 to a language L2. The language L1 consists of all of Java plus a traversal language T. The output language consists of a small subset of AspectJ. The purpose of the translation is to translate high-level traversal specifications to Java traversal methods. The code is in: /proj/adaptive2/course/com1205/w02/project-prep/testing Example input: Java: class Basket { Fruit f; Pencil p; } class Fruit { Weight w;} class Orange extends Fruit { Color c; } class Pencil {} class Color { String s; } class Weight{ int i; } class Main { static public void main(String args[]) throws Exception { } } T: aspect Traversals { declare traversal t1 = Basket to *; declare traversal t2 = Basket to Weight; } output: aspect TraveralsImplementation { void Basket.t1(){ if (f != null) t1_f(); if (p != null) t1_p();} void Basket.t1_f(){f.t1();} void Basket.t1_p(){p.t1();} void Fruit.t1(){t1_w();} void Fruit.t1_w(){w.t1();} void Orange.t1(){t1_c(); super.t1();} void Orange.t1_c(){c.t1();} void Pencil.t1(){}; void Color.t1(){}; void Weight.t1(){}; void Basket.t2(){t2_f();} void Basket.t2_f(){f.t2();} void Fruit.t2(){t2_w();} void Fruit.t2_w(){w.t2();} void Orange.t2(){super.t2();} void Weight.t2(){}; } aspect TraversalsTest { pointcut mainCall() : call(void main(..)); before() : mainCall() { // create Basket b Basket b = new Basket(); b.f = new Orange(); b.t1(); b.t2(); pointcut nodeTraversals(Object o): call(void t1()) && target(o); pointcut edgeTraversals(Object s, Object t): withincode(void t1_*(..)) && call(void t1()) && this(s) && target(t); before(Object o) : nodeTraversals(o) { System.out.println("traversing node " + o); } before(Object s, Object t) : edgeTraversals(s, t) { System.out.println( "traversing edge: source= " + s + " target= " + t); System.out.println(thisJoinPoint); } // same but replace t1 by t2 } The project consists of the following steps: 1. Produce the class graph cg from the Java program. 2. For cg and a fixed traversal strategy produce the traversal graph. 2.1. Generate the traversal graph for each strategy appearing in T. Translate the T file, into a list of strategies and print them to a file. Read from this file to construct the traversal graphs using DJ. 3. Generate the traversal code from a fixed traversal graph using the interface of the AP Library available in DJ. 4. Test the generated traversal code manually. 5. Automate 2./3./4.: Read in traversal graphs from file Traversals.java and translate them into a file that, when compiled by AspectJ and executed, will produce the file TraversalsImplementation.java (containing the traversal code) and TraversalsTest.java (containing the test code). Note: Artan has a better way to generate the traversal: aspect TraveralsImplementation { void Basket.t1(){ if (f != null) crossing_f(f); if (p != null) crossing_p(p);} // use if throughout void Basket.crossing_f(Fruit f){f.t1();} void Basket.crossing_p(Pencil p){p.t1();} The we can define pointcuts like: pointcut crossing_fFromBasket(Basket b, Fruit f): call(void Basket.crossing_f(..)) && this(b) && args(f);