Aspect-Oriented Programming in Java Adaptive Programming in Java what is new over: Adaptive Programming in C++ partial class structures, late binding of methods to classes, not good? AP is useful independent of AOP. AOP is about cross-cutting concerns, tangling control. AP is about loose coupling between building blocks. No. Too much fits? Good: AP is about loose coupling between software artifacts based on succinct subgraph specs. Now: subgraph of object graph. special case: AP is about controling object traversals (which subobject is releavnt). AOP is about programming cross-cutting concerns. Combine the two: one aspect may be represented by a graph, another aspect by a subgraph of that graph. Computation: graph with nodes and edges. Code attached: before, after, around. Example: object graph: structure. subgraph. traversal: kind and order. Code to execute before/after/around edges. Call graph: nodes: objects, edges: message sends. Subgraph of nodes and edges where additional code is executed before, after, around. Define subgraph by a program programmatically: kind of succinct specification. Other ways of defining subgraphs by a program. AspectJ. Aspectual Adaptive Programming. aspects represented as graphs: Make concepts of AP available to programmer Goals: control tangling, control redundancy Partition programs into cross-cutting, loosely coupled building blocks so that a change in one building block has a limited impact on the other building blocks. Tangling: Behavior with behavior, structure with behavior Implementation is done by reflection. But it could be optimized by preprocessing the Java program based on the knowledge that certain objects never change: ClassGraph objects, Strategy-objects, Visitor-objects. Building blocks ClassGraph ObjectGraph (conforms to ClassGraph) Strategy = subgraph of transitive closure of ClassGraph ObjectGraphSlice = ObjectGraph + ClassGraph + Strategy = subgraph of ObjectGraph Visitor = advice for object graph slice nodes and edges. Each visitor method defines a pattern that some object graph slice nodes and edges satisfy. previous, current, next OPath = Path in ObjectGraph CPath = Path in ClassGraph SPaths = Paths in StrategyGraph : multiple paths possible, e.g., A -> B and A -> C A -> B and A -> B B -> C There are multiple paths because of non-deterministic nature of strategy graphs. For efficiency: TraversalGraph = ClassGraph + Strategy An object graph slice is a subgraph of an object graph. Traverse an object graph slice: points of interest during a traversal of an object graph slice: before or after or around a node before or after or around an edge before entry into a subgraph or after leaving a subgraph Because ogss are very common, we introduce a domain specific language for selecting ogss from an og. Uses info in class graph to make selection of ogss more convenient. A strategy is a mapping that maps object graphs to subgraphs of those object graphs. void before (Node host) defines advice for all object graph slice nodes that are an instance of a subclass of Node. void cbefore_x(Source s, Target t){} defines advice for all object graph slice edges that satisfy the following pattern: The source is an instance of a subclass of Source. The target is an instance of a subclass of Target and the edge label is "x". void f(ObjectGraphSlice ogs) { ogs.traverse(new Visitor() { void start() {}; void finish() {}; void before (Node h) { if (ogs2.contains(h)) ... if (currentPath().lastSourceName().equals("Married")) ... }; void cbefore_x(Source s, Target t){ if ogs2.contains(s,"x",t){} if ogs2.contains(currentPath()) // current path completely contained in ogs2 if currentPath().containsNode(x)?? if currentPath().containsEdge(x,"A",y)?? void sbefore (ObjectGraphSlice ogs2) {} }); currentPath() new method on visitor Traversal state: Introduce a Path class. Path currentPath() What if multiple sources? Start traversal at one node. Path: associative structure. Can give name to objects traversed. setPathElement("xyz"); A a = (XYZ) currentPath.retrieve("xyz"); interface for Path: Path() will be automatically maintained by Visitor. Observer pattern. Path = Object List(PathElement). PathElement = String Object. contains(Object o) retrieve(String s) last() lastSourceName AspectJ: Ogss correspond to join points Visitors correspond to advice