1 8 package yapbaop.demo; 9 10 import org.codehaus.aspectwerkz.proxy.Proxy; 11 import org.codehaus.aspectwerkz.intercept.Advisable; 12 import org.codehaus.aspectwerkz.intercept.Advice; 13 import org.codehaus.aspectwerkz.intercept.AfterThrowingAdvice; 14 import org.codehaus.aspectwerkz.joinpoint.JoinPoint; 15 import yapbaop.core.Yapbaop; 16 17 20 public class YapbaopDemo { 21 22 static int COUNT = 0; 23 24 String m_name; 25 26 public YapbaopDemo() { 27 m_name = "YapbaopDemo-" + COUNT++; 28 } 29 30 public void method() { 31 System.out.println(m_name + " .method"); 32 } 33 34 public void canThrow(boolean doThrow) { 35 System.out.println(m_name + " .canThrow"); 36 if (doThrow) 37 throw new RuntimeException ("Was asked to throw"); 38 } 39 40 public static void main(String args[]) throws Throwable { 41 42 System.out.println(" ( no aspect )"); 44 YapbaopDemo me0 = new YapbaopDemo(); 45 me0.method(); 46 47 System.out.println(" ( bind a new aspect )"); 48 Yapbaop.Handle handle = Yapbaop.bindAspect(DemoAspect.class, "* yapbaop.demo.YapbaopDemo.*(..)"); 49 YapbaopDemo me1 = (YapbaopDemo) Proxy.newInstance(YapbaopDemo.class); 50 me1.method(); 51 52 handle.unbind(); 53 54 System.out.println(" ( unbind it and get a new proxy YapbaopDemo-2)"); 56 YapbaopDemo me2 = (YapbaopDemo) Proxy.newInstance(YapbaopDemo.class, false, false); 57 me1.method(); me2.method(); 60 System.out.println(" ( real per instance interception, lets add an afterThrowing on YapbaopDemo-3..)"); 63 YapbaopDemo me3 = (YapbaopDemo) Proxy.newInstance(YapbaopDemo.class, false, true); 64 me3.method(); 66 ((Advisable)me3).aw_addAdvice( 68 "execution(* *.canThrow(..))", 69 new AfterThrowingAdvice() { 70 public void invoke(JoinPoint joinPoint, Throwable throwable) throws Throwable { 71 System.out.print("afterThrowing on "); 72 System.out.print(((YapbaopDemo)joinPoint.getTarget()).m_name); 73 System.out.println(" : afterThrowing on " + joinPoint.getSignature().toString()); 74 System.out.println(" exception is " + throwable.getClass().getName() 75 + " / " + throwable.getMessage()); 76 } 77 } 78 ); 79 me3.canThrow(false); System.out.println(" ( nothing happen on YapbaopDemo-2 off course)"); 81 try { 82 me2.canThrow(true); } catch (Throwable t) { 84 System.out.println("got " + t.getClass().getName() + " / " + t.getMessage()); 85 } 86 System.out.println(" ( after throwing per instance on YapbaopDemo-3 happens)"); 87 try { 88 me3.canThrow(true); } catch (Throwable t) { 90 System.out.println("got " + t.getClass().getName() + " / " + t.getMessage()); 91 } 92 93 } 94 95 } 96 | Popular Tags |