KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > ejb > cfg > MethodSignature


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.ejb.cfg;
31
32 import com.caucho.bytecode.JClass;
33 import com.caucho.bytecode.JMethod;
34 import com.caucho.config.ConfigException;
35 import com.caucho.config.types.Signature;
36 import com.caucho.util.CharBuffer;
37 import com.caucho.util.IntMap;
38 import com.caucho.util.L10N;
39
40 import java.lang.reflect.Method JavaDoc;
41 import java.util.ArrayList JavaDoc;
42
43 public class MethodSignature {
44   private static L10N L = new L10N(MethodSignature.class);
45
46   private static IntMap _methodElements;
47
48   private String JavaDoc _ejbName;
49   private EjbBean _bean;
50   
51   private String JavaDoc _methodName;
52   private String JavaDoc _methodIntf;
53
54   private Object JavaDoc _value;
55
56   private ArrayList JavaDoc<String JavaDoc> _paramTypes;
57   
58   public MethodSignature()
59   {
60   }
61
62   public void setEJBName(String JavaDoc ejbName)
63   {
64     _ejbName = ejbName;
65   }
66
67   public String JavaDoc getEJBName()
68   {
69     if (_bean != null)
70       return _bean.getEJBName();
71     else
72       return _ejbName;
73   }
74
75   public void setMethodName(String JavaDoc name)
76     throws ConfigException
77   {
78     setName(name);
79   }
80   
81   public String JavaDoc getName()
82   {
83     return _methodName;
84   }
85
86   public void setName(String JavaDoc methodName)
87     throws ConfigException
88   {
89     if (methodName.indexOf('(') < 0) {
90       _methodName = methodName;
91       return;
92     }
93     
94     Signature sig = new Signature();
95     sig.addText(methodName);
96     sig.init();
97
98     _methodName = sig.getName();
99
100     String JavaDoc []params = sig.getParameterTypes();
101     if (params != null) {
102       _paramTypes = new ArrayList JavaDoc<String JavaDoc>();
103
104       for (int i = 0; i < params.length; i++)
105         _paramTypes.add(params[i]);
106     }
107   }
108
109   public void addText(String JavaDoc text)
110     throws ConfigException
111   {
112     setName(text);
113   }
114
115   /**
116    * Adds a method parameter.
117    */

118   public void addParam(String JavaDoc typeName)
119   {
120     if (_paramTypes == null)
121       _paramTypes = new ArrayList JavaDoc<String JavaDoc>();
122
123     _paramTypes.add(typeName);
124   }
125
126   /**
127    * Adds a method parameter.
128    */

129   public MethodParams createMethodParams()
130   {
131     return new MethodParams();
132   }
133
134   /**
135    * Sets the parameters to zero to distinguish between
136    * methods with zero arguments and methods which don't
137    * specify the requirements.
138    */

139   public void setHasParams()
140   {
141     if (_paramTypes == null)
142       _paramTypes = new ArrayList JavaDoc<String JavaDoc>();
143   }
144   
145   /**
146    * Sets the method interface.
147    */

148   public void setMethodIntf(String JavaDoc intf)
149   {
150     _methodIntf = intf;
151   }
152
153   boolean isHome()
154   {
155     return _methodIntf == null || _methodIntf.equals("Home");
156   }
157
158   boolean isRemote()
159   {
160     return _methodIntf == null || _methodIntf.equals("Remote");
161   }
162
163   boolean isLocalHome()
164   {
165     return _methodIntf == null || _methodIntf.equals("LocalHome");
166   }
167
168   boolean isLocal()
169   {
170     return _methodIntf == null || _methodIntf.equals("Local");
171   }
172
173   int getCost()
174   {
175     int cost = _methodIntf == null ? 0 : 1;
176
177     if (_methodName.equals("*"))
178       return cost;
179     else if (_paramTypes == null)
180       return 2 + cost;
181     else
182       return 4 + cost;
183   }
184
185   public boolean isMatch(Method method, String JavaDoc intf)
186   {
187     if (method == null)
188       return _methodName.equals("*");
189     else
190       return isMatch(method.getName(), method.getParameterTypes(), intf);
191   }
192   
193   public boolean isMatch(String JavaDoc methodName, Class JavaDoc []params, String JavaDoc intf)
194   {
195     if (_methodIntf != null && ! _methodIntf.equals(intf))
196       return false;
197     else if (_methodName == null)
198       return false;
199     else if (_methodName.equals("*"))
200       return true;
201     else if (! _methodName.equals(methodName))
202       return false;
203     else if (_paramTypes == null)
204       return true;
205
206     if (params.length != _paramTypes.size())
207       return false;
208
209     for (int i = 0; i < params.length; i++) {
210       String JavaDoc name = params[i].getName();
211       String JavaDoc param = (String JavaDoc) _paramTypes.get(i);
212
213       if (! name.equals(param) && ! name.endsWith("." + param))
214         return false;
215     }
216
217     return true;
218   }
219
220   public boolean isMatch(JMethod method, String JavaDoc intf)
221   {
222     if (method == null)
223       return _methodName.equals("*");
224     else
225       return isMatch(method.getName(), method.getParameterTypes(), intf);
226   }
227   
228   public boolean isMatch(String JavaDoc methodName, JClass []params, String JavaDoc intf)
229   {
230     if (_methodIntf != null && ! _methodIntf.equals(intf))
231       return false;
232     else if (_methodName == null)
233       return false;
234     else if (_methodName.equals("*"))
235       return true;
236     else if (! _methodName.equals(methodName))
237       return false;
238     else if (_paramTypes == null)
239       return true;
240
241     if (params.length != _paramTypes.size())
242       return false;
243
244     for (int i = 0; i < params.length; i++) {
245       String JavaDoc name = params[i].getName();
246       String JavaDoc param = (String JavaDoc) _paramTypes.get(i);
247
248       if (! name.equals(param) && ! name.endsWith("." + param))
249         return false;
250     }
251
252     return true;
253   }
254
255   void setValue(Object JavaDoc value)
256   {
257     _value = value;
258   }
259
260   Object JavaDoc getValue()
261   {
262     return _value;
263   }
264
265   public int hashCode()
266   {
267     return _methodName.hashCode();
268   }
269
270   public boolean equals(Object JavaDoc o)
271   {
272     if (! (o instanceof MethodSignature))
273       return false;
274
275     MethodSignature cfg = (MethodSignature) o;
276
277     if (! _methodName.equals(cfg._methodName))
278       return false;
279
280     if (_paramTypes == null || cfg._paramTypes == null)
281       return _paramTypes == cfg._paramTypes;
282
283     if (_paramTypes.size() != cfg._paramTypes.size())
284       return false;
285
286     for (int i = 0; i < _paramTypes.size(); i++)
287       if (! _paramTypes.get(i).equals(cfg._paramTypes.get(i)))
288         return false;
289
290     if (_methodIntf == cfg._methodIntf)
291       return true;
292
293     else if (_methodIntf == null || cfg._methodIntf == null)
294       return false;
295
296     else
297       return _methodIntf.equals(cfg._methodIntf);
298   }
299
300   public String JavaDoc toSignatureString()
301   {
302     CharBuffer cb = new CharBuffer();
303
304     cb.append(_methodName);
305     cb.append("(");
306     for (int i = 0; _paramTypes != null && i < _paramTypes.size(); i++) {
307       if (i != 0)
308         cb.append(", ");
309       cb.append(_paramTypes.get(i));
310     }
311     cb.append(")");
312
313     return cb.toString();
314   }
315
316   public String JavaDoc toString()
317   {
318     return ("MethodSignature[" + toSignatureString() + "]");
319   }
320
321   public class MethodParams {
322     public void addMethodParam(String JavaDoc value)
323     {
324       addParam(value);
325     }
326   }
327 }
328
Popular Tags