1 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 ; 41 import java.util.ArrayList ; 42 43 public class MethodSignature { 44 private static L10N L = new L10N(MethodSignature.class); 45 46 private static IntMap _methodElements; 47 48 private String _ejbName; 49 private EjbBean _bean; 50 51 private String _methodName; 52 private String _methodIntf; 53 54 private Object _value; 55 56 private ArrayList <String > _paramTypes; 57 58 public MethodSignature() 59 { 60 } 61 62 public void setEJBName(String ejbName) 63 { 64 _ejbName = ejbName; 65 } 66 67 public String getEJBName() 68 { 69 if (_bean != null) 70 return _bean.getEJBName(); 71 else 72 return _ejbName; 73 } 74 75 public void setMethodName(String name) 76 throws ConfigException 77 { 78 setName(name); 79 } 80 81 public String getName() 82 { 83 return _methodName; 84 } 85 86 public void setName(String 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 []params = sig.getParameterTypes(); 101 if (params != null) { 102 _paramTypes = new ArrayList <String >(); 103 104 for (int i = 0; i < params.length; i++) 105 _paramTypes.add(params[i]); 106 } 107 } 108 109 public void addText(String text) 110 throws ConfigException 111 { 112 setName(text); 113 } 114 115 118 public void addParam(String typeName) 119 { 120 if (_paramTypes == null) 121 _paramTypes = new ArrayList <String >(); 122 123 _paramTypes.add(typeName); 124 } 125 126 129 public MethodParams createMethodParams() 130 { 131 return new MethodParams(); 132 } 133 134 139 public void setHasParams() 140 { 141 if (_paramTypes == null) 142 _paramTypes = new ArrayList <String >(); 143 } 144 145 148 public void setMethodIntf(String 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 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 methodName, Class []params, String 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 name = params[i].getName(); 211 String param = (String ) _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 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 methodName, JClass []params, String 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 name = params[i].getName(); 246 String param = (String ) _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 value) 256 { 257 _value = value; 258 } 259 260 Object getValue() 261 { 262 return _value; 263 } 264 265 public int hashCode() 266 { 267 return _methodName.hashCode(); 268 } 269 270 public boolean equals(Object 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 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 toString() 317 { 318 return ("MethodSignature[" + toSignatureString() + "]"); 319 } 320 321 public class MethodParams { 322 public void addMethodParam(String value) 323 { 324 addParam(value); 325 } 326 } 327 } 328 | Popular Tags |