KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > dynaop > bsh > BshHelper


1 package dynaop.bsh;
2
3 import java.lang.reflect.Method JavaDoc;
4
5 import org.apache.oro.text.regex.MalformedPatternException;
6
7 import dynaop.Aspects;
8 import dynaop.ClassPointcut;
9 import dynaop.Interceptor;
10 import dynaop.InterceptorFactory;
11 import dynaop.MethodPointcut;
12 import dynaop.MixinFactory;
13 import dynaop.Pointcuts;
14 import dynaop.util.Closure;
15
16 /**
17  * Convenience methods for BeanShell configuration script. Overloads pointcut
18  * and aspect methods. Allows for use of <code>Class</code> or
19  * regular expression <code>String</code> in place of
20  * <code>ClassPointcut</code> and <code>Method</code> or regular expression
21  * <code>String</code> in place of <code>MethodPointcut</code>.
22  * Matches class name or method signature against regular expression when
23  * a string is used.
24  *
25  * @author Bob Lee (crazybob@crazybob.org)
26  */

27 public class BshHelper {
28
29     Aspects aspects;
30     
31     BshHelper(Aspects aspects) {
32         this.aspects = aspects;
33     }
34
35     //********************************************************************
36
// Pointcut convenience methods.
37
//********************************************************************
38

39     public MethodPointcut declaringClass(Object JavaDoc o)
40             throws MalformedPatternException {
41         return Pointcuts.declaringClass(toClassPointcut(o));
42     }
43     
44     public MethodPointcut returnType(Object JavaDoc o)
45             throws MalformedPatternException {
46         return Pointcuts.returnType(toClassPointcut(o));
47     }
48     
49     public Object JavaDoc union(Object JavaDoc a, Object JavaDoc b) throws MalformedPatternException {
50         if (ambiguousType(a) && ambiguousType(b))
51             throw new RuntimeException JavaDoc(
52                 "Can't differentiate between method and class regex." +
53                 "Please use methodSignature() or className().");
54         if (isClassPointcut(a) && isClassPointcut(b))
55             return Pointcuts.union(toClassPointcut(a), toClassPointcut(b));
56         return Pointcuts.union(toMethodPointcut(a), toMethodPointcut(b));
57     }
58     
59     public Object JavaDoc intersection(Object JavaDoc a, Object JavaDoc b)
60             throws MalformedPatternException {
61         if (ambiguousType(a) && ambiguousType(b))
62             throw new RuntimeException JavaDoc(
63                 "Can't differentiate between method and class regex." +
64                 "Please use methodSignature() or className().");
65         if (isClassPointcut(a) && isClassPointcut(b))
66             return Pointcuts.intersection(toClassPointcut(a),
67                 toClassPointcut(b));
68         return Pointcuts.intersection(toMethodPointcut(a),
69             toMethodPointcut(b));
70     }
71     
72     boolean ambiguousType(Object JavaDoc o) {
73         return isClassPointcut(o) && isMethodPointcut(o);
74     }
75     
76     boolean isClassPointcut(Object JavaDoc o) {
77         return (o instanceof ClassPointcut ||
78                 o instanceof Class JavaDoc ||
79                 o instanceof String JavaDoc);
80     }
81     
82     boolean isMethodPointcut(Object JavaDoc o) {
83         return (o instanceof MethodPointcut ||
84                 o instanceof Method JavaDoc ||
85                 o instanceof String JavaDoc);
86     }
87     
88     //********************************************************************
89
// Mixin convenience methods.
90
//********************************************************************
91

92     public void mixin(Object JavaDoc classPointcut, Class JavaDoc[] interfaces, Class JavaDoc mixin,
93                 Closure initializer)
94             throws MalformedPatternException {
95         this.aspects.mixin(toClassPointcut(classPointcut), interfaces,
96             mixin, initializer);
97     }
98     
99     public void mixin(Object JavaDoc classPointcut, Class JavaDoc[] interfaces, Class JavaDoc mixin)
100             throws MalformedPatternException {
101         this.aspects.mixin(toClassPointcut(classPointcut), interfaces,
102             mixin, null);
103     }
104     
105     public void mixin(Object JavaDoc classPointcut, Class JavaDoc[] interfaces,
106                 MixinFactory mixinFactory)
107             throws MalformedPatternException {
108         this.aspects.mixin(toClassPointcut(classPointcut), interfaces,
109             mixinFactory);
110     }
111     
112     public void mixin(Object JavaDoc classPointcut, Class JavaDoc mixin, Closure initializer)
113             throws MalformedPatternException {
114         this.aspects.mixin(toClassPointcut(classPointcut), mixin, initializer);
115     }
116     
117     public void mixin(Object JavaDoc classPointcut, Class JavaDoc mixin)
118             throws MalformedPatternException {
119         this.aspects.mixin(toClassPointcut(classPointcut), mixin, null);
120     }
121     
122     public void interfaces(Object JavaDoc classPointcut, Class JavaDoc[] interfaces)
123             throws MalformedPatternException {
124         this.aspects.interfaces(toClassPointcut(classPointcut), interfaces);
125     }
126     
127     //********************************************************************
128
// Interceptor convenience methods.
129
//********************************************************************
130

131     public void interceptor(Object JavaDoc classPointcut, Object JavaDoc methodPointcut,
132                 Class JavaDoc interceptorClass, Closure initializer)
133             throws MalformedPatternException {
134         this.aspects.interceptor(
135             toClassPointcut(classPointcut),
136             toMethodPointcut(methodPointcut),
137             interceptorClass,
138             initializer);
139     }
140     
141     public void interceptor(Object JavaDoc classPointcut, Object JavaDoc methodPointcut,
142                 Class JavaDoc interceptorClass)
143             throws MalformedPatternException {
144         this.aspects.interceptor(
145             toClassPointcut(classPointcut),
146             toMethodPointcut(methodPointcut),
147             interceptorClass,
148             null);
149     }
150     
151     public void interceptor(Object JavaDoc classPointcut, Object JavaDoc methodPointcut,
152                 Interceptor interceptor)
153             throws MalformedPatternException {
154         this.aspects.interceptor(
155             toClassPointcut(classPointcut),
156             toMethodPointcut(methodPointcut),
157             interceptor
158         );
159     }
160     
161     public void interceptor(Object JavaDoc classPointcut, Object JavaDoc methodPointcut,
162                 InterceptorFactory interceptorFactory)
163             throws MalformedPatternException {
164         this.aspects.interceptor(
165             toClassPointcut(classPointcut),
166             toMethodPointcut(methodPointcut),
167             interceptorFactory
168         );
169     }
170     
171     //********************************************************************
172
// Pointcut converters.
173
//********************************************************************
174

175     ClassPointcut toClassPointcut(Object JavaDoc o) throws MalformedPatternException {
176         if (o instanceof ClassPointcut)
177             return (ClassPointcut) o;
178         if (o instanceof Class JavaDoc)
179             return Pointcuts.instancesOf((Class JavaDoc) o);
180         if (o instanceof String JavaDoc)
181             return Pointcuts.className((String JavaDoc) o);
182         throw new IllegalArgumentException JavaDoc("Can't convert " + o +
183                 " to ClassPointcut.");
184     }
185
186     MethodPointcut toMethodPointcut(Object JavaDoc o) throws MalformedPatternException {
187         if (o instanceof MethodPointcut)
188             return (MethodPointcut) o;
189         if (o instanceof Method JavaDoc)
190             return Pointcuts.singleton((Method JavaDoc) o);
191         if (o instanceof String JavaDoc)
192             return Pointcuts.signature((String JavaDoc) o);
193         throw new IllegalArgumentException JavaDoc("Can't convert " + o +
194                 " to MethodPointcut.");
195     }
196 }
197
Popular Tags