1 23 24 package com.sun.jdo.api.persistence.enhancer.generator; 25 26 import java.util.List ; 27 import java.util.ArrayList ; 28 import java.util.HashMap ; 29 30 import com.sun.jdo.api.persistence.enhancer.meta.ExtendedJDOMetaData; 31 import com.sun.jdo.api.persistence.enhancer.util.Assertion; 32 import com.sun.jdo.spi.persistence.utility.JavaTypeHelper; 33 import com.sun.jdo.spi.persistence.utility.generator.JavaClassWriterHelper; 34 35 38 final class ImplHelper extends Assertion 39 { 40 static final String [] COMMENT_ENHANCER_ADDED 42 = null; static final String [] COMMENT_NOT_ENHANCER_ADDED 44 = null; 46 static final String CLASSNAME_JDO_PERSISTENCE_CAPABLE 47 = "com.sun.jdo.spi.persistence.support.sqlstore.PersistenceCapable"; 48 static final String CLASSNAME_JDO_PERSISTENCE_MANAGER 49 = "com.sun.jdo.api.persistence.support.PersistenceManager"; 50 static final String CLASSNAME_JDO_STATE_MANAGER 51 = "com.sun.jdo.spi.persistence.support.sqlstore.StateManager"; 52 static final String CLASSNAME_JDO_SCO 53 = "com.sun.jdo.spi.persistence.support.sqlstore.SCO"; 54 static final String CLASSNAME_JDO_FATAL_EXCEPTION 55 = "com.sun.jdo.api.persistence.support.JDOFatalException"; 56 57 static final String FIELDNAME_JDO_FLAGS 58 = "jdoFlags"; 59 static final String FIELDNAME_JDO_STATE_MANAGER 60 = "jdoStateManager"; 61 static final String FIELDNAME_JDO_INHERITED_FIELD_COUNT 62 = "jdoInheritedFieldCount"; 63 static final String FIELDNAME_JDO_FIELD_NAMES 64 = "jdoFieldNames"; 65 static final String FIELDNAME_JDO_FIELD_TYPES 66 = "jdoFieldTypes"; 67 static final String FIELDNAME_JDO_FIELD_FLAGS 68 = "jdoFieldFlags"; 69 static final String METHODNAME_JDO_NEW_INSTANCE 70 = "jdoNewInstance"; 71 static final String METHODNAME_JDO_SET_FIELD 72 = "jdoSetField"; 73 static final String METHODNAME_JDO_GET_FIELD 74 = "jdoGetField"; 75 static final String METHODNAME_JDO_GET_STATE_MANAGER 76 = "jdoGetStateManager"; 77 static final String METHODNAME_JDO_SET_STATE_MANAGER 78 = "jdoSetStateManager"; 79 static final String METHODNAME_JDO_GET_FLAGS 80 = "jdoGetFlags"; 81 static final String METHODNAME_JDO_SET_FLAGS 82 = "jdoSetFlags"; 83 static final String METHODNAME_JDO_GET_PERSISTENCE_MANAGER 84 = "jdoGetPersistenceManager"; 85 static final String METHODNAME_JDO_CLEAR 86 = "jdoClear"; 87 static final String METHODNAME_JDO_MAKE_DIRTY 88 = "jdoMakeDirty"; 89 static final String METHODNAME_JDO_GET_OBJECT_ID 90 = "jdoGetObjectId"; 91 static final String METHODNAME_JDO_IS_PERSISTENT 92 = "jdoIsPersistent"; 93 static final String METHODNAME_JDO_IS_TRANSACTIONAL 94 = "jdoIsTransactional"; 95 static final String METHODNAME_JDO_IS_NEW 96 = "jdoIsNew"; 97 static final String METHODNAME_JDO_IS_DIRTY 98 = "jdoIsDirty"; 99 static final String METHODNAME_JDO_IS_DELETED 100 = "jdoIsDeleted"; 101 102 static private final HashMap typeNameConversion = new HashMap (); 103 static 104 { 105 typeNameConversion.put(int.class.getName(), "Int"); 106 typeNameConversion.put(long.class.getName(), "Long"); 107 typeNameConversion.put(byte.class.getName(), "Byte"); 108 typeNameConversion.put(char.class.getName(), "Char"); 109 typeNameConversion.put(boolean.class.getName(), "Boolean"); 110 typeNameConversion.put(short.class.getName(), "Short"); 111 typeNameConversion.put(float.class.getName(), "Float"); 112 typeNameConversion.put(double.class.getName(), "Double"); 113 typeNameConversion.put("String", "String"); 114 } 115 116 static private boolean isPrimitiveClass(String className) 117 { 118 affirm(!className.equals("void")); 120 return JavaTypeHelper.getWrapperName(className) != null; 121 } 122 123 static private String getConvertedTypeName(String fieldType) 124 { 125 final String name = (String )typeNameConversion.get(fieldType); 126 return (name != null ? name : JavaClassWriterHelper.Object_); 127 } 128 129 static private String getMethodNameGetField(String fieldType) 130 { 131 return JavaClassWriterHelper.get_ + 132 getConvertedTypeName(fieldType) + "Field"; 133 } 134 135 static private String getMethodNameSetField(String fieldType) 136 { 137 return JavaClassWriterHelper.set_ + 138 getConvertedTypeName(fieldType) + "Field"; 139 } 140 141 142 144 static String [] getJDOManagedFieldCountImpl(int fieldcount) 145 { 146 return new String [] { 147 FIELDNAME_JDO_INHERITED_FIELD_COUNT + " + " + fieldcount + JavaClassWriterHelper.delim_ 148 }; 149 } 150 151 static String [] getDefaultConstructorImpl() 152 { 153 return JavaClassWriterHelper.super_; 154 } 155 156 static String [] getCloneImpl(String className) 157 { 158 className = normalizeClassName(className); 159 String [] bodies = new String [4]; 160 int i = 0; 161 bodies[i++] = (new StringBuffer (className)).append(" clone = (") 162 .append(className).append(")super.clone();").toString(); 163 bodies[i++] = "clone.jdoFlags = (byte)0;"; 164 bodies[i++] = "clone.jdoStateManager = null;"; 165 bodies[i++] = "return clone;"; 166 return bodies; 167 } 168 169 static String [] getJDOConstructorSMImpl(String statemanager) 170 { 171 String [] bodies = new String [2]; 172 int i = 0; 173 bodies[i++] = FIELDNAME_JDO_FLAGS + " = (byte)1; // == LOAD_REQUIRED"; 174 bodies[i++] = "this." + FIELDNAME_JDO_STATE_MANAGER 175 + " = " + statemanager + JavaClassWriterHelper.delim_; 176 return bodies; 177 } 178 179 static String [] getJDONewInstanceImpl(String className, 180 String statemanager) 181 { 182 className = getClassName(className); 183 return new String [] { (new StringBuffer ("return new ")) 184 .append(className).append("(").append(statemanager) 185 .append(");").toString() }; 186 } 187 188 static String [] getFieldDirectReadImpl(String fieldName, 189 String fieldType, 190 int fieldNumber) 191 { 192 fieldType = normalizeClassName(fieldType); 193 return new String [] { 194 "// annotation: grant direct read access", 195 "return " + fieldName + JavaClassWriterHelper.delim_ 196 }; 197 } 198 199 static String [] getFieldMediateReadImpl(String fieldName, 200 String fieldType, 201 int fieldNumber) 202 { 203 fieldType = normalizeClassName(fieldType); 204 String [] bodies = new String [6]; 205 int i = 0; 206 bodies[i++] = "// annotation: mediate read access"; 207 bodies[i++] = (new StringBuffer ("final ")) 208 .append(CLASSNAME_JDO_STATE_MANAGER) 209 .append(" stateManager = this.") 210 .append(FIELDNAME_JDO_STATE_MANAGER) 211 .append(JavaClassWriterHelper.delim_).toString(); 212 bodies[i++] = "if (stateManager != null) {"; 213 bodies[i++] = (new StringBuffer (" ")) 214 .append("stateManager.prepareGetField(").append(fieldNumber) 215 .append(");").toString(); 216 bodies[i++] = "}"; 217 bodies[i++] = "return " + fieldName + JavaClassWriterHelper.delim_; 218 return bodies; 219 } 220 221 static String [] getFieldCheckReadImpl(String fieldName, 222 String fieldType, 223 int fieldNumber) 224 { 225 String [] bodies = new String [5]; 226 int i = 0; 227 fieldType = normalizeClassName(fieldType); 228 bodies[i++] = "// annotation: check read access"; 229 bodies[i++] = (new StringBuffer ("if (")) 230 .append(FIELDNAME_JDO_FLAGS).append(" > 0) {").toString(); 231 bodies[i++] = " " + FIELDNAME_JDO_STATE_MANAGER + ".loadForRead();"; 232 bodies[i++] = "}"; 233 bodies[i++] = "return " + fieldName + JavaClassWriterHelper.delim_; 234 return bodies; 235 } 236 237 static String [] getFieldDirectWriteImpl(String fieldName, 238 String fieldType, 239 int fieldNumber, 240 String newvalue) 241 { 242 fieldType = normalizeClassName(fieldType); 243 return new String [] { 244 "// annotation: grant direct write access", 245 (new StringBuffer ("this.")) 246 .append(fieldName).append(" = ").append(newvalue) 247 .append(JavaClassWriterHelper.delim_).toString() 248 }; 249 } 250 251 static String [] getFieldMediateWriteImpl(String fieldName, 252 String fieldType, 253 int fieldNumber, 254 String newvalue) 255 { 256 String [] bodies = new String [7]; 257 int i = 0; 258 fieldType = normalizeClassName(fieldType); 259 bodies[i++] = "// annotation: mediate write access"; 260 bodies[i++] = (new StringBuffer ("final ")) 261 .append(CLASSNAME_JDO_STATE_MANAGER) 262 .append(" stateManager = this.") 263 .append(FIELDNAME_JDO_STATE_MANAGER) 264 .append(JavaClassWriterHelper.delim_).toString(); 265 bodies[i++] = "if (stateManager == null) {"; 266 bodies[i++] = (new StringBuffer (" this.")) 267 .append(fieldName).append(" = ") 268 .append(newvalue).append(JavaClassWriterHelper.delim_) 269 .toString(); 270 bodies[i++] = "} else {"; 271 bodies[i++] = (new StringBuffer (" stateManager.")) 272 .append(getMethodNameSetField(fieldType)).append('(') 273 .append(fieldNumber).append(", ").append(newvalue) 274 .append(");").toString(); 275 bodies[i++] = "}"; 276 return bodies; 277 } 278 279 static String [] getFieldCheckWriteImpl(String fieldName, 280 String fieldType, 281 int fieldNumber, 282 String newvalue) 283 { 284 String [] bodies = new String [5]; 285 int i = 0; 286 fieldType = normalizeClassName(fieldType); 287 bodies[i++] = "// annotation: check write access"; 288 bodies[i++] = "if (" + FIELDNAME_JDO_FLAGS + " != 0) {"; 289 bodies[i++] = " " + FIELDNAME_JDO_STATE_MANAGER + ".loadForUpdate();"; 290 bodies[i++] = "}"; 291 bodies[i++] = (new StringBuffer ("this.")).append(fieldName) 292 .append(" = ").append(newvalue) 293 .append(JavaClassWriterHelper.delim_).toString(); 294 return bodies; 295 } 296 297 static String [] getJDOClearImpl(String className, ExtendedJDOMetaData meta, 298 String [] fieldNames, String [] fieldTypes) { 299 final List impl = new ArrayList (20); 300 for (int i = 0; i < fieldNames.length; i++) { 301 String fieldTypeClassPath = fieldTypes[i]; 302 String fieldType = normalizeClassName(fieldTypes[i]); 303 String fieldName = fieldNames[i]; 304 if (meta.isKeyField(className, fieldName)) { 305 continue; 306 } 307 String primClass = JavaTypeHelper.getWrapperName(fieldType); 308 if (meta.isMutableSecondClassObjectType(fieldTypeClassPath)) { 309 impl.add((new StringBuffer ("if (")) 310 .append(fieldName).append(" instanceof ") 311 .append(CLASSNAME_JDO_SCO).append(") {") 312 .toString()); 313 impl.add((new StringBuffer (" ((")) 314 .append(CLASSNAME_JDO_SCO).append(")") 315 .append(fieldName).append(").unsetOwner();") 316 .toString()); 317 impl.add("}"); 318 impl.add(fieldName + " = null;"); 319 } else if (primClass == null) { 320 impl.add(fieldName + " = null;"); 321 } else if (JavaClassWriterHelper.boolean_.equals(fieldType)) { 322 impl.add(fieldName + " = false;"); 323 } else { 324 impl.add(fieldName + " = 0;"); 325 } 326 } 327 String [] strArr = new String [impl.size()]; 328 return (String [])impl.toArray(strArr); 329 } 330 331 static String [] getJDOGetFieldImpl(String fieldNumber, 332 String [] fieldNames, 333 String [] fieldTypes) 334 { 335 final List impl = new ArrayList (20); 336 impl.add("switch (" + fieldNumber + ") {"); 337 for (int i = 0; i < fieldNames.length; i++) { 338 String fieldType = normalizeClassName(fieldTypes[i]); 339 impl.add("case " + i + ':'); 340 String primClass = JavaTypeHelper.getWrapperName(fieldType); 341 if (primClass == null) { 342 impl.add(" return " + fieldNames[i] + ";"); 343 } else { 344 impl.add((new StringBuffer (" return new ")) 345 .append(primClass).append("(") 346 .append(fieldNames[i]).append(");").toString()); 347 } 348 } 349 impl.add("default:"); 350 impl.add(" throw new " + CLASSNAME_JDO_FATAL_EXCEPTION + "();"); 351 impl.add("}"); 352 String [] strArr = new String [impl.size()]; 353 return (String [])impl.toArray(strArr); 354 } 355 356 static String [] getJDOSetFieldImpl(String fieldNumber, String objName, 357 String [] fieldNames, 358 String [] fieldTypes) 359 { 360 final List impl = new ArrayList (20); 361 impl.add("switch (" + fieldNumber + ") {"); 362 for (int i = 0; i < fieldNames.length; i++) { 363 String fieldType = normalizeClassName(fieldTypes[i]); 364 impl.add("case " + i + ':'); 365 String primClass = JavaTypeHelper.getWrapperName(fieldType); 366 if (primClass == null) { 367 impl.add((new StringBuffer (" this.")) 368 .append(fieldNames[i]) 369 .append(" = (").append(fieldType) 370 .append(")").append(objName) 371 .append(";").toString()); 372 } else { 373 impl.add((new StringBuffer (" this.")) 374 .append(fieldNames[i]) 375 .append(" = ((").append(primClass) 376 .append(")").append(objName).append(").") 377 .append(((String )typeNameConversion.get(fieldType)).toLowerCase()) 378 .append("Value();").toString()); 379 } 380 impl.add(" return;"); 381 } 382 impl.add("default:"); 383 impl.add(" throw new " + CLASSNAME_JDO_FATAL_EXCEPTION + "();"); 384 impl.add("}"); 385 String [] strArr = new String [impl.size()]; 386 return (String [])impl.toArray(strArr); 387 } 388 389 private static String [] getJDOStateManagerDelegationImpl( 391 String delegation, String returnType) { 392 final List impl = new ArrayList (7); 393 impl.add((new StringBuffer ("final ")) 394 .append(CLASSNAME_JDO_STATE_MANAGER) 395 .append(" stateManager = this.") 396 .append(FIELDNAME_JDO_STATE_MANAGER) 397 .append(JavaClassWriterHelper.delim_) 398 .toString()); 399 impl.add("if (stateManager != null) {"); 400 StringBuffer buf = new StringBuffer (" "); 401 if (returnType != null) { 402 buf.append("return "); 403 } 404 impl.add(((buf.append("stateManager.")) 405 .append(delegation).append(";")).toString()); 406 impl.add("}"); 407 if (returnType != null) { 408 impl.add((new StringBuffer ("return ")).append(returnType) 409 .append(";").toString()); 410 }; 411 String [] strArr = new String [impl.size()]; 412 return (String [])impl.toArray(strArr); 413 } 414 415 static String [] getJDOStateManagerVoidDelegationImpl(String delegation) 416 { 417 return getJDOStateManagerDelegationImpl(delegation, null); 418 } 419 420 static String [] getJDOStateManagerObjectDelegationImpl(String delegation) 421 { 422 return getJDOStateManagerDelegationImpl(delegation, 423 JavaClassWriterHelper.null_); 424 } 425 426 static String [] getJDOStateManagerBooleanDelegationImpl(String delegation) 427 { 428 return getJDOStateManagerDelegationImpl(delegation, 429 JavaClassWriterHelper.false_); 430 } 431 432 static String [] getOidHashCodeImpl(String [] pknames, 433 String [] pktypes, 434 boolean isRoot) 435 { 436 final List impl = new ArrayList (3); 437 if (isRoot) { 438 impl.add("int hash = 0;"); 439 } else { 440 impl.add("int hash = super.hashCode();"); 441 } 442 for (int i = 0; i < pknames.length; i++) { 443 if (isPrimitiveClass(pktypes[i])) { 444 if (pktypes[i].equals("boolean")) { 445 impl.add("hash += (" + pknames[i] + " ? 1 : 0);"); 446 } else { 447 impl.add("hash += (int)" + pknames[i] + 448 JavaClassWriterHelper.delim_); 449 } 450 } else { 451 impl.add("hash += (this." + pknames[i] 452 + " != null ? this." + pknames[i] 453 + ".hashCode() : 0);"); 454 } 455 } 456 impl.add("return hash;"); 457 String [] strArr = new String [impl.size()]; 458 return (String [])impl.toArray(strArr); 459 } 460 461 static String [] getOidEqualsImpl(String oidClassName, 462 String [] pknames, 463 String [] pktypes, 464 String pk, 465 boolean isRoot) 466 { 467 final List impl = new ArrayList (31); 468 if (isRoot) { 469 impl.add("if (" + pk + " == null || !this.getClass().equals(" 470 + pk + ".getClass())) {"); 471 } else { 472 impl.add("if (!super.equals(" + pk + ")) {"); 473 } 474 impl.add(" return false;"); 475 impl.add("}"); 476 oidClassName = getClassName(oidClassName); 477 impl.add(oidClassName + " oid = (" + oidClassName + ")" + pk + 478 JavaClassWriterHelper.delim_); 479 for (int i = 0; i < pknames.length; i++) { 480 if (isPrimitiveClass(pktypes[i])) { 481 impl.add("if (this." + pknames[i] + " != oid." 482 + pknames[i] + ") return false;"); 483 } else { 484 impl.add("if (this." + pknames[i] + " != oid." 485 + pknames[i] + " && (this." + pknames[i] 486 + " == null || " + "!this." + pknames[i] 487 + ".equals(oid." + pknames[i] 488 + "))) return false;"); 489 } 490 } 491 impl.add("return true;"); 492 String [] strArr = new String [impl.size()]; 493 return (String [])impl.toArray(strArr); 494 } 495 496 497 502 static String normalizeClassName(String className) 503 { 504 if (className == null) { 505 return null; 506 } 507 return className.replace('/', '.').replace('$', '.'); 508 } 509 510 514 static String getPackageName(String className) 515 { 516 if (className == null) { 517 return null; 518 } 519 return JavaTypeHelper.getPackageName(className.replace('/', '.')); 520 } 521 522 526 static String getClassName(String className) { 527 return JavaTypeHelper.getShortClassName(normalizeClassName(className)); 528 } 529 } 530 | Popular Tags |