1 29 30 package com.caucho.amber.field; 31 32 import com.caucho.amber.expr.AmberExpr; 33 import com.caucho.amber.expr.PathExpr; 34 import com.caucho.amber.manager.AmberConnection; 35 import com.caucho.amber.manager.AmberPersistenceUnit; 36 import com.caucho.amber.query.QueryParser; 37 import com.caucho.amber.table.Table; 38 import com.caucho.amber.type.AbstractStatefulType; 39 import com.caucho.amber.type.RelatedType; 40 import com.caucho.bytecode.JClass; 41 import com.caucho.bytecode.JClassWrapper; 42 import com.caucho.bytecode.JField; 43 import com.caucho.bytecode.JMethod; 44 import com.caucho.bytecode.JType; 45 import com.caucho.config.ConfigException; 46 import com.caucho.java.JavaWriter; 47 import com.caucho.log.Log; 48 import com.caucho.util.CharBuffer; 49 import com.caucho.util.L10N; 50 51 import java.io.IOException ; 52 import java.io.Serializable ; 53 import java.sql.SQLException ; 54 import java.util.ArrayList ; 55 import java.util.HashSet ; 56 import java.util.logging.Logger ; 57 58 61 abstract public class AbstractField implements AmberField { 62 private static final L10N L = new L10N(AbstractField.class); 63 protected static final Logger log = Log.open(AbstractField.class); 64 65 private AbstractStatefulType _sourceType; 66 67 private String _name; 68 69 private JType _javaType; 70 71 private JMethod _getterMethod; 72 private JMethod _setterMethod; 73 74 private boolean _isLazy = true; 75 76 private int _updateIndex; 77 private int _loadGroupIndex = -1; 78 79 AbstractField(AbstractStatefulType sourceType) 80 { 81 _sourceType = sourceType; 82 } 83 84 AbstractField(AbstractStatefulType sourceType, String name) 85 throws ConfigException 86 { 87 this(sourceType); 88 89 setName(name); 90 } 91 92 95 public void setName(String name) 96 throws ConfigException 97 { 98 _name = name; 99 100 if (! getSourceType().isFieldAccess()) { 101 char ch = name.charAt(0); 102 if (Character.isLowerCase(ch)) 103 name = Character.toUpperCase(ch) + name.substring(1); 104 105 String getter = "get" + name; 106 String setter = "set" + name; 107 108 _getterMethod = AbstractStatefulType.getGetter(getBeanClass(), getter); 109 110 if (_getterMethod == null) { 111 getter = "is" + name; 112 _getterMethod = AbstractStatefulType.getGetter(getBeanClass(), getter); 113 } 114 115 120 121 if (_getterMethod == null) { 122 JField field = AbstractStatefulType.getField(getBeanClass(), _name); 123 124 if (field == null) 125 throw new ConfigException(L.l("{0}: {1} has no matching field.", 126 getBeanClass().getName(), _name)); 127 128 _javaType = field.getGenericType(); 129 } 130 else { 131 _javaType = _getterMethod.getGenericReturnType(); 132 133 _setterMethod = AbstractStatefulType.getSetter(getBeanClass(), setter); 134 } 135 } 136 else { 137 JField field = AbstractStatefulType.getField(getBeanClass(), name); 138 139 if (field == null) 140 throw new ConfigException(L.l("{0}: {1} has no matching field.", 141 getBeanClass().getName(), name)); 142 143 _javaType = field.getGenericType(); 144 } 145 146 151 } 152 153 156 public String getName() 157 { 158 return _name; 159 } 160 161 164 protected void setJavaType(JType type) 165 { 166 _javaType = type; 167 } 168 169 172 protected void setJavaType(Class type) 173 { 174 setJavaType(new JClassWrapper(type, getPersistenceUnit().getJClassLoader())); 175 } 176 177 180 public AbstractStatefulType getSourceType() 181 { 182 return _sourceType; 183 } 184 185 188 public AmberPersistenceUnit getPersistenceUnit() 189 { 190 return getSourceType().getPersistenceUnit(); 191 } 192 193 196 public JClass getBeanClass() 197 { 198 return getSourceType().getBeanClass(); 199 } 200 201 205 public RelatedType getEntitySourceType() 206 { 207 return (RelatedType) getSourceType(); 208 } 209 210 213 public Table getTable() 214 { 215 return getEntitySourceType().getTable(); 216 } 217 218 221 public int getIndex() 222 { 223 return _updateIndex; 224 } 225 226 229 public void setIndex(int index) 230 { 231 _updateIndex = index; 232 } 233 234 237 public int getLoadGroupIndex() 238 { 239 return _loadGroupIndex; 240 } 241 242 245 public long getCreateLoadMask(int group) 246 { 247 int index = getLoadGroupIndex(); 248 249 if (64 * group <= index && index < 64 * (group + 1)) 250 return 1L << (index % 64); 251 else 252 return 0; 253 } 254 255 258 public boolean isLazy() 259 { 260 return _isLazy; 261 } 262 263 266 public void setLazy(boolean isLazy) 267 { 268 _isLazy = isLazy; 269 } 270 271 274 public JMethod getGetterMethod() 275 { 276 return _getterMethod; 277 } 278 279 282 public String getGetterName() 283 { 284 if (getSourceType().isFieldAccess()) 285 return "__caucho_get_" + getName(); 286 else 287 return _getterMethod.getName(); 288 } 289 290 293 public String getJavaTypeName() 294 { 295 return getJavaType().getPrintName(); 296 } 297 298 301 private String getJavaTypeName(Class cl) 302 { 303 if (cl.isArray()) 304 return getJavaTypeName(cl.getComponentType()) + "[]"; 305 else 306 return cl.getName(); 307 } 308 309 312 public JType getJavaType() 313 { 314 return _javaType; 315 } 316 317 320 public JMethod getSetterMethod() 321 { 322 return _setterMethod; 323 } 324 325 328 public String getSetterName() 329 { 330 if (getSourceType().isFieldAccess()) 331 return "__caucho_set_" + getName(); 332 else if (_setterMethod != null) 333 return _setterMethod.getName(); 334 else 335 return "set" + getGetterName().substring(3); 336 } 337 338 341 public boolean isFieldAccess() 342 { 343 return getSourceType().isFieldAccess(); 344 } 345 346 349 public boolean isAbstract() 350 { 351 353 if (getSourceType().isFieldAccess() || getSourceType().isIdClass()) 354 return true; 355 else if (_getterMethod == null) 356 return false; 357 else 358 return _getterMethod.isAbstract(); 359 } 360 361 364 public boolean isCascadable() 365 { 366 return false; 367 } 368 369 372 public boolean isUpdateable() 373 { 374 return true; 375 } 376 377 380 public void init() 381 throws ConfigException 382 { 383 if (_loadGroupIndex < 0) { 384 if (_isLazy) 385 _loadGroupIndex = getEntitySourceType().nextLoadGroupIndex(); 386 else 387 _loadGroupIndex = getEntitySourceType().getDefaultLoadGroupIndex(); 388 } 389 } 390 391 394 public void generatePostConstructor(JavaWriter out) 395 throws IOException 396 { 397 } 398 399 402 public void generatePrologue(JavaWriter out, HashSet <Object > completedSet) 403 throws IOException 404 { 405 if (isAbstract()) { 406 out.println(); 407 out.print("public "); 408 out.print(getJavaType().getPrintName()); 409 out.print(" " + getFieldName() + ";"); 410 } 411 } 412 413 416 public String generateLoadSelect(Table table, String id) 417 { 418 return null; 419 } 420 421 424 public String generateSelect(String id) 425 { 426 return null; 427 } 428 429 432 public String generateJavaSelect(String id) 433 { 434 return null; 435 } 436 437 440 public String generateWhere(String id) 441 { 442 return null; 443 } 444 445 448 public void generateUpdate(CharBuffer sql) 449 { 450 } 451 452 455 public void generateUpdate(JavaWriter out, String maskVar, String pstmt, 456 String index) 457 throws IOException 458 { 459 int group = getIndex() / 64; 460 long mask = 1L << getIndex() % 64; 461 462 out.println(); 463 out.println("if ((" + maskVar + "_" + group + " & " + mask + "L) != 0) {"); 464 out.pushDepth(); 465 466 generateSet(out, pstmt, index); 467 468 out.popDepth(); 469 out.println("}"); 470 } 471 472 475 public boolean hasLoadGroup(int index) 476 { 477 return index == _loadGroupIndex; 478 } 479 480 483 public int generateLoad(JavaWriter out, String rs, 484 String indexVar, int index) 485 throws IOException 486 { 487 return index; 488 } 489 490 493 public int generateLoadEager(JavaWriter out, String rs, 494 String indexVar, int index) 495 throws IOException 496 { 497 return index; 498 } 499 500 503 public void generateLoadFromObject(JavaWriter out, String obj) 504 throws IOException 505 { 506 if (getGetterMethod() == null || getSetterMethod() == null) 507 return; 508 509 String getter = getGetterName(); 510 511 String loadVar = "__caucho_loadMask_" + (getLoadGroupIndex() / 64); 512 long loadMask = (1L << getLoadGroupIndex()); 513 514 out.println("if ((" + loadVar + " & " + loadMask + "L) != 0)"); 515 out.print(" "); 516 517 out.println(" " + generateSuperSetter(generateGet(obj)) + ";"); 518 } 519 520 523 public void generateSet(JavaWriter out, String obj) 524 throws IOException 525 { 526 out.println(generateSuperSetter(obj) + ";"); 527 } 528 529 532 public void generateUpdateFromObject(JavaWriter out, String obj) 533 throws IOException 534 { 535 out.println(generateSuperSetter(generateGet(obj)) + ";"); 536 } 537 538 543 public void generateGet(JavaWriter out, String value) 544 throws IOException 545 { 546 out.print(generateGet(value)); 547 } 548 549 552 public String generateNull() 553 { 554 return "null"; 555 } 556 557 562 public String generateGet(String obj) 563 { 564 if (obj == null) 565 return generateNull(); 566 567 if (obj.equals("super")) 568 return generateSuperGetter(); 569 else if (! isAbstract()) 570 return obj + "." + _getterMethod.getName() + "()"; 571 else if (_getterMethod != null) 572 return obj + "." + _getterMethod.getName() + "()"; 573 else 574 return obj + "." + getFieldName(); 575 } 576 577 582 public String generateSet(String obj, String value) 583 { 584 if (obj.equals("super")) 585 return generateSuperSetter(value); 586 else if (isAbstract()) 587 return obj + "." + getFieldName() + " = " + value; 588 else if (_setterMethod != null) 589 return obj + "." + _setterMethod.getName() + "(" + value + ")"; 590 else 591 return ""; } 593 594 597 protected String getFieldName() 598 { 599 return "__amber_" + getName(); 600 } 601 602 605 public final String generateInsert() 606 { 607 return null; 608 } 609 610 613 public void generateInsertColumns(ArrayList <String > columns) 614 { 615 } 616 617 620 public void generateGetProperty(JavaWriter out) 621 throws IOException 622 { 623 624 } 625 626 629 public void generateSetProperty(JavaWriter out) 630 throws IOException 631 { 632 } 633 634 637 public String generateSuperGetter() 638 { 639 644 return("__caucho_super_get_" + getName() + "()"); 645 } 646 647 650 public String generateSuperSetter(String value) 651 { 652 657 return "__caucho_super_set_" + getName() + "(" + value + ")"; 658 } 659 660 663 public void generateSuperGetter(JavaWriter out) 664 throws IOException 665 { 666 out.println(); 667 out.println("public final " + getJavaTypeName() + " __caucho_super_get_" + getName() + "()"); 668 out.println("{"); 669 out.pushDepth(); 670 671 if (isAbstract() || getGetterMethod() == null) 672 out.println("return " + getFieldName() + ";"); 673 else 674 out.println("return super." + getGetterName() + "();"); 675 676 out.popDepth(); 677 out.println("}"); 678 } 679 680 683 public void generateSuperSetter(JavaWriter out) 684 throws IOException 685 { 686 out.println(); 687 out.println("public final void __caucho_super_set_" + getName() + "(" + getJavaTypeName() + " v)"); 688 out.println("{"); 689 out.pushDepth(); 690 691 if (isAbstract() || getGetterMethod() == null) 692 out.println(getFieldName() + " = v;"); 693 else if (getSetterMethod() != null) 694 out.println("super." + getSetterMethod().getName() + "(v);"); 695 696 out.popDepth(); 697 out.println("}"); 698 } 699 700 703 public String generateCreateTableSQL(AmberPersistenceUnit manager) 704 { 705 return null; 706 } 707 708 711 public void generateSet(JavaWriter out, String pstmt, String index) 712 throws IOException 713 { 714 generateSet(out, pstmt, index, "super"); 715 } 716 717 720 public void generateInsertSet(JavaWriter out, String pstmt, 721 String index, String obj) 722 throws IOException 723 { 724 generateSet(out, pstmt, index, obj); 725 } 726 727 730 public void generateUpdateSet(JavaWriter out, String pstmt, 731 String index, String obj) 732 throws IOException 733 { 734 generateSet(out, pstmt, index, obj); 735 } 736 737 740 public void generateCopyUpdateObject(JavaWriter out, 741 String dst, String src, 742 int updateIndex) 743 throws IOException 744 { 745 747 if (getIndex() == updateIndex) { 748 String value = generateGet(src); 749 out.println(generateSet(dst, value) + ";"); 750 } 751 } 752 753 756 public void generateCopyLoadObject(JavaWriter out, 757 String dst, String src, 758 int loadIndex) 759 throws IOException 760 { 761 763 if (getLoadGroupIndex() == loadIndex) { 765 String value = generateGet(src); 766 out.println(generateSet(dst, value) + ";"); 767 } 768 } 769 770 773 public void generateSet(JavaWriter out, String pstmt, 774 String index, String obj) 775 throws IOException 776 { 777 } 778 779 782 public String toObject(String value) 783 { 784 return value; 785 } 786 787 790 public void link() 791 { 792 } 793 794 797 public void generatePreDelete(JavaWriter out) 798 throws IOException 799 { 800 } 801 802 805 public void generatePostDelete(JavaWriter out) 806 throws IOException 807 { 808 } 809 810 813 public void generateExpire(JavaWriter out) 814 throws IOException 815 { 816 } 817 818 821 public void generateInvalidateForeign(JavaWriter out) 822 throws IOException 823 { 824 } 825 826 829 public void childDelete(AmberConnection aConn, Serializable primaryKey) 830 throws SQLException 831 { 832 } 833 834 837 public String generateCastFromObject(String value) 838 { 839 return value; 840 } 841 842 845 public String generateEquals(String leftBase, String value) 846 { 847 return leftBase + ".equals(" + value + ")"; 848 } 849 850 853 public AmberExpr createExpr(QueryParser parser, PathExpr parent) 854 { 855 throw new UnsupportedOperationException (getClass().getName()); 856 } 857 858 public String toString() 859 { 860 return getClass().getName() + "[" + getName() + "]"; 861 } 862 } 863 | Popular Tags |