1 23 24 25 package com.sun.jdo.api.persistence.enhancer.impl; 26 27 import java.util.Map ; 28 import java.util.Enumeration ; 29 30 import com.sun.jdo.api.persistence.enhancer.classfile.ClassFile; 31 import com.sun.jdo.api.persistence.enhancer.classfile.ClassField; 32 import com.sun.jdo.api.persistence.enhancer.classfile.VMConstants; 33 import com.sun.jdo.api.persistence.enhancer.classfile.Descriptor; 34 35 import com.sun.jdo.api.persistence.enhancer.util.Support; 36 import com.sun.jdo.api.persistence.enhancer.util.InternalError; 37 38 import com.sun.jdo.api.persistence.enhancer.meta.JDOMetaData; 40 41 42 60 63 67 final class FieldAction 68 extends Support 69 implements VMConstants { 70 71 72 private ClassField theField; 74 75 76 private ClassAction ca; 78 79 80 private final Environment env; 83 84 85 private boolean fieldIsPrimaryKey; 87 88 89 private boolean fieldIsMutableSCO; 91 92 95 private boolean fieldIsPersistent; 96 97 99 private int nFieldArrayDims; 100 101 102 private String fieldClassName; 103 104 106 private FieldTypeInfo fieldTypeInfo; 107 108 109 113 114 117 FieldAction(ClassAction ca, 119 ClassField field, 120 Environment env) { 121 this.ca = ca; 122 this.theField = field; 123 this.env = env; 124 } 125 126 128 131 String typeDescriptor() { 132 return theField.signature().asString(); 133 } 134 135 141 String typeName() { 142 String typeDesc = typeDescriptor(); 143 if (typeDesc.charAt(0) == 'L') 144 return typeDesc.substring(1, typeDesc.length() - 1); 145 return typeDesc; 146 } 147 148 152 160 161 164 170 171 174 boolean isPrimaryKey() { 176 return fieldIsPrimaryKey; 177 } 178 179 182 boolean isMutableSCO() { 184 return fieldIsMutableSCO; 185 } 186 187 191 boolean isPersistent() { 192 return fieldIsPersistent; 193 } 194 195 198 String fieldName() { 199 return theField.name().asString(); 200 } 201 202 206 boolean isSynthetic() { 207 return theField.attributes().findAttribute("Synthetic") != null; } 209 210 214 String createMethod() { 215 return fieldTypeInfo.fieldCreateMethod; 216 } 217 218 222 String createMethodSig() { 223 return fieldTypeInfo.fieldCreateMethodSig; 224 } 225 226 230 String setMethod() { 231 return fieldTypeInfo.fieldSetMethod; 232 } 233 234 238 String setMethodSig() { 239 return fieldTypeInfo.fieldSetMethodSig; 240 } 241 242 245 int setMethodArg() { 246 return fieldTypeInfo.fieldSetArgType; 247 } 248 249 253 String getMethod() { 254 return fieldTypeInfo.fieldGetMethod; 255 } 256 257 261 String getMethodSig() { 262 return fieldTypeInfo.fieldGetMethodSig; 263 } 264 265 268 int getMethodReturn() { 269 return fieldTypeInfo.fieldGetReturnType; 270 } 271 272 276 String fieldClassName() { 277 return fieldClassName; 278 } 279 280 284 int nDims () { 285 return nFieldArrayDims; 286 } 287 288 291 void check() { 292 296 String sig = theField.signature().asString(); 297 fieldTypeInfo = FieldTypeInfo.determineFieldType(sig, env); 298 299 final String className = ca.className(); 300 final String userClass = ca.userClassName(); 301 final String fieldName = theField.name().asString(); 302 final String fullFieldName = userFieldName(); 303 304 final JDOMetaData jdoMetaData = env.getJDOMetaData(); 306 307 final boolean fieldShouldBePersistent 310 = jdoMetaData.isPersistentField(className, fieldName); 311 if (false) { 313 System.out.println("FieldAction.check(): field " + className + "/" + fieldName + " should be persistent = " + fieldShouldBePersistent); 317 } 318 319 fieldIsPrimaryKey 321 = jdoMetaData.isPrimaryKeyField(className, fieldName); 322 if (false) { 324 System.out.println("FieldAction.check(): field " + className + "/" + fieldName + " is primary key = " + fieldIsPrimaryKey); 328 } 329 330 fieldIsMutableSCO 332 = jdoMetaData.isMutableSecondClassObjectType(typeName()); 333 if (false) { 335 System.out.println("FieldAction.check(): field " + className + "/" + fieldName + " is mutable SCO = " + fieldIsMutableSCO); 339 } 340 341 nFieldArrayDims = 0; 342 while (sig.charAt(nFieldArrayDims) == '[') 343 nFieldArrayDims++; 344 345 if (sig.charAt(nFieldArrayDims) == 'L') 347 fieldClassName = sig.substring(nFieldArrayDims+1, sig.length()-1); 348 349 if (!fieldShouldBePersistent) { 351 return; 353 } 354 355 357 affirm(!theField.isStatic(), 359 ("The field " + fullFieldName + " is a static field which cannot be made persistent.")); 362 affirm(!theField.isFinal(), 364 ("The field " + fullFieldName + " is a final field which cannot be made persistent.")); 367 affirm((fieldClassName == null 369 || jdoMetaData.isSecondClassObjectType(fieldClassName) 370 || jdoMetaData.isPersistenceCapableClass(fieldClassName)), 371 ("The field " + fullFieldName + " cannot be made persistent because of a non-primitive, " + " non-sco, or non-pc target type " + fieldClassName)); 375 fieldIsPersistent = true; 376 } 377 378 382 392 393 396 String userFieldName() { 397 return ca.userClassName() + "." + theField.name().asString(); } 399 400 403 private String userSig(String vmSig) { 404 return Descriptor.userFieldSig(vmSig); 406 } 407 } 408 409 410 class FieldTypeInfo 411 extends Support 412 implements VMConstants { 413 414 415 String fieldCreateMethod; 416 String fieldCreateMethodSig; 417 418 419 String fieldGetMethod; 420 String fieldGetMethodSig; 421 int fieldGetReturnType; 422 423 424 String fieldSetMethod; 425 String fieldSetMethodSig; 426 int fieldSetArgType; 427 428 430 private FieldTypeInfo(String createName, String createSig, 431 String setName, String setSig, int argType, 432 String getName, String getSig, int returnType) { 433 fieldCreateMethod = createName; 434 fieldCreateMethodSig = createSig; 435 fieldGetMethod = getName; 436 fieldGetMethodSig = getSig; 437 fieldGetReturnType = returnType; 438 fieldSetMethod = setName; 439 fieldSetMethodSig = setSig; 440 fieldSetArgType = argType; 441 } 442 443 static private FieldTypeInfo byteInfo = 444 new FieldTypeInfo("createByte", "(Ljava/lang/String;)Lcom/sun/forte4j/persistence/internal/Field;", "setByteField", "(IBLcom/sun/forte4j/persistence/internal/ClassInfo;)V", T_BYTE, "getByteField", "(ILcom/sun/forte4j/persistence/internal/ClassInfo;)B", T_BYTE); 448 static private FieldTypeInfo charInfo = 449 new FieldTypeInfo("createChar", "(Ljava/lang/String;)Lcom/sun/forte4j/persistence/internal/Field;", "setCharField", "(ICLcom/sun/forte4j/persistence/internal/ClassInfo;)V", T_CHAR, "getCharField", "(ILcom/sun/forte4j/persistence/internal/ClassInfo;)C", T_CHAR); 453 static private FieldTypeInfo shortInfo = 454 new FieldTypeInfo("createShort", "(Ljava/lang/String;)Lcom/sun/forte4j/persistence/internal/Field;", "setShortField", "(ISLcom/sun/forte4j/persistence/internal/ClassInfo;)V", T_SHORT, "getShortField", "(ILcom/sun/forte4j/persistence/internal/ClassInfo;)S", T_SHORT); 458 static private FieldTypeInfo intInfo = 459 new FieldTypeInfo("createInt", "(Ljava/lang/String;)Lcom/sun/forte4j/persistence/internal/Field;", "setIntField", "(IILcom/sun/forte4j/persistence/internal/ClassInfo;)V", T_INT, "getIntField", "(ILcom/sun/forte4j/persistence/internal/ClassInfo;)I", T_INT); 463 static private FieldTypeInfo longInfo = 464 new FieldTypeInfo("createLong", "(Ljava/lang/String;)Lcom/sun/forte4j/persistence/internal/Field;", "setLongField", "(IJLcom/sun/forte4j/persistence/internal/ClassInfo;)V", T_LONG, "getLongField", "(ILcom/sun/forte4j/persistence/internal/ClassInfo;)J", T_LONG); 468 static private FieldTypeInfo floatInfo = 469 new FieldTypeInfo("createFloat", "(Ljava/lang/String;)Lcom/sun/forte4j/persistence/internal/Field;", "setFloatField", "(IFLcom/sun/forte4j/persistence/internal/ClassInfo;)V", T_FLOAT, "getFloatField", "(ILcom/sun/forte4j/persistence/internal/ClassInfo;)F", T_FLOAT); 473 static private FieldTypeInfo doubleInfo = 474 new FieldTypeInfo("createDouble", "(Ljava/lang/String;)Lcom/sun/forte4j/persistence/internal/Field;", "setDoubleField", "(IDLcom/sun/forte4j/persistence/internal/ClassInfo;)V", T_DOUBLE, "getDoubleField", "(ILcom/sun/forte4j/persistence/internal/ClassInfo;)D", T_DOUBLE); 478 static private FieldTypeInfo booleanInfo = 479 new FieldTypeInfo("createBoolean", "(Ljava/lang/String;)Lcom/sun/forte4j/persistence/internal/Field;", "setBooleanField", "(IZLcom/sun/forte4j/persistence/internal/ClassInfo;)V", T_BOOLEAN, "getBooleanField", "(ILcom/sun/forte4j/persistence/internal/ClassInfo;)Z", T_BOOLEAN); 482 483 static private FieldTypeInfo classInfo = 484 new FieldTypeInfo("createClass", "(Ljava/lang/String;Ljava/lang/String;)Lcom/sun/forte4j/persistence/internal/Field;", "setClassField", "(ILjava/lang/Object;Lcom/sun/forte4j/persistence/internal/ClassInfo;)V", TC_OBJECT, "getClassField", "(ILcom/sun/forte4j/persistence/internal/ClassInfo;)Ljava/lang/Object;", TC_OBJECT); 488 496 497 static private FieldTypeInfo stringInfo = 498 new FieldTypeInfo("createString", "(Ljava/lang/String;)Lcom/sun/forte4j/persistence/internal/Field;", "setStringField", "(ILjava/lang/String;Lcom/sun/forte4j/persistence/internal/ClassInfo;)V", TC_STRING, "getStringField", "(ILcom/sun/forte4j/persistence/internal/ClassInfo;)Ljava/lang/String;", TC_STRING); 502 static private FieldTypeInfo byteArrayInfo = 503 new FieldTypeInfo("createByteArray", "(Ljava/lang/String;I)Lcom/sun/forte4j/persistence/internal/Field;", "setArrayField", "(ILjava/lang/Object;Lcom/sun/forte4j/persistence/internal/ClassInfo;)V", TC_OBJECT, "getArrayField", "(ILcom/sun/forte4j/persistence/internal/ClassInfo;)Ljava/lang/Object;", TC_OBJECT); 507 static private FieldTypeInfo charArrayInfo = 508 new FieldTypeInfo("createCharArray", "(Ljava/lang/String;I)Lcom/sun/forte4j/persistence/internal/Field;", "setArrayField", "(ILjava/lang/Object;Lcom/sun/forte4j/persistence/internal/ClassInfo;)V", TC_OBJECT, "getArrayField", "(ILcom/sun/forte4j/persistence/internal/ClassInfo;)Ljava/lang/Object;", TC_OBJECT); 512 static private FieldTypeInfo shortArrayInfo = 513 new FieldTypeInfo("createShortArray", "(Ljava/lang/String;I)Lcom/sun/forte4j/persistence/internal/Field;", "setArrayField", "(ILjava/lang/Object;Lcom/sun/forte4j/persistence/internal/ClassInfo;)V", TC_OBJECT, "getArrayField", "(ILcom/sun/forte4j/persistence/internal/ClassInfo;)Ljava/lang/Object;", TC_OBJECT); 517 static private FieldTypeInfo intArrayInfo = 518 new FieldTypeInfo("createIntArray", "(Ljava/lang/String;I)Lcom/sun/forte4j/persistence/internal/Field;", "setArrayField", "(ILjava/lang/Object;Lcom/sun/forte4j/persistence/internal/ClassInfo;)V", TC_OBJECT, "getArrayField", "(ILcom/sun/forte4j/persistence/internal/ClassInfo;)Ljava/lang/Object;", TC_OBJECT); 522 static private FieldTypeInfo longArrayInfo = 523 new FieldTypeInfo("createLongArray", "(Ljava/lang/String;I)Lcom/sun/forte4j/persistence/internal/Field;", "setArrayField", "(ILjava/lang/Object;Lcom/sun/forte4j/persistence/internal/ClassInfo;)V", TC_OBJECT, "getArrayField", "(ILcom/sun/forte4j/persistence/internal/ClassInfo;)Ljava/lang/Object;", TC_OBJECT); 527 static private FieldTypeInfo floatArrayInfo = 528 new FieldTypeInfo("createFloatArray", "(Ljava/lang/String;I)Lcom/sun/forte4j/persistence/internal/Field;", "setArrayField", "(ILjava/lang/Object;Lcom/sun/forte4j/persistence/internal/ClassInfo;)V", TC_OBJECT, "getArrayField", "(ILcom/sun/forte4j/persistence/internal/ClassInfo;)Ljava/lang/Object;", TC_OBJECT); 532 static private FieldTypeInfo doubleArrayInfo = 533 new FieldTypeInfo("createDoubleArray", "(Ljava/lang/String;I)Lcom/sun/forte4j/persistence/internal/Field;", "setArrayField", "(ILjava/lang/Object;Lcom/sun/forte4j/persistence/internal/ClassInfo;)V", TC_OBJECT, "getArrayField", "(ILcom/sun/forte4j/persistence/internal/ClassInfo;)Ljava/lang/Object;", TC_OBJECT); 537 static private FieldTypeInfo booleanArrayInfo = 538 new FieldTypeInfo("createBooleanArray", "(Ljava/lang/String;I)Lcom/sun/forte4j/persistence/internal/Field;", "setArrayField", "(ILjava/lang/Object;Lcom/sun/forte4j/persistence/internal/ClassInfo;)V", TC_OBJECT, "getArrayField", "(ILcom/sun/forte4j/persistence/internal/ClassInfo;)Ljava/lang/Object;", TC_OBJECT); 542 static private FieldTypeInfo classArrayInfo = 543 new FieldTypeInfo("createClassArray", "(Ljava/lang/String;Ljava/lang/String;I)Lcom/sun/forte4j/persistence/internal/Field;", "setArrayField", "(ILjava/lang/Object;Lcom/sun/forte4j/persistence/internal/ClassInfo;)V", TC_OBJECT, "getArrayField", "(ILcom/sun/forte4j/persistence/internal/ClassInfo;)Ljava/lang/Object;", TC_OBJECT); 547 static private FieldTypeInfo interfaceArrayInfo = 548 new FieldTypeInfo("createInterfaceArray", "(Ljava/lang/String;Ljava/lang/String;I)Lcom/sun/forte4j/persistence/internal/Field;", "setArrayField", "(ILjava/lang/Object;Lcom/sun/forte4j/persistence/internal/ClassInfo;)V", TC_OBJECT, "getArrayField", "(ILcom/sun/forte4j/persistence/internal/ClassInfo;)Ljava/lang/Object;", TC_OBJECT); 552 static private FieldTypeInfo stringArrayInfo = 553 new FieldTypeInfo("createStringArray", "(Ljava/lang/String;I)Lcom/sun/forte4j/persistence/internal/Field;", "setArrayField", "(ILjava/lang/Object;Lcom/sun/forte4j/persistence/internal/ClassInfo;)V", TC_OBJECT, "getArrayField", "(ILcom/sun/forte4j/persistence/internal/ClassInfo;)Ljava/lang/Object;", TC_OBJECT); 557 558 static FieldTypeInfo determineFieldType(String sig, 559 Environment env) { 560 switch (sig.charAt(0)) { 561 case 'B': return byteInfo; 563 case 'C': return charInfo; 565 case 'D': return doubleInfo; 567 case 'F': return floatInfo; 569 case 'I': return intInfo; 571 case 'J': return longInfo; 573 case 'S': return shortInfo; 575 case 'Z': return booleanInfo; 577 case 'L': if (sig.equals("Ljava/lang/String;")) return stringInfo; 580 { 581 592 return classInfo; 593 } 594 case '[': int baseTypeIndex = findArrayBaseType(sig); 596 switch (sig.charAt(baseTypeIndex)) { 597 case 'B': return byteArrayInfo; 599 case 'C': return charArrayInfo; 601 case 'D': return doubleArrayInfo; 603 case 'F': return floatArrayInfo; 605 case 'I': return intArrayInfo; 607 case 'J': return longArrayInfo; 609 case 'S': return shortArrayInfo; 611 case 'Z': return booleanArrayInfo; 613 case 'L': if (sig.substring(baseTypeIndex).equals("Ljava/lang/String;")) return stringArrayInfo; 616 { 617 629 return classArrayInfo; 630 } 631 default: 632 throw new InternalError ("Missing case"); } 634 635 default: 636 throw new InternalError ("Missing case"); } 638 } 639 640 private static int findArrayBaseType(String sig) { 641 int idx = 0; 642 while (sig.charAt(idx) == '[') 643 idx++; 644 return idx; 645 } 646 } 647 648 | Popular Tags |