KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > aop > introduction > InterfaceIntroduction


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.aop.introduction;
23
24 import java.lang.ref.WeakReference JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.io.StringReader JavaDoc;
27 import org.jboss.aop.Advisor;
28 import org.jboss.aop.pointcut.TypeMatcher;
29 import org.jboss.aop.pointcut.Util;
30 import org.jboss.aop.pointcut.ast.ASTStart;
31 import org.jboss.aop.pointcut.ast.ClassExpression;
32 import org.jboss.aop.pointcut.ast.TypeExpressionParser;
33 import org.jboss.aop.pointcut.ast.ParseException;
34 import javassist.CtClass;
35
36 /**
37  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
38  * @version $Revision: 44750 $
39  */

40 public class InterfaceIntroduction
41 {
42    public static class Mixin
43    {
44       protected String JavaDoc classname;
45       protected String JavaDoc[] interfaces;
46       protected String JavaDoc construction;
47       protected boolean trans;
48
49       public Mixin() {}
50
51       public Mixin(String JavaDoc classname, String JavaDoc[] interfaces, String JavaDoc construction, boolean trans)
52       {
53          this.classname = classname;
54          this.interfaces = interfaces;
55          this.construction = construction;
56          this.trans = trans;
57       }
58
59       public String JavaDoc getClassName()
60       {
61          return classname;
62       }
63
64       public String JavaDoc[] getInterfaces()
65       {
66          return interfaces;
67       }
68
69       public String JavaDoc getConstruction()
70       {
71          return construction;
72       }
73
74       public boolean isTransient()
75       {
76          return trans;
77       }
78
79       public void setClassname(String JavaDoc classname)
80       {
81          this.classname = classname;
82       }
83
84       public void setInterfaces(String JavaDoc[] interfaces)
85       {
86          this.interfaces = interfaces;
87       }
88
89       public void setConstruction(String JavaDoc construction)
90       {
91          this.construction = construction;
92       }
93
94       public void setTrans(boolean trans)
95       {
96          this.trans = trans;
97       }
98
99
100    }
101
102    protected String JavaDoc name;
103    protected ArrayList JavaDoc advisors = new ArrayList JavaDoc();
104    protected String JavaDoc[] interfaces;
105    protected ArrayList JavaDoc mixins = new ArrayList JavaDoc();
106    protected ClassExpression classExpr;
107    protected ASTStart ast;
108
109    public InterfaceIntroduction()
110    {
111
112    }
113    public InterfaceIntroduction(String JavaDoc name, String JavaDoc exp, String JavaDoc[] interfaces)
114    {
115       this.name = name;
116       this.interfaces = interfaces;
117       this.classExpr = new ClassExpression(exp);
118    }
119
120    public InterfaceIntroduction(String JavaDoc name, ASTStart ast, String JavaDoc[] interfaces)
121    {
122       this.name = name;
123       this.ast = ast;
124       this.interfaces = interfaces;
125    }
126
127    public void setClassExpression(String JavaDoc exp)
128    {
129       this.classExpr = new ClassExpression(exp);
130    }
131
132    public void setTypeExpression(String JavaDoc exp)
133    {
134       try
135       {
136          ast = new TypeExpressionParser(new StringReader JavaDoc(exp)).Start();
137       }
138       catch (ParseException e)
139       {
140          throw new RuntimeException JavaDoc(e);
141       }
142    }
143
144    public void setMixins(ArrayList JavaDoc mixins)
145    {
146       this.mixins = mixins;
147    }
148    
149    public void addMixin(Mixin mixin)
150    {
151       mixins.add(mixin);
152    }
153
154    public void setInterfaces(String JavaDoc[] interfaces)
155    {
156       this.interfaces = interfaces;
157    }
158
159    public void setName(String JavaDoc name)
160    {
161       this.name = name;
162    }
163
164    public String JavaDoc getName()
165    {
166       return name;
167    }
168
169    public String JavaDoc[] getInterfaces()
170    {
171       return interfaces;
172    }
173
174    public ArrayList JavaDoc getMixins()
175    {
176       return mixins;
177    }
178
179
180    public void addAdvisor(Advisor advisor)
181    {
182       advisors.add(new WeakReference JavaDoc(advisor));
183       advisor.addInterfaceIntroduction(this);
184    }
185
186    public void clearAdvisors()
187    {
188       for (int i = 0; i < advisors.size(); i++)
189       {
190          WeakReference JavaDoc ref = (WeakReference JavaDoc) advisors.get(i);
191          Advisor advisor = (Advisor) ref.get();
192          if (advisor != null)
193             advisor.removeInterfaceIntroduction(this);
194       }
195    }
196
197    public boolean equals(Object JavaDoc obj)
198    {
199       if (obj == this) return true;
200       if (!(obj instanceof InterfaceIntroduction)) return false;
201       return ((InterfaceIntroduction) obj).getName().equals(name);
202    }
203
204    public int hashCode()
205    {
206       return name.hashCode();
207    }
208
209    public String JavaDoc getClassExpr()
210    {
211       if (classExpr == null) return null;
212       return classExpr.getOriginal();
213    }
214
215    public ASTStart getAst()
216    {
217       return ast;
218    }
219
220    public boolean matches(Advisor advisor, CtClass clazz) throws Exception JavaDoc
221    {
222       if (classExpr != null)
223          return Util.matchesClassExpr(classExpr, clazz, advisor);
224       else
225       {
226          TypeMatcher matcher = new TypeMatcher(advisor, clazz);
227          return ((Boolean JavaDoc) ast.jjtAccept(matcher, null)).booleanValue();
228       }
229    }
230
231    public boolean matches(Advisor advisor, Class JavaDoc clazz)
232    {
233       if (classExpr != null)
234          return Util.matchesClassExpr(classExpr, clazz, advisor);
235       else
236       {
237          TypeMatcher matcher = new TypeMatcher(advisor, clazz);
238          return ((Boolean JavaDoc) ast.jjtAccept(matcher, null)).booleanValue();
239       }
240    }
241 }
242
Popular Tags