310 likes | 502 Views
Kimaya Kamat. Exception Handling Framework in AOP . Outline. Background Exception Handling Exception Handling with AOP Bugs in AOP Exception Handling. Exceptions. A disruption in the normal flow of the program An unwanted condition Unchecked exceptions Checked exceptions.
E N D
Kimaya Kamat Exception Handling Framework in AOP
Outline • Background • Exception Handling • Exception Handling with AOP • Bugs in AOP Exception Handling
Exceptions.. • A disruption in the normal flow of the program • An unwanted condition • Unchecked exceptions • Checked exceptions
Java code snippet with assertions.. public class Stack { public Object [] array; private inttopOfStack = 0; public void push(Object obj) { assert (0 <= topOfStack)&& (topOfStack < array.length); this.array[topOfStack++] = obj; assert (0 <= topOfStack)&& (topOfStack < array.length); } public Object pop() { assert (0 <= topOfStack)&& (topOfStack < array.length); Object result = this.array[topOfStack-1]; this.array[topOfStack-1] = null; this.topOfStack- - ; assert (0 <= topOfStack)&& (topOfStack < array.length); return result; } public () Object peek(){ assert (0 <= topOfStack)&& (topOfStack < array.length); Object result = this.array[topOfStack-1]; assert (0 <= topOfStack)&& (topOfStack < array.length); return result; } ... }
The Contract Enforcement Pattern • Also called the Design by Contract • Add contracts to an aspect • Contract checking done at runtime • Contracts support inheritance
Contracts • Set of mutual obligations between classes and their clients • Preconditions • Post-conditions • Invariants
Contract 4 Java • @Requires • @Ensures • @Invariant
Contract code @Invariant("size() >= 0") interface Stack<T> { public int size(); @Requires("size() >= 1") public T peek(); @Requires("size() >= 1") @Ensures({ "size() == old(size()) - 1", "result == old(peek())" }) public T pop(); @Ensures({ "size() == old(size()) + 1", "peek() == old(obj)" }) public void push(T obj); }
Basic Idea of AOP • Create modularized code • Avoid tangled code • Promote reuse • Ease of maintenance .....All this applied to exception handling!
AspectJ • Exception handling: • After Throwing • After Returning
AOP exception handling constructs • Some are taken from Java: • Try, catch, finally • Throws • Some are it’s own! • Declare soft • After and After Throwing advice
Bug Patterns in AOP • When aspects are used to catch Exceptions • When aspects throw exceptions
Fragile Catch • Happens due to minor spelling mistakes
Fragile Catch.. • Or even differing return types:
Residual Catch.. • Before aspectiz-ing:
Throw without catch • What if log(ex) throws an exception when the file gets too large!!
Path Dependent Throw • A condition where an exception should be thrown when methodC() is called through the following path: • methodA() methodC() • Happens due to extensive use of • within, withincode, cflow, cflowbelow
Exception Handling in AOP • Advantages: • Exception handling code concentrated in the aspects • Clear separation of business code and exception handling • Reusable code • Free of duplication
Exception Handling in AOP • Disadvantages • Knowledge of aspects required • No clear connection between the aspects and classes • Environment support required • Design needs to be documented precisely
References: • Lippert, Martin, and Cristina Videira Lopes. A study on exception detection and handling using aspect-oriented programming. Limerick: ACM, 2000. Print. • Coelho, Roberta , Awais Rashid, James Noble, and Carlos Lucena. “A Catalogue of Bug Patterns for Exception Handling in Aspect-Oriented Programs." ACM 978 (2008): 1-13. Print. • Henrique Rebelo, Roberta Coelho, AlexandreMota.”The Contract Enforcement Aspect Pattern” • http://jcontractor.sourceforge.net/