1 28 29 package com.caucho.ejb.hessian; 30 31 import com.caucho.java.AbstractGenerator; 32 import com.caucho.loader.DynamicClassLoader; 33 import com.caucho.util.CharBuffer; 34 import com.caucho.util.IntMap; 35 import com.caucho.vfs.Path; 36 37 import java.io.IOException ; 38 import java.lang.reflect.Method ; 39 import java.util.ArrayList ; 40 41 44 abstract class MarshalGenerator extends AbstractGenerator { 45 static IntMap immutableClasses; 48 49 protected static Class readerClass; 50 protected static Class inputStreamClass; 51 protected static Class remoteClass; 52 protected static Class nodeClass; 53 54 static { 55 readerClass = java.io.Reader .class; 56 inputStreamClass = java.io.InputStream .class; 57 remoteClass = java.rmi.Remote .class; 58 nodeClass = org.w3c.dom.Node .class; 59 } 60 61 protected Class _cl; 62 String objClass; 63 String fullName; 64 String pkg; 65 String className; 66 67 protected int unique; 68 69 protected ArrayList marshallClasses; 70 protected ArrayList unmarshallClasses; 71 72 protected ArrayList marshallArrays; 73 protected ArrayList unmarshallArrays; 74 75 82 Path initClassNames(Class beanClass, String suffix) 83 throws Exception 84 { 85 ClassLoader parentLoader = getParentLoader(); 86 87 if (parentLoader instanceof DynamicClassLoader) { 88 DynamicClassLoader dcl = (DynamicClassLoader) parentLoader; 89 90 dcl.make(); 91 } 92 93 Path workPath = getClassDir(); 94 95 _cl = beanClass; 96 97 fullName = _cl.getName() + suffix; 98 99 objClass = _cl.getName(); 100 int p = objClass.lastIndexOf('.'); 101 if (p > 0) 102 objClass = objClass.substring(p + 1); 103 104 p = fullName.lastIndexOf('.'); 105 if (p > 0) { 106 pkg = fullName.substring(0, p); 107 className = fullName.substring(p + 1); 108 } 109 else 110 className = fullName; 111 112 Path path = workPath.lookup(fullName.replace('.', '/') + ".java"); 113 path.getParent().mkdirs(); 114 115 return path; 116 } 117 118 128 protected String mangleMethodName(String name, Method method, boolean isFull) 129 { 130 CharBuffer cb = CharBuffer.allocate(); 131 132 cb.append(name); 133 134 Class []params = method.getParameterTypes(); 135 for (int i = 0; i < params.length; i++) { 136 cb.append('_'); 137 mangleClass(cb, params[i], isFull); 138 } 139 140 return cb.close(); 141 } 142 143 153 protected String mangleMethodName(String name, Class []param, boolean isFull) 154 { 155 CharBuffer cb = CharBuffer.allocate(); 156 157 cb.append(name); 158 159 for (int i = 0; i < param.length; i++) { 160 cb.append('_'); 161 mangleClass(cb, param[i], isFull); 162 } 163 164 return cb.close(); 165 } 166 167 170 private void mangleClass(CharBuffer cb, Class cl, boolean isFull) 171 { 172 String name = cl.getName(); 173 174 if (name.equals("boolean")) 175 cb.append("boolean"); 176 else if (name.equals("int") || 177 name.equals("short") || 178 name.equals("byte")) 179 cb.append("int"); 180 else if (name.equals("long")) 181 cb.append("long"); 182 else if (name.equals("double") || name.equals("float")) 183 cb.append("double"); 184 else if (name.equals("java.lang.String") || 185 name.equals("com.caucho.util.CharBuffer") || 186 name.equals("char") || 187 name.equals("java.io.Reader")) 188 cb.append("string"); 189 else if (name.equals("java.util.Date") || 190 name.equals("com.caucho.util.QDate")) 191 cb.append("date"); 192 else if (inputStreamClass.isAssignableFrom(cl) || 193 name.equals("[B")) 194 cb.append("binary"); 195 else if (cl.isArray()) { 196 cb.append("["); 197 mangleClass(cb, cl.getComponentType(), isFull); 198 } 199 else if (name.equals("org.w3c.dom.Node") || 200 name.equals("org.w3c.dom.Element") || 201 name.equals("org.w3c.dom.Document")) 202 cb.append("xml"); 203 else if (isFull) 204 cb.append(name); 205 else { 206 int p = name.lastIndexOf('.'); 207 if (p > 0) 208 cb.append(name.substring(p + 1)); 209 else 210 cb.append(name); 211 } 212 } 213 214 220 protected void printMethodDeclaration(String name, Method method) 221 throws IOException 222 { 223 Class ret = method.getReturnType(); 224 Class []params = method.getParameterTypes(); 225 226 Class []exns = method.getExceptionTypes(); 227 228 println(); 229 print("public "); 230 printClass(ret); 231 print(" " + name + "("); 232 for (int i = 0; i < params.length; i++) { 233 if (i != 0) 234 print(", "); 235 printClass(params[i]); 236 print(" _arg" + i); 237 } 238 println(")"); 239 if (exns.length > 0) 240 print(" throws "); 241 for (int i = 0; i < exns.length; i++) { 242 if (i != 0) 243 print(", "); 244 printClass(exns[i]); 245 } 246 if (exns.length > 0) 247 println(); 248 } 249 250 257 protected void printUnmarshalType(Class cl) 258 throws IOException 259 { 260 String name = cl.getName(); 261 262 if (cl.equals(boolean.class)) { 263 println("in.readBoolean();"); 264 } 265 else if (cl.equals(int.class)) { 266 println("in.readInt();"); 267 } 268 else if (cl.equals(short.class) || 269 cl.equals(char.class) || 270 cl.equals(byte.class)) { 271 println("(" + name + ") in.readInt();"); 272 } 273 else if (cl.equals(long.class)) { 274 println("in.readLong();"); 275 } 276 else if (cl.equals(double.class)) { 277 println("in.readDouble();"); 278 } 279 else if (cl.equals(float.class)) { 280 println("(" + name + ") in.readDouble();"); 281 } 282 else if (cl.equals(String .class)) 283 println("in.readString();"); 284 else if (cl.equals(java.util.Date .class)) { 285 println("new java.util.Date(in.readUTCDate());"); 286 } 287 else if (cl.equals(byte[].class)) { 288 println("in.readBytes();"); 289 } 290 else if (org.w3c.dom.Node .class.isAssignableFrom(cl)) { 291 println("in.readNode();"); 292 } 293 else if (cl.equals(java.lang.Object .class)) { 294 println("in.readObject();"); 295 } 296 else { 297 print("("); 298 printClass(cl); 299 print(") in.readObject("); 300 printClass(cl); 301 println(".class);"); 302 } 303 } 304 305 protected void printMarshalType(Class cl, String var) 306 throws IOException 307 { 308 String name = cl.getName(); 309 310 if (cl.equals(void.class)) { 311 println("out.writeNull();"); 312 } 313 else if (cl.equals(boolean.class)) { 314 println("out.writeBoolean(" + var + ");"); 315 } 316 else if (cl.equals(int.class) || 317 cl.equals(short.class) || 318 cl.equals(char.class) || 319 cl.equals(byte.class)) { 320 println("out.writeInt(" + var + ");"); 321 } 322 else if (cl.equals(long.class)) { 323 println("out.writeLong(" + var + ");"); 324 } 325 else if (cl.equals(double.class) || cl.equals(float.class)) { 326 println("out.writeDouble(" + var + ");"); 327 } 328 else if (cl.equals(String .class)) { 329 println("out.writeString(" + var + ");"); 330 } 331 else if (cl.equals(java.util.Date .class)) { 332 println("out.writeUTCDate(" + var + " == null ? 0 : " + var + ".getTime());"); 333 } 334 else if (org.w3c.dom.Node .class.isAssignableFrom(cl)) { 335 println("out.writeXml(" + var + ");"); 336 } 337 else if (cl.equals(byte[].class)) { 338 println("out.writeBytes(" + var + ");"); 339 } 340 else { 341 println("out.writeObject(" + var + ");"); 342 } 343 } 344 345 352 boolean needsSerialization(Class cl) 353 { 354 if (cl.isPrimitive()) 355 return false; 356 else 357 return immutableClasses.get(cl) < 0; 358 } 359 360 public void printClass(Class cl) 361 throws IOException 362 { 363 if (! cl.isArray()) 364 print(cl.getName()); 365 else { 366 printClass(cl.getComponentType()); 367 print("[]"); 368 } 369 } 370 371 protected void printNewArray(Class cl) 372 throws IOException 373 { 374 if (! cl.isArray()) { 375 print(cl.getName()); 376 print("[length]"); 377 } 378 else { 379 printNewArray(cl.getComponentType()); 380 print("[]"); 381 } 382 } 383 384 static { 385 immutableClasses = new IntMap(); 386 immutableClasses.put(String .class, 1); 387 immutableClasses.put(Byte .class, 1); 388 immutableClasses.put(Character .class, 1); 389 immutableClasses.put(Short .class, 1); 390 immutableClasses.put(Integer .class, 1); 391 immutableClasses.put(Long .class, 1); 392 immutableClasses.put(Float .class, 1); 393 immutableClasses.put(Double .class, 1); 394 immutableClasses.put(Class .class, 1); 395 } 396 } 397 | Popular Tags |