1 28 29 package com.caucho.ejb.cfg; 30 31 import com.caucho.config.ConfigException; 32 import com.caucho.util.CharBuffer; 33 import com.caucho.util.L10N; 34 35 import java.util.ArrayList ; 36 37 40 public class FunctionSignature { 41 static L10N L = new L10N(FunctionSignature.class); 42 43 private String _signature; 44 private String _name; 46 private Class []_parameterTypes; 48 private Class _returnType; 50 51 private int _index; 52 53 private String _sql; 54 55 60 public FunctionSignature(String signature) 61 throws ConfigException 62 { 63 _signature = signature; 64 65 parseSignature(); 66 } 67 68 71 public String getName() 72 { 73 return _name; 74 } 75 76 79 public String getSignature() 80 { 81 return _signature; 82 } 83 84 87 public Class []getParameterTypes() 88 { 89 return _parameterTypes; 90 } 91 92 95 public Class getReturnType() 96 { 97 return _returnType; 98 } 99 100 103 public void setSQL(String sql) 104 { 105 _sql = sql; 106 } 107 108 111 public String getSQL() 112 { 113 return _sql; 114 } 115 116 119 private void parseSignature() 120 throws ConfigException 121 { 122 _index = 0; 123 124 _returnType = parseType(skipWhitespace(read())); 125 126 CharBuffer cb = CharBuffer.allocate(); 127 int ch = skipWhitespace(read()); 128 129 for (; Character.isJavaIdentifierPart((char) ch); ch = read()) 130 cb.append((char) ch); 131 132 if (cb.length() == 0) 133 throw new ConfigException(L.l("unexpected empty function name in `{0}'", 134 _signature)); 135 136 _name = cb.toString(); 137 138 ch = skipWhitespace(ch); 139 140 if (ch != '(') 141 throw new ConfigException(L.l("function syntax is `ret-type name(arg1, ..., argn)' in `{0}'", 142 _signature)); 143 144 ArrayList <Class > argList = new ArrayList <Class >(); 145 146 ch = read(); 147 while (Character.isJavaIdentifierStart((char) (ch = skipWhitespace(ch)))) { 148 Class type = parseType(ch); 149 150 argList.add(type); 151 152 ch = skipWhitespace(read()); 153 154 if (ch == ',') 155 ch = read(); 156 } 157 158 _parameterTypes = argList.toArray(new Class [argList.size()]); 159 160 if (ch != ')') 161 throw new ConfigException(L.l("function syntax is `ret-type name(arg1, ..., argn)' in `{0}'", 162 _signature)); 163 164 ch = skipWhitespace(read()); 165 166 if (ch != -1) 167 throw new ConfigException(L.l("function syntax is `ret-type name(arg1, ..., argn)' in `{0}'", 168 _signature)); 169 } 170 171 174 private Class parseType(int ch) 175 throws ConfigException 176 { 177 CharBuffer cb = CharBuffer.allocate(); 178 179 for (; Character.isJavaIdentifierPart((char) ch); ch = read()) 180 cb.append((char) ch); 181 182 if (cb.length() == 0) 183 throw new ConfigException(L.l("unexpected empty type in `{0}'", 184 _signature)); 185 186 String className = cb.toString(); 187 188 unread(ch); 189 190 return findClass(className); 191 } 192 193 196 private Class findClass(String className) 197 throws ConfigException 198 { 199 if ("int".equals(className)) 200 return int.class; 201 else if ("boolean".equals(className)) 202 return boolean.class; 203 else if ("double".equals(className)) 204 return double.class; 205 else if ("String".equals(className)) 206 return String .class; 207 else if ("Date".equals(className)) 208 return java.util.Date .class; 209 else if ("any".equals(className)) 210 return Object .class; 211 212 throw new ConfigException(L.l("unknown type `{0}' in `{1}'", 213 className, _signature)); 214 } 215 216 219 private int skipWhitespace(int ch) 220 { 221 for (; Character.isWhitespace((char) ch); ch = read()) { 222 } 223 224 return ch; 225 } 226 227 230 private int read() 231 { 232 if (_index < _signature.length()) 233 return _signature.charAt(_index++); 234 else 235 return -1; 236 } 237 238 241 private void unread(int ch) 242 { 243 if (ch >= 0) 244 _index--; 245 } 246 247 250 public int hashCode() 251 { 252 return _name.hashCode(); 253 } 254 255 258 public boolean equals(Object o) 259 { 260 if (! (o instanceof FunctionSignature)) 261 return false; 262 263 FunctionSignature sig = (FunctionSignature) o; 264 265 if (! _name.equalsIgnoreCase(sig._name)) 266 return false; 267 268 if (_parameterTypes.length != sig._parameterTypes.length) 269 return false; 270 271 for (int i = 0; i < _parameterTypes.length; i++) 272 if (! _parameterTypes[i].equals(sig._parameterTypes[i])) 273 return false; 274 275 return true; 276 } 277 278 281 public String toString() 282 { 283 return "Function[" + _signature + "]"; 284 } 285 } 286 | Popular Tags |