1 29 30 package com.caucho.java; 31 32 import com.caucho.java.gen.DependencyComponent; 33 import com.caucho.java.gen.GenClass; 34 import com.caucho.loader.SimpleLoader; 35 import com.caucho.server.util.CauchoSystem; 36 import com.caucho.util.CharBuffer; 37 import com.caucho.vfs.IOExceptionWrapper; 38 import com.caucho.vfs.Path; 39 import com.caucho.vfs.PersistentDependency; 40 import com.caucho.vfs.WriteStream; 41 42 import java.io.IOException ; 43 import java.lang.reflect.Method ; 44 import java.util.ArrayList ; 45 46 49 public abstract class AbstractGenerator { 50 private String _fullClassName; 52 private String _packageName; 54 private String _className; 56 57 private ClassLoader _parentLoader; 59 private ClassLoader _loader; 61 62 private WriteStream _os; 64 protected JavaWriter _out; 66 67 private Path _searchPath; 69 70 private Path _workPath; 72 73 private GenClass _genClass; 74 75 private String _initMethod = "_caucho_init"; 76 private String _isModifiedMethod = "_caucho_is_modified"; 77 78 81 public void setSearchPath(Path path) 82 { 83 _searchPath = path; 84 } 85 86 public Path getSearchPath() 87 { 88 return _searchPath; 89 } 90 91 94 public void setFullClassName(String fullClassName) 95 { 96 CharBuffer cb = CharBuffer.allocate(); 97 for (int i = 0; i < fullClassName.length(); i++) { 98 char ch = fullClassName.charAt(i); 99 100 if (ch == '.' || ch == '/') 101 cb.append('.'); 102 else if (Character.isJavaIdentifierPart(ch)) 103 cb.append(ch); 104 else 105 cb.append('_'); 106 } 107 108 _fullClassName = cb.close(); 109 110 int p = _fullClassName.lastIndexOf('.'); 111 if (p > 0) { 112 _packageName = _fullClassName.substring(0, p); 113 _className = _fullClassName.substring(p + 1); 114 } 115 else { 116 _packageName = ""; 117 _className = _fullClassName; 118 } 119 } 120 121 124 public String getFullClassName() 125 { 126 if (_genClass != null) 127 return _genClass.getFullClassName(); 128 else 129 return _fullClassName; 130 } 131 132 135 public String getPackageName() 136 { 137 if (_genClass != null) 138 return _genClass.getPackageName(); 139 else 140 return _packageName; 141 } 142 143 146 public String getClassName() 147 { 148 if (_genClass != null) 149 return _genClass.getClassName(); 150 else 151 return _className; 152 } 153 154 157 public void setGenClass(GenClass genClass) 158 { 159 _genClass = genClass; 160 } 161 162 165 public GenClass getGenClass() 166 { 167 return _genClass; 168 } 169 170 175 public void setParentLoader(ClassLoader loader) 176 { 177 _parentLoader = loader; 178 } 179 180 185 public void setLoader(ClassLoader loader) 186 { 187 _loader = loader; 188 } 189 190 195 public ClassLoader getParentLoader() 196 { 197 if (_parentLoader == null) 198 _parentLoader = Thread.currentThread().getContextClassLoader(); 199 200 return _parentLoader; 201 } 202 203 206 public void setClassDir(Path workPath) 207 { 208 _workPath = workPath; 209 } 210 211 214 public Path getClassDir() 215 { 216 if (_workPath == null) 217 return CauchoSystem.getWorkPath(); 218 else 219 return _workPath; 220 } 221 222 227 public Class preload() 228 throws IOException 229 { 230 return loadClass(true); 231 } 232 233 236 public void generate() 237 throws Exception 238 { 239 String className = getFullClassName(); 240 String javaPathName = className.replace('.', '/') + ".java"; 241 String classPathName = className.replace('.', '/') + ".class"; 242 243 Path javaPath = getClassDir().lookup(javaPathName); 244 Path classPath = getClassDir().lookup(classPathName); 245 246 try { 247 classPath.remove(); 248 } catch (IOException e) { 249 } 250 251 javaPath.getParent().mkdirs(); 252 253 _os = javaPath.openWrite(); 254 _out = new JavaWriter(_os); 255 256 if (_genClass != null) 257 _genClass.generate(_out); 258 else 259 generateJava(); 260 261 _os.close(); 262 } 263 264 267 public Class compile() 268 throws Exception 269 { 270 compileJava(); 271 272 return loadClass(false); 273 } 274 275 278 public void generateJava() 279 throws Exception 280 { 281 } 282 283 288 protected void printDependList(ArrayList <PersistentDependency> depends) 289 throws IOException 290 { 291 DependencyComponent depend = new DependencyComponent(); 292 depend.setSearchPath(_searchPath); 293 294 for (int i = 0; i < depends.size(); i++) 295 depend.addDependency(depends.get(i)); 296 297 depend.generate(getOut()); 298 } 299 300 303 protected void printVersionChange() 304 throws IOException 305 { 306 println("if (com.caucho.server.util.CauchoSystem.getVersionId() != " + 307 CauchoSystem.getVersionId() + ")"); 308 println(" return true;"); 309 } 310 311 316 public void printMethodHeader(Method method) 317 throws IOException 318 { 319 printMethodHeader(method.getName(), method.getParameterTypes(), 320 method.getReturnType(), method.getExceptionTypes()); 321 } 322 323 329 public void printMethodHeader(String name, Method method) 330 throws IOException 331 { 332 printMethodHeader(name, method.getParameterTypes(), 333 method.getReturnType(), method.getExceptionTypes()); 334 } 335 336 344 public void printMethodHeader(String methodName, Class []parameters, 345 Class returnType, Class []exn) 346 throws IOException 347 { 348 println(); 349 print("public "); 350 printClass(returnType); 351 print(" "); 352 print(methodName); 353 print("("); 354 355 for (int i = 0; i < parameters.length; i++) { 356 if (i != 0) 357 print(", "); 358 359 printClass(parameters[i]); 360 print(" a" + i); 361 } 362 println(")"); 363 364 if (exn != null && exn.length > 0) { 365 print(" throws "); 366 printClass(exn[0]); 367 368 for (int i = 1; i < exn.length; i++) { 369 print(", "); 370 printClass(exn[i]); 371 } 372 println(); 373 } 374 } 375 376 379 public void printClass(Class cl) 380 throws IOException 381 { 382 if (! cl.isArray()) 383 print(cl.getName().replace('$', '.')); 384 else { 385 printClass(cl.getComponentType()); 386 print("[]"); 387 } 388 } 389 390 393 public void compileJava() 394 throws IOException , ClassNotFoundException 395 { 396 JavaCompiler compiler = getCompiler(); 397 398 compiler.compile(getFullClassName().replace('.', '/') + ".java", null); 399 } 400 401 public JavaCompiler getCompiler() 402 { 403 JavaCompiler compiler = JavaCompiler.create(getParentLoader()); 404 405 compiler.setClassLoader(getParentLoader()); 406 compiler.setClassDir(getClassDir()); 407 408 return compiler; 409 } 410 411 415 public Class loadClass(boolean preload) 416 throws IOException 417 { 418 return loadClass(getFullClassName(), preload); 419 } 420 421 425 public Class loadClass(String fullClassName, boolean preload) 426 throws IOException 427 { 428 try { 429 ClassLoader loader; 430 431 if (! preload && _loader != null) 432 loader = _loader; 433 else { 434 loader = SimpleLoader.create(getParentLoader(), 435 getClassDir(), 436 fullClassName); 437 } 438 439 Class cl = CauchoSystem.loadClass(fullClassName, false, loader); 440 441 442 if (cl == null) 443 return null; 444 if (! preload) 445 return cl; 446 447 Method method = cl.getMethod(_initMethod, new Class [] { Path.class }); 448 method.invoke(null, new Object [] { getSearchPath() }); 449 450 method = cl.getMethod(_isModifiedMethod, new Class [0]); 451 Boolean value = (Boolean ) method.invoke(null, new Object [] {}); 452 453 if (value.booleanValue()) 454 return null; 455 456 if (_loader != null) 457 loader = _loader; 458 else { 459 loader = SimpleLoader.create(getParentLoader(), 460 getClassDir(), 461 fullClassName); 462 } 463 464 return CauchoSystem.loadClass(fullClassName, false, loader); 465 } catch (Throwable e) { 466 if (! preload) 467 throw new IOExceptionWrapper(e); 468 else 469 return null; 470 } 471 } 472 473 476 public JavaWriter getOut() 477 { 478 return _out; 479 } 480 481 484 public void pushDepth() 485 throws IOException 486 { 487 _out.pushDepth(); 488 } 489 490 493 public void popDepth() 494 throws IOException 495 { 496 _out.popDepth(); 497 } 498 499 502 public void print(int ch) 503 throws IOException 504 { 505 _out.print(ch); 506 } 507 508 511 public void print(char ch) 512 throws IOException 513 { 514 _out.print(ch); 515 } 516 517 520 public void print(String s) 521 throws IOException 522 { 523 _out.print(s); 524 } 525 526 529 public void printStr(String s) 530 throws IOException 531 { 532 int len = s.length(); 533 for (int i = 0; i < len; i++) { 534 char ch = s.charAt(i); 535 536 switch (ch) { 537 case '\\': 538 _out.print("\\\\"); 539 break; 540 case '\n': 541 _out.print("\\n"); 542 break; 543 case '\r': 544 _out.print("\\r"); 545 break; 546 case '"': 547 _out.print("\\\""); 548 break; 549 default: 550 _out.print(ch); 551 } 552 } 553 } 554 555 558 public void println() 559 throws IOException 560 { 561 _out.println(); 562 } 563 564 567 public void println(String s) 568 throws IOException 569 { 570 _out.println(s); 571 } 572 } 573 | Popular Tags |