01: /**************************************************************************************
02: * Copyright (c) Jonas Bonér, Alexandre Vasseur. All rights reserved. *
03: * http://aspectwerkz.codehaus.org *
04: * ---------------------------------------------------------------------------------- *
05: * The software in this package is published under the terms of the LGPL license *
06: * a copy of which has been included with this distribution in the license.txt file. *
07: **************************************************************************************/package examples.proxy;
08:
09: import org.codehaus.aspectwerkz.annotation.Before;
10: import org.codehaus.aspectwerkz.joinpoint.StaticJoinPoint;
11: import org.codehaus.aspectwerkz.joinpoint.JoinPoint;
12: import org.codehaus.aspectwerkz.proxy.Proxy;
13: import org.codehaus.aspectwerkz.intercept.Advisable;
14: import org.codehaus.aspectwerkz.intercept.BeforeAdvice;
15:
16: /**
17: * @author <a href="mailto:alex AT gnilux DOT com">Alexandre Vasseur</a>
18: */
19: public class Proxy5 {
20:
21: static @interface AspectMarker {
22: }
23:
24: public String getName() {
25: return "AWProxy";
26: }
27:
28: //@AspectMarker
29: public void publicMethod() {
30: System.out.println("publicMethod");
31: protectedMethod();
32: }
33:
34: @AspectMarker
35: protected void protectedMethod() {
36: System.out.println("protectedMethod");
37: privateMethod();
38: }
39:
40: @AspectMarker
41: private void privateMethod() {
42: System.out.println("privateMethod");
43: publicFinalMethod();
44: }
45:
46: @AspectMarker
47: public final void publicFinalMethod() {
48: System.out.println("publicFinalMethod");
49: }
50:
51: public static void main(String args[]) throws Throwable {
52: System.out
53: .println("**** Use without proxy - only regular weaving may occur on non-proxy");
54: Proxy5 me = new Proxy5();
55: me.publicMethod();
56:
57: System.out
58: .println("\n**** Use with proxy - both regular weaving and proxy weaving occur");
59: // make it advisable
60: Proxy5 meP = (Proxy5) Proxy.newInstance(Proxy5.class, true,
61: true);
62: System.out.println("I am : " + meP.getName());
63: meP.publicMethod();
64:
65: System.out
66: .println("\n**** Use with proxy - adding interceptor to publicMethod()");
67: // do some per instance changes
68: ((Advisable) meP).aw_addAdvice(
69: "execution(* *.publicMethod(..))", new BeforeAdvice() {
70: public void invoke(JoinPoint jp) throws Throwable {
71: System.out.println("Intercept : "
72: + jp.getSignature());
73: }
74: });
75: meP.publicMethod();
76: }
77:
78: /**
79: * An aspect that is always there
80: */
81: public static class Aspect {
82: @Before("execution(@examples.proxy.Proxy5$AspectMarker !static * examples.proxy.Proxy5.*(..))")
83: void before(StaticJoinPoint jp) {
84: System.out
85: .println(jp.getType() + " : " + jp.getSignature());
86: }
87: }
88: }
|