1 2 12 package com.versant.core.metadata.generator; 13 14 import com.versant.core.common.*; 15 import com.versant.core.util.FastParser; 16 import com.versant.core.util.OIDObjectOutput; 17 import com.versant.core.util.OIDObjectInput; 18 import com.versant.core.metadata.*; 19 import com.versant.core.metadata.*; 20 import com.versant.core.compiler.ClassSpec; 21 23 import java.util.ArrayList ; 24 import java.util.HashMap ; 25 import java.io.*; 26 27 30 public abstract class OIDSrcGenerator { 31 32 protected final ModelMetaData jmd; 33 protected ClassMetaData cmd; 34 protected String className; 35 protected ClassSpec spec; 36 protected ClassMetaData currentCMD = null; 37 38 protected final String CLASS_INDEX = "classIndex"; 39 protected final String RESOLVED = "resolved"; 40 41 protected HashMap typeToResultSetGetField; 42 protected HashMap typeToPreparedStatementSetField; 43 44 protected HashMap primitiveToWrapperTypes; 45 protected HashMap primitiveTypesToValue; 46 protected HashMap wrapperTypesToValue; 47 protected HashMap wrapperTypesToPrimitive; 48 49 protected String stateClassName; 50 51 public static final String JDBC_CONVERTER_FIELD_PREFIX = "jdbcConverter_"; 52 53 protected HashMap primClassToSerReadMethod; 54 protected HashMap primClassToSerWriteMethod; 55 56 public OIDSrcGenerator(ModelMetaData jmd) { 57 this.jmd = jmd; 58 59 wrapperTypesToValue = new HashMap (8); 60 wrapperTypesToValue.put(int.class, "intValue"); 61 wrapperTypesToValue.put(byte.class, "byteValue"); 62 wrapperTypesToValue.put(char.class, "charValue"); 63 wrapperTypesToValue.put(short.class, "shortValue"); 64 wrapperTypesToValue.put(float.class, "floatValue"); 65 wrapperTypesToValue.put(double.class, "doubleValue"); 66 wrapperTypesToValue.put(long.class, "longValue"); 67 wrapperTypesToValue.put(boolean.class, "booleanValue"); 68 69 70 primitiveToWrapperTypes = new HashMap (8); 71 primitiveToWrapperTypes.put(int.class, "Integer"); 72 primitiveToWrapperTypes.put(byte.class, "Byte"); 73 primitiveToWrapperTypes.put(char.class, "Character"); 74 primitiveToWrapperTypes.put(short.class, "Short"); 75 primitiveToWrapperTypes.put(float.class, "Float"); 76 primitiveToWrapperTypes.put(double.class, "Double"); 77 primitiveToWrapperTypes.put(long.class, "Long"); 78 primitiveToWrapperTypes.put(boolean.class, "Boolean"); 79 80 81 82 wrapperTypesToPrimitive = new HashMap (8); 83 wrapperTypesToPrimitive.put(Integer .class, int.class); 84 wrapperTypesToPrimitive.put(Byte .class, byte.class); 85 wrapperTypesToPrimitive.put(Character .class, char.class); 86 wrapperTypesToPrimitive.put(Short .class, short.class); 87 wrapperTypesToPrimitive.put(Float .class, float.class); 88 wrapperTypesToPrimitive.put(Double .class, double.class); 89 wrapperTypesToPrimitive.put(Long .class, long.class); 90 wrapperTypesToPrimitive.put(Boolean .class, boolean.class); 91 92 primitiveTypesToValue = new HashMap (8); 93 primitiveTypesToValue.put(int.class, "intValue"); 94 primitiveTypesToValue.put(byte.class, "byteValue"); 95 primitiveTypesToValue.put(char.class, "charValue"); 96 primitiveTypesToValue.put(short.class, "shortValue"); 97 primitiveTypesToValue.put(float.class, "floatValue"); 98 primitiveTypesToValue.put(double.class, "doubleValue"); 99 primitiveTypesToValue.put(long.class, "longValue"); 100 primitiveTypesToValue.put(boolean.class, "booleanValue"); 101 102 103 typeToResultSetGetField = new HashMap (); 104 typeToResultSetGetField.put(int.class, "getInt"); 105 typeToResultSetGetField.put(byte.class, "getByte"); 106 typeToResultSetGetField.put(short.class, "getShort"); 107 typeToResultSetGetField.put(float.class, "getFloat"); 108 typeToResultSetGetField.put(double.class, "getDouble"); 109 typeToResultSetGetField.put(long.class, "getLong"); 110 typeToResultSetGetField.put(boolean.class, "getBoolean"); 111 typeToResultSetGetField.put(String .class, "getString"); 112 typeToResultSetGetField.put(byte[].class, "getBytes"); 113 typeToResultSetGetField.put(java.math.BigDecimal .class, "getBigDecimal"); typeToResultSetGetField.put(java.util.Date .class, "getDate"); 115 typeToResultSetGetField.put(java.sql.Time .class, "getTime"); 116 typeToResultSetGetField.put(java.sql.Timestamp .class, "getTimestamp"); 117 typeToResultSetGetField.put(java.io.InputStream .class, "getAsciiStream"); 118 typeToResultSetGetField.put(java.io.InputStream .class, "getBinaryStream"); 119 120 121 typeToPreparedStatementSetField = new HashMap (); 122 typeToPreparedStatementSetField.put(int.class, "setInt"); 123 typeToPreparedStatementSetField.put(byte.class, "setByte"); 124 typeToPreparedStatementSetField.put(short.class, "setShort"); 125 typeToPreparedStatementSetField.put(float.class, "setFloat"); 126 typeToPreparedStatementSetField.put(double.class, "setDouble"); 127 typeToPreparedStatementSetField.put(long.class, "setLong"); 128 typeToPreparedStatementSetField.put(boolean.class, "setBoolean"); 129 typeToPreparedStatementSetField.put(String .class, "setString"); 130 typeToPreparedStatementSetField.put(Object .class, "setObject"); 131 typeToPreparedStatementSetField.put(byte[].class, "setBytes"); 132 typeToPreparedStatementSetField.put(java.math.BigDecimal .class, "setBigDecimal"); 133 typeToPreparedStatementSetField.put(java.util.Date .class, "setDate"); 134 typeToPreparedStatementSetField.put(java.sql.Time .class, "setTime"); 135 typeToPreparedStatementSetField.put(java.sql.Timestamp .class, "setTimestamp"); 136 typeToPreparedStatementSetField.put(java.io.InputStream .class, "setAsciiStream"); 137 typeToPreparedStatementSetField.put(java.io.InputStream .class, "setBinaryStream"); 138 139 primClassToSerReadMethod = new HashMap (8); 140 primClassToSerReadMethod.put(int.class, "readInt"); 141 primClassToSerReadMethod.put(byte.class, "readByte"); 142 primClassToSerReadMethod.put(char.class, "readChar"); 143 primClassToSerReadMethod.put(short.class, "readShort"); 144 primClassToSerReadMethod.put(float.class, "readFloat"); 145 primClassToSerReadMethod.put(double.class, "readDouble"); 146 primClassToSerReadMethod.put(long.class, "readLong"); 147 primClassToSerReadMethod.put(boolean.class, "readBoolean"); 148 149 primClassToSerWriteMethod = new HashMap (8); 150 primClassToSerWriteMethod.put(int.class, "writeInt"); 151 primClassToSerWriteMethod.put(byte.class, "writeByte"); 152 primClassToSerWriteMethod.put(char.class, "writeChar"); 153 primClassToSerWriteMethod.put(short.class, "writeShort"); 154 primClassToSerWriteMethod.put(float.class, "writeFloat"); 155 primClassToSerWriteMethod.put(double.class, "writeDouble"); 156 primClassToSerWriteMethod.put(long.class, "writeLong"); 157 primClassToSerWriteMethod.put(boolean.class, "writeBoolean"); 158 } 159 160 public ClassSpec generateOID(ClassMetaData cmd) { 161 this.cmd = cmd; 162 this.className = cmd.oidClassName; 163 this.stateClassName = cmd.stateClassName; 164 spec = new ClassSpec(null, className, null); 165 166 spec.addImportsForJavaLang(); 167 spec.addImport(OID.class.getName()); 168 spec.addImport(State.class.getName()); 169 spec.addImport(ClassMetaData.class.getName()); 170 spec.addImport(ModelMetaData.class.getName()); 171 spec.addImport(ObjectOutput.class.getName()); 172 spec.addImport(ObjectInput.class.getName()); 173 spec.addImport(DataInputStream.class.getName()); 174 spec.addImport(DataOutputStream.class.getName()); 175 spec.addImport(IOException.class.getName()); 176 spec.addImport(BindingSupportImpl.class.getName()); 177 spec.addImport(FastParser.class.getName()); 178 spec.addImport(SerUtils.class.getName()); 179 spec.addImport(OIDObjectOutput.class.getName()); 180 spec.addImport(OIDObjectInput.class.getName()); 181 spec.addImport(Externalizable.class.getName()); 182 184 spec.addInterface("OID"); 185 spec.addInterface("Externalizable"); 186 187 currentCMD = cmd.top; 188 189 addFields(); 190 addInitStatics(); 191 addDefaultConstructor(); addConstructor(); addCopyKeyFieldsObjects(); 194 addEqualsObject(); 195 addHashCode(); 196 addReadExternal(); addWriteExternal(); addReadExternalFast(); addWriteExternalFast(); addCopyKeyFieldsFromState(); 201 addCopyKeyFieldsUpdate(); 202 addIsResolved(); addResolve(); addGetClassIndex(); addGetAvailableClassMetaData(); addGetBaseClassMetaData(); addGetClassMetaData(); addGetIdentityType(); addGetCopy(); addfillFromPK(); 211 addToPKString(); 212 addToSString(); 213 addToString(); 214 addToStringImp(); 215 addFillFromIDString2(); 216 addGetCompareClassIndex(); 217 addCompareTo(); 218 addVersion(); 219 addGetLongPrimaryKey(); 220 addSetLongPrimaryKey(); 221 addPopulateObjectIdClassInstance(); 222 addIsNewEtc(); 223 addGetAvailableClassId(); 224 addFillFromIDObject(); 225 226 return spec; 227 } 228 229 protected void addIsNewEtc() { 230 spec.addMethod( 231 "public boolean isNew() {\n" + 232 " return false;\n" + 233 "}"); 234 spec.addMethod( 235 "public OID getMappedOID() {\n" + 236 " return this;\n" + 237 "}"); 238 spec.addMethod( 239 "public OID getRealOID() {\n" + 240 " return this;\n" + 241 "}"); 242 spec.addMethod( 243 "public OID getAvailableOID() {\n" + 244 " return this;\n" + 245 "}"); 246 } 247 248 protected void addPopulateObjectIdClassInstance() { 249 StringBuffer buf = new StringBuffer (); 250 buf.append("\n\tpublic void populateObjectIdClassInstance(Object o) {\n"); 251 buf.append("\t}\n"); 252 spec.addMethod(buf.toString()); 253 } 254 255 protected abstract void addGetLongPrimaryKey(); 256 257 protected abstract void addSetLongPrimaryKey(); 258 259 protected void addVersion() { 260 StringBuffer buf = new StringBuffer (); 261 buf.append("\n\tpublic String getVersion() {\n"); 262 buf.append("\t\treturn \""+ Debug.VERSION +"\";\n\t}\n"); 263 spec.addMethod(buf.toString()); 264 } 265 266 protected abstract void addfillFromPK(); 267 268 protected void addGetBaseClassMetaData() { 269 StringBuffer buf = new StringBuffer (); 270 buf.append("\n\tpublic ClassMetaData getBaseClassMetaData() {\n"); 271 if (cmd.isInHeirachy()) { 272 buf.append("\t\treturn top;\n"); 273 } else { 274 buf.append("\t\treturn cmd;\n"); 275 } 276 buf.append("\t}\n"); 277 spec.addMethod(buf.toString()); 278 } 279 280 protected void addGetAvailableClassMetaData() { 281 StringBuffer buf = new StringBuffer (); 282 buf.append("\n\tpublic ClassMetaData getAvailableClassMetaData() {\n"); 283 buf.append("\t\treturn cmd;\n"); 284 buf.append("\t}\n"); 285 spec.addMethod(buf.toString()); 286 } 287 288 protected void addGetAvailableClassId() { 289 StringBuffer buf = new StringBuffer (); 290 buf.append("\n\tpublic int getAvailableClassId() {\n"); 291 if (cmd.isInHeirachy()) { 292 buf.append("\t\treturn cmd.classId;\n"); 293 } else { 294 buf.append("\t\treturn " + cmd.classId + ";\n"); 295 } 296 buf.append("\t}\n"); 297 spec.addMethod(buf.toString()); 298 } 299 300 protected void addGetClassMetaData() { 301 StringBuffer buf = new StringBuffer (); 302 buf.append("\n\tpublic ClassMetaData getClassMetaData() {\n"); 303 if (cmd.isInHeirachy()) { 304 buf.append( 305 "\t\tif (!resolved) {\n" + 306 "\t\t\tthrow BindingSupportImpl.getInstance().internal(\n" + 307 "\t\t\t\t\"Called 'getClassMetaData()' on unresolved oid\");\n" + 308 "\t\t}\n"); 309 } 310 buf.append("\t\treturn cmd;\n"); 311 buf.append("\t}\n"); 312 spec.addMethod(buf.toString()); 313 } 314 315 protected void addGetClassIndex() { 316 StringBuffer buf = new StringBuffer (); 317 buf.append("\n\tpublic int getClassIndex() {\n"); 318 if (cmd.isInHeirachy()) { 319 buf.append("\t\treturn cmd.index;\n"); 320 } else { 321 buf.append("\t\treturn "+ cmd.index); 322 buf.append(";\n"); 323 } 324 buf.append("\t}\n"); 325 spec.addMethod(buf.toString()); 326 } 327 328 protected void addGetCompareClassIndex() { 329 StringBuffer buf = new StringBuffer (); 330 335 buf.append("\n\tpublic int getCompareClassIndex() {\n"); 336 buf.append("\t\treturn "+ getIndex() +";\n\t}\n"); 337 spec.addMethod(buf.toString()); 338 } 339 340 protected abstract void addCompareTo(); 341 342 protected int getIndex() { 343 if (cmd.isInHeirachy()) { 344 return cmd.pcHeirachy[0].index; 345 } else { 346 return cmd.index; 347 } 348 } 349 350 351 protected void addResolve() { 352 StringBuffer buf = new StringBuffer (); 353 buf.append("\n\tpublic void resolve(State state) {\n"); 354 if (cmd.isInHeirachy()) { 355 buf.append("\t\tif (!resolved) {\n"); 356 buf.append("\t\t\tcmd = state.getClassMetaData();\n"); 357 buf.append("\t\t\tresolved = true;\n"); 358 buf.append("\t\t}\n"); 359 } 360 buf.append("\t}\n"); 361 spec.addMethod(buf.toString()); 362 } 363 364 365 protected void addIsResolved() { 366 StringBuffer buf = new StringBuffer (); 367 372 buf.append("\n\tpublic boolean isResolved() {\n"); 373 if (cmd.isInHeirachy()) { 374 buf.append("\t\treturn resolved;\n"); 375 } else { 376 buf.append("\t\treturn true;\n"); 377 } 378 buf.append("\t}\n"); 379 spec.addMethod(buf.toString()); 380 } 381 382 protected abstract void addToPKString(); 383 384 protected abstract void addToSString(); 385 386 protected ClassMetaData[] getAllSubClasses(ClassMetaData topCMD) { 387 ArrayList list = new ArrayList (); 388 ClassMetaData[] cmds = jmd.classes; 389 for (int i = 0; i < cmds.length; i++) { 390 ClassMetaData cmd = cmds[i]; 391 if (cmd.top.index == topCMD.index) { 392 list.add(cmd); 393 } 394 } 395 ClassMetaData[] allCMDs = new ClassMetaData[list.size()]; 396 list.toArray(allCMDs); 397 return allCMDs; 398 } 399 400 protected void addFillFromIDString2() { 401 StringBuffer buf = new StringBuffer (); 402 407 buf.append("\n\tpublic void fillFromIDString(String idString, int index) {\n"); 408 409 if (cmd.identityType == MDStatics.IDENTITY_TYPE_APPLICATION) { 410 buf.append("\t\tthrow new javax.jdo.JDOFatalInternalException(\"fillFromIDString() called on applicationId class.\");"); 411 } else if (cmd.identityType == MDStatics.IDENTITY_TYPE_DATASTORE) { 412 switch (cmd.datastoreIdentityTypeCode) { 413 case MDStatics.INT: 414 buf.append("\t\t_0 = FastParser.parseInt(idString, index);"); 415 break; 416 case MDStatics.LONG: 417 buf.append("\t\t_0 = FastParser.parseLong(idString, index);"); 418 break; 419 default: 420 throw BindingSupportImpl.getInstance().runtime( 421 "Unhandled java type code " + cmd.datastoreIdentityTypeCode); 422 } 423 } 424 buf.append("\t}\n"); 425 spec.addMethod(buf.toString()); 426 } 427 428 protected void addToStringImp() { 429 StringBuffer buf = new StringBuffer (); 430 436 buf.append("\n\tpublic String toStringImp() {\n"); 437 if (!currentCMD.isInHeirachy()) { 438 buf.append("\t\treturn toString();\n"); 439 } else { 440 444 buf.append("\t\tif (!resolved) return \"null pk\";\n"); 445 buf.append("\t\telse return this.toString();\n"); 446 } 447 buf.append("\t}\n"); 448 spec.addMethod(buf.toString()); 449 } 450 451 protected abstract void addToString(); 452 453 protected void addGetIdentityType() { 454 StringBuffer buf = new StringBuffer (); 455 buf.append("\n\tpublic int getIdentityType() {\n"); 456 buf.append("\t\treturn "+ cmd.identityType + ";\n"); 457 buf.append("\t}\n"); 458 spec.addMethod(buf.toString()); 459 } 460 461 protected abstract void addGetCopy(); 462 463 protected abstract void addCopyKeyFieldsUpdate(); 464 465 protected abstract void addCopyKeyFieldsFromState(); 466 467 protected void addReadExternal() { 468 StringBuffer buf = new StringBuffer (); 469 buf.append("\n\tpublic void readExternal(ObjectInput is) throws ClassNotFoundException, IOException {\n"); 470 if (cmd.isInHeirachy()) { 471 buf.append("\t\tcmd = top.jmd.classes[is.readShort()];\n"); 472 buf.append("\t\tresolved = is.readBoolean();\n"); 473 } 474 addReadExternalImp(buf); 475 buf.append("\t}\n"); 476 spec.addMethod(buf.toString()); 477 } 478 479 protected void addWriteExternal() { 480 StringBuffer buf = new StringBuffer (); 481 buf.append("\n\tpublic void writeExternal(ObjectOutput os) throws IOException {\n"); 482 if (cmd.isInHeirachy()) { 483 buf.append("\t\tos.writeShort(cmd.index);\n"); 484 buf.append("\t\tos.writeBoolean(resolved);\n"); 485 } 486 addWriteExternalImp(buf); 487 buf.append("\t}\n"); 488 spec.addMethod(buf.toString()); 489 } 490 491 protected void addReadExternalFast() { 492 StringBuffer buf = new StringBuffer (); 493 buf.append("\n\tpublic void readExternal(OIDObjectInput is) throws ClassNotFoundException, IOException {\n"); 494 addReadExternalImp(buf); 495 buf.append("\t}\n"); 496 spec.addMethod(buf.toString()); 497 } 498 499 protected void addWriteExternalFast() { 500 StringBuffer buf = new StringBuffer (); 501 buf.append("\n\tpublic void writeExternal(OIDObjectOutput os) throws IOException {\n"); 502 addWriteExternalImp(buf); 503 buf.append("\t}\n"); 504 spec.addMethod(buf.toString()); 505 } 506 507 protected void addReadExternalImp(StringBuffer buf) { 508 Class bigIntegerType = java.math.BigInteger .class; 509 boolean isBigInteger = false; FieldMetaData[] pkc = currentCMD.pkFields; 511 if (pkc != null) { 512 for (int i = 0; i < pkc.length; i++) { 513 if (pkc[i].type.equals(bigIntegerType)) { 514 isBigInteger = true; 515 } 516 } 517 } 518 if (isBigInteger) { 519 buf.append("\t\tbyte bytes[] = null;\n"); 520 } 521 int n = pkc == null ? 1 : pkc.length; 522 for (int i = 0; i < n; i++) { 523 Class type = pkc == null ? currentCMD.datastoreIdentityType : pkc[i].type; 524 if (type.isPrimitive()) { buf.append("\t\t_" + i + " = is." + primClassToSerReadMethod.get(type) + "();\n"); 526 } else if (type.equals(String .class)) { 527 buf.append("\t\t_" + i + " = com.versant.core.common.Utils.readLongUTF8(is);\n"); 528 } else if (type.equals(java.util.Locale .class)) { 529 buf.append("\t\t_" + i + " = new java.util.Locale(is.readUTF(), is.readUTF(), is.readUTF());\n"); 530 } else if (type.equals(java.math.BigDecimal .class)) { 531 buf.append("\t\t_" + i + " = new java.math.BigDecimal(is.readUTF());\n"); 532 } else if (type.equals(java.math.BigInteger .class)) { 533 buf.append("\t\tbytes = new byte[is.readInt()];\n"); 534 buf.append("\t\tis.readFully(bytes);\n"); 535 buf.append("\t\t_" + i + " = new java.math.BigInteger(bytes);\n"); 536 } else if (type.equals(java.util.Date .class)) { 537 buf.append("\t\t_" + i + " = new java.util.Date(is.readLong());\n"); 538 } else if (type.equals(java.net.URL .class)) { 539 buf.append("\t\t_" + i + " = new java.net.URL(is.readUTF());\n"); 540 } else if (type.equals(File.class)) { 541 buf.append("\t\t_" + i + " = new java.io.File(is.readUTF());\n"); 542 } else if (type.equals(java.sql.Timestamp .class)) { 543 buf.append("\t\t_" + i + " = new java.sql.Timestamp(is.readLong());\n"); 544 } else if (wrapperTypesToPrimitive.containsKey(type)) { buf.append("\t\t_" + i + " = new " + type.getName() + "(is." + primClassToSerReadMethod.get(wrapperTypesToPrimitive.get(type)) + "());\n"); 546 } else { 547 buf.append("\t\t_" + i + " = (" + type.getName() + ")SerUtils.readObject(is);\n"); 548 } 549 } 550 } 551 552 protected void addWriteExternalImp(StringBuffer buf) { 553 Class bigIntegerType = java.math.BigInteger .class; 554 boolean isBigInteger = false; FieldMetaData[] pkc = currentCMD.pkFields; 556 if (pkc != null) { 557 for (int i = 0; i < pkc.length; i++) { 558 if (pkc[i].type.equals(bigIntegerType)) { 559 isBigInteger = true; 560 } 561 } 562 } 563 if (isBigInteger) { 564 buf.append("\t\tbyte bytes[] = null;\n"); 565 } 566 int n = pkc == null ? 1 : pkc.length; 567 for (int i = 0; i < n; i++) { 568 Class type = pkc == null ? currentCMD.datastoreIdentityType : pkc[i].type; 569 if (type.isPrimitive()) { buf.append("\t\tos." + primClassToSerWriteMethod.get(type) + "(_" + i + ");\n"); 571 } else if (type.equals(String .class)) { 572 buf.append("\t\tcom.versant.core.common.Utils.writeLongUTF8(_" + i + ", os);\n"); 573 } else if (type.equals(java.util.Locale .class)) { 574 buf.append("\t\tos.writeUTF(_" + i + ".getLanguage());\n"); 575 buf.append("\t\tos.writeUTF(_" + i + ".getCountry());\n"); 576 buf.append("\t\tos.writeUTF(_" + i + ".getVariant());\n"); 577 } else if (type.equals(java.math.BigDecimal .class)) { 578 buf.append("\t\tos.writeUTF(_" + i + ".toString());\n"); 579 } else if (type.equals(java.math.BigInteger .class)) { 580 buf.append("\t\tbytes = _" + i + ".toByteArray();\n"); 581 buf.append("\t\tos.writeInt(bytes.length);\n"); 582 buf.append("\t\tos.write(bytes);\n"); 583 } else if (type.equals(java.util.Date .class)) { 584 buf.append("\t\tos.writeLong(_" + i + ".getTime());\n"); 585 } else if (type.equals(java.net.URL .class)) { 586 buf.append("\t\tos.writeUTF(_" + i + ".toString());\n"); 587 } else if (type.equals(File.class)) { 588 buf.append("\t\tos.writeUTF(_" + i + ".toString());\n"); 589 } else if (type.equals(java.sql.Timestamp .class)) { 590 buf.append("\t\tos.writeLong(_" + i + ".getTime());\n"); 591 } else if (wrapperTypesToPrimitive.containsKey(type)) { buf.append("\t\tos." + 593 primClassToSerWriteMethod.get(wrapperTypesToPrimitive.get(type)) + 594 "(_" + i + "." + wrapperTypesToValue.get(wrapperTypesToPrimitive.get(type)) + "());\n"); 595 } else { 596 buf.append("\t\tSerUtils.writeObject(_" + i + ", os);\n"); 597 } 598 } 599 } 600 601 protected abstract void addEqualsObject(); 602 603 protected abstract void addHashCode(); 604 605 609 protected void addDefaultConstructor() { 610 StringBuffer buf = new StringBuffer (); 611 buf.append("\n\tpublic "); 612 buf.append(className); 613 buf.append("(){}\n"); 614 spec.addMethod(buf.toString()); 615 } 616 617 622 protected void addConstructor() { 623 StringBuffer buf = new StringBuffer (); 624 if (cmd.isInHeirachy()) { 625 buf.append("\n\tpublic "); 626 buf.append(className); 627 buf.append("(ClassMetaData cmd, boolean resolved){\n"); 628 buf.append("\t\tthis.cmd = cmd;\n"); 629 buf.append("\t\tthis.resolved = resolved;\n\t}\n"); 630 } 631 spec.addMethod(buf.toString()); 632 } 633 634 protected ClassMetaData tempClassMetaData; 635 636 protected ClassMetaData getTopPCSuperClassMetaData() { 637 if (cmd.pcSuperMetaData == null) { 638 return cmd; 639 } else { 640 setTopPCSuperClassMetaData(cmd); 641 } 642 return tempClassMetaData; 643 } 644 645 protected void setTopPCSuperClassMetaData(ClassMetaData currentCMD) { 646 if (currentCMD.pcSuperMetaData == null) { 647 tempClassMetaData = currentCMD; 648 return; 649 } else { 650 setTopPCSuperClassMetaData(currentCMD.pcSuperMetaData); 651 } 652 } 653 654 protected ClassMetaData getCurrentCMD() { 655 if (cmd.identityType == MDStatics.IDENTITY_TYPE_APPLICATION) { 656 return getTopPCSuperClassMetaData(); 657 } else if (cmd.identityType == MDStatics.IDENTITY_TYPE_DATASTORE) { 658 return cmd; 659 } else { 660 throw BindingSupportImpl.getInstance().unsupported(""); 661 } 662 } 663 664 667 protected void addFields() { 668 if (cmd.isInHeirachy()) { 669 spec.addField("public static ClassMetaData top"); 670 spec.addField("public ClassMetaData cmd"); 671 spec.addField("public boolean " + RESOLVED); 672 } else { 673 spec.addField("public static ClassMetaData cmd"); 674 } 675 } 676 677 681 protected void addInitStatics() { 682 StringBuffer buf = new StringBuffer (); 683 buf.append("\tpublic static boolean initStatics(ModelMetaData jmd) {\n"); 684 addInitStaticsBody(buf); 685 buf.append("\t\treturn true;\n"); 686 buf.append("\t}"); 687 spec.addMethod(buf.toString()); 688 } 689 690 protected void addInitStaticsBody(StringBuffer buf) { 691 if (cmd.isInHeirachy()) { 692 buf.append("\t\tif (top != null) return false;\n"); 693 buf.append("\t\ttop = jmd.classes[" + cmd.top.index + "];\n"); 694 } else { 695 buf.append("\t\tif (cmd != null) return false;\n"); 696 buf.append("\t\tcmd = jmd.classes[" + cmd.index + "];\n"); 697 } 698 } 699 700 protected abstract void addCopyKeyFieldsObjects(); 701 702 protected String getFieldName(int num) { 703 return "_" + num; 704 } 705 706 protected void addFillFromIDObject() { 707 StringBuffer buf = new StringBuffer (); 729 buf.append("\n\tpublic OID fillFromIDObject(Object id) {\n"); 730 if (currentCMD.identityType == MDStatics.IDENTITY_TYPE_APPLICATION) { 731 if (currentCMD.objectIdClass == null) { 732 if (currentCMD.pkFields.length != 1) { 733 throw BindingSupportImpl.getInstance().invalidOperation("Class "+ 734 currentCMD.cls.getName() 735 +" with application can only have a single pk " + 736 "field if a ApplicationId class is not specified"); 737 } 738 Class type = currentCMD.pkFields[0].type; 739 if (type.isPrimitive() && !type.isArray()) { buf.append("\t\t_0 = (("+ primitiveToWrapperTypes.get(type) + 741 ")id)."+ wrapperTypesToValue.get(type) +"();\n"); 742 } else { buf.append("\t\t_0 = (" + type.getName() + ")id;\n"); 744 } 745 } else { 746 } 748 } 749 buf.append("\t\treturn this;\n"); 750 buf.append("\t}\n"); 751 spec.addMethod(buf.toString()); 752 753 } 754 755 756 757 758 } 759 760 761 | Popular Tags |