KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > config > types > SignaturePattern


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  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.config.types;
30
31 import com.caucho.config.ConfigException;
32 import com.caucho.util.CharBuffer;
33 import com.caucho.util.L10N;
34
35 import java.lang.reflect.Method JavaDoc;
36 import java.util.ArrayList JavaDoc;
37
38 public class SignaturePattern {
39   private static L10N L = new L10N(SignaturePattern.class);
40
41   private String JavaDoc _methodName;
42
43   private ArrayList JavaDoc<String JavaDoc> _paramTypes;
44
45   public void addText(String JavaDoc methodName)
46     throws ConfigException
47   {
48     if (methodName.indexOf('(') < 0) {
49       _methodName = methodName;
50       return;
51     }
52     
53     Signature sig = new Signature();
54     sig.addText(methodName);
55     sig.init();
56
57     _methodName = sig.getName();
58
59     String JavaDoc []params = sig.getParameterTypes();
60     if (params != null) {
61       _paramTypes = new ArrayList JavaDoc<String JavaDoc>();
62
63       for (int i = 0; i < params.length; i++)
64         _paramTypes.add(params[i]);
65     }
66   }
67
68   /**
69    * Adds a method parameter.
70    */

71   public void addParam(String JavaDoc typeName)
72   {
73     if (_paramTypes == null)
74       _paramTypes = new ArrayList JavaDoc<String JavaDoc>();
75
76     _paramTypes.add(typeName);
77   }
78
79   /**
80    * Sets the parameters to zero to distinguish between
81    * methods with zero arguments and methods which don't
82    * specify the requirements.
83    */

84   public void setHasParams()
85   {
86     if (_paramTypes == null)
87       _paramTypes = new ArrayList JavaDoc<String JavaDoc>();
88   }
89
90   public boolean isMatch(Method JavaDoc method)
91   {
92     return isMatch(method.getName(), method.getParameterTypes());
93   }
94   
95   public boolean isMatch(String JavaDoc methodName, Class JavaDoc []params)
96   {
97     if (_methodName == null)
98       return false;
99     else if (_methodName.equals("*"))
100       return true;
101     else if (! _methodName.equals(methodName))
102       return false;
103     else if (_paramTypes == null)
104       return true;
105
106     if (params.length != _paramTypes.size())
107       return false;
108
109     for (int i = 0; i < params.length; i++) {
110       String JavaDoc name = params[i].getName();
111       String JavaDoc param = (String JavaDoc) _paramTypes.get(i);
112
113       if (! name.equals(param) && ! name.endsWith("." + param))
114         return false;
115     }
116
117     return true;
118   }
119
120   public int hashCode()
121   {
122     return _methodName.hashCode();
123   }
124
125   public boolean equals(Object JavaDoc o)
126   {
127     if (! (o instanceof SignaturePattern))
128       return false;
129
130     SignaturePattern cfg = (SignaturePattern) o;
131
132     if (! _methodName.equals(cfg._methodName))
133       return false;
134
135     if (_paramTypes == null || cfg._paramTypes == null)
136       return _paramTypes == cfg._paramTypes;
137
138     if (_paramTypes.size() != cfg._paramTypes.size())
139       return false;
140
141     for (int i = 0; i < _paramTypes.size(); i++)
142       if (! _paramTypes.get(i).equals(cfg._paramTypes.get(i)))
143         return false;
144
145     return true;
146   }
147
148   public String JavaDoc toString()
149   {
150     CharBuffer cb = new CharBuffer();
151
152     cb.append("SignaturePattern[");
153     cb.append(_methodName);
154     cb.append("(");
155     for (int i = 0; _paramTypes != null && i < _paramTypes.size(); i++) {
156       if (i != 0)
157         cb.append(", ");
158       cb.append(_paramTypes.get(i));
159     }
160     cb.append(")]");
161     return cb.toString();
162   }
163 }
164
Popular Tags