| 1 |
/*- |
| 2 |
* See the file LICENSE for redistribution information. |
| 3 |
* |
| 4 |
* Copyright (c) 2002, 2013 Oracle and/or its affiliates. All rights reserved. |
| 5 |
* |
| 6 |
*/ |
| 7 |
|
| 8 |
package com.sleepycat.persist.model; |
| 9 |
|
| 10 |
import static com.sleepycat.asm.Opcodes.AALOAD; |
| 11 |
import static com.sleepycat.asm.Opcodes.ACC_ABSTRACT; |
| 12 |
import static com.sleepycat.asm.Opcodes.ACC_PRIVATE; |
| 13 |
import static com.sleepycat.asm.Opcodes.ACC_PUBLIC; |
| 14 |
import static com.sleepycat.asm.Opcodes.ACC_STATIC; |
| 15 |
import static com.sleepycat.asm.Opcodes.ACC_TRANSIENT; |
| 16 |
import static com.sleepycat.asm.Opcodes.ACONST_NULL; |
| 17 |
import static com.sleepycat.asm.Opcodes.ALOAD; |
| 18 |
import static com.sleepycat.asm.Opcodes.ANEWARRAY; |
| 19 |
import static com.sleepycat.asm.Opcodes.ARETURN; |
| 20 |
import static com.sleepycat.asm.Opcodes.ASM4; |
| 21 |
import static com.sleepycat.asm.Opcodes.BIPUSH; |
| 22 |
import static com.sleepycat.asm.Opcodes.CHECKCAST; |
| 23 |
import static com.sleepycat.asm.Opcodes.DCMPL; |
| 24 |
import static com.sleepycat.asm.Opcodes.DCONST_0; |
| 25 |
import static com.sleepycat.asm.Opcodes.DUP; |
| 26 |
import static com.sleepycat.asm.Opcodes.FCMPL; |
| 27 |
import static com.sleepycat.asm.Opcodes.FCONST_0; |
| 28 |
import static com.sleepycat.asm.Opcodes.GETFIELD; |
| 29 |
import static com.sleepycat.asm.Opcodes.GOTO; |
| 30 |
import static com.sleepycat.asm.Opcodes.ICONST_0; |
| 31 |
import static com.sleepycat.asm.Opcodes.ICONST_1; |
| 32 |
import static com.sleepycat.asm.Opcodes.ICONST_2; |
| 33 |
import static com.sleepycat.asm.Opcodes.ICONST_3; |
| 34 |
import static com.sleepycat.asm.Opcodes.ICONST_4; |
| 35 |
import static com.sleepycat.asm.Opcodes.ICONST_5; |
| 36 |
import static com.sleepycat.asm.Opcodes.IFEQ; |
| 37 |
import static com.sleepycat.asm.Opcodes.IFGT; |
| 38 |
import static com.sleepycat.asm.Opcodes.IFLE; |
| 39 |
import static com.sleepycat.asm.Opcodes.IFNE; |
| 40 |
import static com.sleepycat.asm.Opcodes.IFNONNULL; |
| 41 |
import static com.sleepycat.asm.Opcodes.IF_ICMPNE; |
| 42 |
import static com.sleepycat.asm.Opcodes.ILOAD; |
| 43 |
import static com.sleepycat.asm.Opcodes.INVOKEINTERFACE; |
| 44 |
import static com.sleepycat.asm.Opcodes.INVOKESPECIAL; |
| 45 |
import static com.sleepycat.asm.Opcodes.INVOKESTATIC; |
| 46 |
import static com.sleepycat.asm.Opcodes.INVOKEVIRTUAL; |
| 47 |
import static com.sleepycat.asm.Opcodes.IRETURN; |
| 48 |
import static com.sleepycat.asm.Opcodes.ISUB; |
| 49 |
import static com.sleepycat.asm.Opcodes.LCMP; |
| 50 |
import static com.sleepycat.asm.Opcodes.LCONST_0; |
| 51 |
import static com.sleepycat.asm.Opcodes.NEW; |
| 52 |
import static com.sleepycat.asm.Opcodes.POP; |
| 53 |
import static com.sleepycat.asm.Opcodes.PUTFIELD; |
| 54 |
import static com.sleepycat.asm.Opcodes.RETURN; |
| 55 |
|
| 56 |
import java.math.BigDecimal; |
| 57 |
import java.math.BigInteger; |
| 58 |
import java.util.ArrayList; |
| 59 |
import java.util.Collections; |
| 60 |
import java.util.Comparator; |
| 61 |
import java.util.Date; |
| 62 |
import java.util.HashMap; |
| 63 |
import java.util.List; |
| 64 |
import java.util.Map; |
| 65 |
|
| 66 |
import com.sleepycat.asm.AnnotationVisitor; |
| 67 |
import com.sleepycat.asm.Attribute; |
| 68 |
import com.sleepycat.asm.ClassVisitor; |
| 69 |
import com.sleepycat.asm.FieldVisitor; |
| 70 |
import com.sleepycat.asm.Label; |
| 71 |
import com.sleepycat.asm.MethodVisitor; |
| 72 |
import com.sleepycat.asm.Type; |
| 73 |
import com.sleepycat.compat.DbCompat; |
| 74 |
|
| 75 |
/** |
| 76 |
* An ASM ClassVisitor that examines a class, throws NotPersistentException if |
| 77 |
* it is not persistent, or enhances it if it is persistent. A class is |
| 78 |
* persistent if it contains the @Entity or @Persistent annotations. A |
| 79 |
* resulting enhanced class implements the com.sleepycat.persist.impl.Enhanced |
| 80 |
* interface. |
| 81 |
* |
| 82 |
* <p>NotPersistentException is thrown to abort the transformation in order to |
| 83 |
* avoid making two passes over the class file (one to look for the annotations |
| 84 |
* and another to enhance the bytecode) or outputing a class that isn't |
| 85 |
* enhanced. By aborting the transformation as soon as we detect that the |
| 86 |
* annotations are missing, we make only one partial pass for a non-persistent |
| 87 |
* class.</p> |
| 88 |
* |
| 89 |
* @author Mark Hayes |
| 90 |
*/ |
| 91 |
class BytecodeEnhancer extends ClassVisitor { |
| 92 |
|
| 93 |
/** Thrown when we determine that a class is not persistent. */ |
| 94 |
@SuppressWarnings("serial") |
| 95 |
static class NotPersistentException extends RuntimeException {} |
| 96 |
|
| 97 |
/** A static instance is used to avoid fillInStaceTrace overhead. */ |
| 98 |
private static final NotPersistentException NOT_PERSISTENT = |
| 99 |
new NotPersistentException(); |
| 100 |
|
| 101 |
private static final Map<String, Integer> PRIMITIVE_WRAPPERS = |
| 102 |
new HashMap<String, Integer>(); |
| 103 |
static { |
| 104 |
PRIMITIVE_WRAPPERS.put(Boolean.class.getName(), Type.BOOLEAN); |
| 105 |
PRIMITIVE_WRAPPERS.put(Character.class.getName(), Type.CHAR); |
| 106 |
PRIMITIVE_WRAPPERS.put(Byte.class.getName(), Type.BYTE); |
| 107 |
PRIMITIVE_WRAPPERS.put(Short.class.getName(), Type.SHORT); |
| 108 |
PRIMITIVE_WRAPPERS.put(Integer.class.getName(), Type.INT); |
| 109 |
PRIMITIVE_WRAPPERS.put(Long.class.getName(), Type.LONG); |
| 110 |
PRIMITIVE_WRAPPERS.put(Float.class.getName(), Type.FLOAT); |
| 111 |
PRIMITIVE_WRAPPERS.put(Double.class.getName(), Type.DOUBLE); |
| 112 |
} |
| 113 |
|
| 114 |
private String className; |
| 115 |
private String superclassName; |
| 116 |
private boolean isPersistent; |
| 117 |
private boolean isAbstract; |
| 118 |
private boolean hasDefaultConstructor; |
| 119 |
private boolean hasPersistentSuperclass; |
| 120 |
private boolean isCompositeKey; |
| 121 |
private FieldInfo priKeyField; |
| 122 |
private List<FieldInfo> secKeyFields; |
| 123 |
private List<FieldInfo> nonKeyFields; |
| 124 |
private String staticBlockMethod; |
| 125 |
|
| 126 |
BytecodeEnhancer(ClassVisitor parentVisitor) { |
| 127 |
super(ASM4, parentVisitor); |
| 128 |
secKeyFields = new ArrayList<FieldInfo>(); |
| 129 |
nonKeyFields = new ArrayList<FieldInfo>(); |
| 130 |
} |
| 131 |
|
| 132 |
@Override |
| 133 |
public void visit(int version, |
| 134 |
int access, |
| 135 |
String name, |
| 136 |
String sig, |
| 137 |
String superName, |
| 138 |
String[] interfaces) { |
| 139 |
className = name; |
| 140 |
superclassName = superName; |
| 141 |
final String ENHANCED = "com/sleepycat/persist/impl/Enhanced"; |
| 142 |
if (containsString(interfaces, ENHANCED)) { |
| 143 |
throw abort(); |
| 144 |
} |
| 145 |
interfaces = appendString(interfaces, ENHANCED); |
| 146 |
isAbstract = ((access & ACC_ABSTRACT) != 0); |
| 147 |
hasPersistentSuperclass = |
| 148 |
(superName != null && !superName.equals("java/lang/Object")); |
| 149 |
super.visit(version, access, name, sig, superName, interfaces); |
| 150 |
} |
| 151 |
|
| 152 |
@Override |
| 153 |
public void visitSource(String source, String debug) { |
| 154 |
super.visitSource(source, debug); |
| 155 |
} |
| 156 |
|
| 157 |
@Override |
| 158 |
public AnnotationVisitor visitAnnotation(String desc, boolean visible) { |
| 159 |
if (desc.equals("Lcom/sleepycat/persist/model/Entity;") || |
| 160 |
desc.equals("Lcom/sleepycat/persist/model/Persistent;")) { |
| 161 |
isPersistent = true; |
| 162 |
} |
| 163 |
return super.visitAnnotation(desc, visible); |
| 164 |
} |
| 165 |
|
| 166 |
@Override |
| 167 |
public FieldVisitor visitField(int access, |
| 168 |
String name, |
| 169 |
String desc, |
| 170 |
String sig, |
| 171 |
Object value) { |
| 172 |
if (!isPersistent) { |
| 173 |
throw abort(); |
| 174 |
} |
| 175 |
FieldVisitor ret = super.visitField(access, name, desc, sig, value); |
| 176 |
if ((access & ACC_STATIC) == 0) { |
| 177 |
FieldInfo info = new FieldInfo(ret, name, desc, |
| 178 |
(access & ACC_TRANSIENT) != 0); |
| 179 |
nonKeyFields.add(info); |
| 180 |
ret = info; |
| 181 |
} |
| 182 |
return ret; |
| 183 |
} |
| 184 |
|
| 185 |
@Override |
| 186 |
public MethodVisitor visitMethod(int access, |
| 187 |
String name, |
| 188 |
String desc, |
| 189 |
String sig, |
| 190 |
String[] exceptions) { |
| 191 |
if (!isPersistent) { |
| 192 |
throw abort(); |
| 193 |
} |
| 194 |
if ("<init>".equals(name) && "()V".equals(desc)) { |
| 195 |
hasDefaultConstructor = true; |
| 196 |
} |
| 197 |
if ("<clinit>".equals(name)) { |
| 198 |
if (staticBlockMethod != null) { |
| 199 |
throw DbCompat.unexpectedState(); |
| 200 |
} |
| 201 |
staticBlockMethod = "bdbExistingStaticBlock"; |
| 202 |
return cv.visitMethod |
| 203 |
(ACC_PRIVATE + ACC_STATIC, staticBlockMethod, "()V", null, |
| 204 |
null); |
| 205 |
} |
| 206 |
return super.visitMethod(access, name, desc, sig, exceptions); |
| 207 |
} |
| 208 |
|
| 209 |
@Override |
| 210 |
public void visitEnd() { |
| 211 |
if (!isPersistent || !hasDefaultConstructor) { |
| 212 |
throw abort(); |
| 213 |
} |
| 214 |
/* Generate new code at the end of the class. */ |
| 215 |
sortFields(); |
| 216 |
genBdbNewInstance(); |
| 217 |
genBdbNewArray(); |
| 218 |
genBdbIsPriKeyFieldNullOrZero(); |
| 219 |
genBdbWritePriKeyField(); |
| 220 |
genBdbReadPriKeyField(); |
| 221 |
genBdbWriteSecKeyFields(); |
| 222 |
genBdbReadSecKeyFields(); |
| 223 |
genBdbWriteNonKeyFields(); |
| 224 |
genBdbReadNonKeyFields(); |
| 225 |
genBdbWriteCompositeKeyFields(); |
| 226 |
genBdbReadCompositeKeyFields(); |
| 227 |
genBdbGetField(); |
| 228 |
genBdbSetField(); |
| 229 |
genBdbSetPriField(); |
| 230 |
genStaticBlock(); |
| 231 |
super.visitEnd(); |
| 232 |
} |
| 233 |
|
| 234 |
private void sortFields() { |
| 235 |
/* |
| 236 |
System.out.println("AllFields: " + nonKeyFields); |
| 237 |
//*/ |
| 238 |
if (nonKeyFields.size() == 0) { |
| 239 |
return; |
| 240 |
} |
| 241 |
isCompositeKey = true; |
| 242 |
for (FieldInfo field : nonKeyFields) { |
| 243 |
if (field.order == null) { |
| 244 |
isCompositeKey = false; |
| 245 |
} |
| 246 |
} |
| 247 |
if (isCompositeKey) { |
| 248 |
Collections.sort(nonKeyFields, new Comparator<FieldInfo>() { |
| 249 |
public int compare(FieldInfo f1, FieldInfo f2) { |
| 250 |
return f1.order.value - f2.order.value; |
| 251 |
} |
| 252 |
}); |
| 253 |
} else { |
| 254 |
for (int i = 0; i < nonKeyFields.size();) { |
| 255 |
FieldInfo field = nonKeyFields.get(i); |
| 256 |
if (field.isTransient) { |
| 257 |
nonKeyFields.remove(i); |
| 258 |
} else if (field.isPriKey) { |
| 259 |
if (priKeyField == null) { |
| 260 |
priKeyField = field; |
| 261 |
nonKeyFields.remove(i); |
| 262 |
} |
| 263 |
} else if (field.isSecKey) { |
| 264 |
secKeyFields.add(field); |
| 265 |
nonKeyFields.remove(i); |
| 266 |
} else { |
| 267 |
i += 1; |
| 268 |
} |
| 269 |
} |
| 270 |
Comparator<FieldInfo> cmp = new Comparator<FieldInfo>() { |
| 271 |
public int compare(FieldInfo f1, FieldInfo f2) { |
| 272 |
return f1.name.compareTo(f2.name); |
| 273 |
} |
| 274 |
}; |
| 275 |
Collections.sort(secKeyFields, cmp); |
| 276 |
Collections.sort(nonKeyFields, cmp); |
| 277 |
} |
| 278 |
/* |
| 279 |
System.out.println("PriKey: " + priKeyField); |
| 280 |
System.out.println("SecKeys: " + secKeyFields); |
| 281 |
System.out.println("NonKeys: " + nonKeyFields); |
| 282 |
//*/ |
| 283 |
} |
| 284 |
|
| 285 |
/** |
| 286 |
* Outputs code in a static block to register the prototype instance: |
| 287 |
* |
| 288 |
* static { |
| 289 |
* EnhancedAccessor.registerClass(TheClassName, new TheClass()); |
| 290 |
* // or for an abstract class: |
| 291 |
* EnhancedAccessor.registerClass(TheClassName, null); |
| 292 |
* } |
| 293 |
*/ |
| 294 |
private void genStaticBlock() { |
| 295 |
MethodVisitor mv = |
| 296 |
cv.visitMethod(ACC_STATIC, "<clinit>", "()V", null, null); |
| 297 |
mv.visitCode(); |
| 298 |
if (staticBlockMethod != null) { |
| 299 |
mv.visitMethodInsn |
| 300 |
(INVOKESTATIC, className, staticBlockMethod, "()V"); |
| 301 |
} |
| 302 |
mv.visitLdcInsn(className.replace('/', '.')); |
| 303 |
if (isAbstract) { |
| 304 |
mv.visitInsn(ACONST_NULL); |
| 305 |
} else { |
| 306 |
mv.visitTypeInsn(NEW, className); |
| 307 |
mv.visitInsn(DUP); |
| 308 |
mv.visitMethodInsn(INVOKESPECIAL, className, "<init>", "()V"); |
| 309 |
} |
| 310 |
mv.visitMethodInsn |
| 311 |
(INVOKESTATIC, "com/sleepycat/persist/impl/EnhancedAccessor", |
| 312 |
"registerClass", |
| 313 |
"(Ljava/lang/String;Lcom/sleepycat/persist/impl/Enhanced;)V"); |
| 314 |
mv.visitInsn(RETURN); |
| 315 |
mv.visitMaxs(3, 0); |
| 316 |
mv.visitEnd(); |
| 317 |
} |
| 318 |
|
| 319 |
/** |
| 320 |
* public Object bdbNewInstance() { |
| 321 |
* return new TheClass(); |
| 322 |
* // or if abstract: |
| 323 |
* return null; |
| 324 |
* } |
| 325 |
*/ |
| 326 |
private void genBdbNewInstance() { |
| 327 |
MethodVisitor mv = cv.visitMethod |
| 328 |
(ACC_PUBLIC, "bdbNewInstance", "()Ljava/lang/Object;", null, null); |
| 329 |
mv.visitCode(); |
| 330 |
if (isAbstract) { |
| 331 |
mv.visitInsn(ACONST_NULL); |
| 332 |
mv.visitInsn(ARETURN); |
| 333 |
mv.visitMaxs(1, 1); |
| 334 |
} else { |
| 335 |
mv.visitTypeInsn(NEW, className); |
| 336 |
mv.visitInsn(DUP); |
| 337 |
mv.visitMethodInsn(INVOKESPECIAL, className, "<init>", "()V"); |
| 338 |
mv.visitInsn(ARETURN); |
| 339 |
mv.visitMaxs(2, 1); |
| 340 |
} |
| 341 |
mv.visitEnd(); |
| 342 |
} |
| 343 |
|
| 344 |
/** |
| 345 |
* public Object bdbNewArray(int len) { |
| 346 |
* return new TheClass[len]; |
| 347 |
* // or if abstract: |
| 348 |
* return null; |
| 349 |
* } |
| 350 |
*/ |
| 351 |
private void genBdbNewArray() { |
| 352 |
MethodVisitor mv = cv.visitMethod |
| 353 |
(ACC_PUBLIC, "bdbNewArray", "(I)Ljava/lang/Object;", null, null); |
| 354 |
mv.visitCode(); |
| 355 |
if (isAbstract) { |
| 356 |
mv.visitInsn(ACONST_NULL); |
| 357 |
mv.visitInsn(ARETURN); |
| 358 |
mv.visitMaxs(1, 2); |
| 359 |
} else { |
| 360 |
mv.visitVarInsn(ILOAD, 1); |
| 361 |
mv.visitTypeInsn(ANEWARRAY, className); |
| 362 |
mv.visitInsn(ARETURN); |
| 363 |
mv.visitMaxs(1, 2); |
| 364 |
mv.visitEnd(); |
| 365 |
} |
| 366 |
} |
| 367 |
|
| 368 |
/** |
| 369 |
* public boolean bdbIsPriKeyFieldNullOrZero() { |
| 370 |
* return theField == null; // or zero or false, as appropriate |
| 371 |
* // or if no primary key but has superclass: |
| 372 |
* return super.bdbIsPriKeyFieldNullOrZero(); |
| 373 |
* } |
| 374 |
*/ |
| 375 |
private void genBdbIsPriKeyFieldNullOrZero() { |
| 376 |
MethodVisitor mv = cv.visitMethod |
| 377 |
(ACC_PUBLIC, "bdbIsPriKeyFieldNullOrZero", "()Z", null, null); |
| 378 |
mv.visitCode(); |
| 379 |
if (priKeyField != null) { |
| 380 |
mv.visitVarInsn(ALOAD, 0); |
| 381 |
mv.visitFieldInsn |
| 382 |
(GETFIELD, className, priKeyField.name, |
| 383 |
priKeyField.type.getDescriptor()); |
| 384 |
Label l0 = new Label(); |
| 385 |
if (isRefType(priKeyField.type)) { |
| 386 |
mv.visitJumpInsn(IFNONNULL, l0); |
| 387 |
} else { |
| 388 |
genBeforeCompareToZero(mv, priKeyField.type); |
| 389 |
mv.visitJumpInsn(IFNE, l0); |
| 390 |
} |
| 391 |
mv.visitInsn(ICONST_1); |
| 392 |
Label l1 = new Label(); |
| 393 |
mv.visitJumpInsn(GOTO, l1); |
| 394 |
mv.visitLabel(l0); |
| 395 |
mv.visitInsn(ICONST_0); |
| 396 |
mv.visitLabel(l1); |
| 397 |
} else if (hasPersistentSuperclass) { |
| 398 |
mv.visitVarInsn(ALOAD, 0); |
| 399 |
mv.visitMethodInsn |
| 400 |
(INVOKESPECIAL, superclassName, "bdbIsPriKeyFieldNullOrZero", |
| 401 |
"()Z"); |
| 402 |
} else { |
| 403 |
mv.visitInsn(ICONST_0); |
| 404 |
} |
| 405 |
mv.visitInsn(IRETURN); |
| 406 |
mv.visitMaxs(1, 1); |
| 407 |
mv.visitEnd(); |
| 408 |
} |
| 409 |
|
| 410 |
/** |
| 411 |
* public void bdbWritePriKeyField(EntityOutput output, Format format) { |
| 412 |
* output.writeKeyObject(theField, format); |
| 413 |
* // or |
| 414 |
* output.writeInt(theField); // and other simple types |
| 415 |
* // or if no primary key but has superclass: |
| 416 |
* return super.bdbWritePriKeyField(output, format); |
| 417 |
* } |
| 418 |
*/ |
| 419 |
private void genBdbWritePriKeyField() { |
| 420 |
MethodVisitor mv = cv.visitMethod |
| 421 |
(ACC_PUBLIC, "bdbWritePriKeyField", |
| 422 |
"(Lcom/sleepycat/persist/impl/EntityOutput;" + |
| 423 |
"Lcom/sleepycat/persist/impl/Format;)V", |
| 424 |
null, null); |
| 425 |
mv.visitCode(); |
| 426 |
if (priKeyField != null) { |
| 427 |
if (!genWriteSimpleKeyField(mv, priKeyField)) { |
| 428 |
/* For a non-simple type, call EntityOutput.writeKeyObject. */ |
| 429 |
mv.visitVarInsn(ALOAD, 1); |
| 430 |
mv.visitVarInsn(ALOAD, 0); |
| 431 |
mv.visitFieldInsn |
| 432 |
(GETFIELD, className, priKeyField.name, |
| 433 |
priKeyField.type.getDescriptor()); |
| 434 |
mv.visitVarInsn(ALOAD, 2); |
| 435 |
mv.visitMethodInsn |
| 436 |
(INVOKEINTERFACE, |
| 437 |
"com/sleepycat/persist/impl/EntityOutput", |
| 438 |
"writeKeyObject", |
| 439 |
"(Ljava/lang/Object;" + |
| 440 |
"Lcom/sleepycat/persist/impl/Format;)V"); |
| 441 |
} |
| 442 |
} else if (hasPersistentSuperclass) { |
| 443 |
mv.visitVarInsn(ALOAD, 0); |
| 444 |
mv.visitVarInsn(ALOAD, 1); |
| 445 |
mv.visitVarInsn(ALOAD, 2); |
| 446 |
mv.visitMethodInsn |
| 447 |
(INVOKESPECIAL, superclassName, "bdbWritePriKeyField", |
| 448 |
"(Lcom/sleepycat/persist/impl/EntityOutput;" + |
| 449 |
"Lcom/sleepycat/persist/impl/Format;)V"); |
| 450 |
} |
| 451 |
mv.visitInsn(RETURN); |
| 452 |
mv.visitMaxs(3, 3); |
| 453 |
mv.visitEnd(); |
| 454 |
} |
| 455 |
|
| 456 |
/** |
| 457 |
* public void bdbReadPriKeyField(EntityInput input, Format format) { |
| 458 |
* theField = (TheFieldClass) input.readKeyObject(format); |
| 459 |
* // or |
| 460 |
* theField = input.readInt(); // and other simple types |
| 461 |
* // or if no primary key but has superclass: |
| 462 |
* super.bdbReadPriKeyField(input, format); |
| 463 |
* } |
| 464 |
*/ |
| 465 |
private void genBdbReadPriKeyField() { |
| 466 |
MethodVisitor mv = cv.visitMethod |
| 467 |
(ACC_PUBLIC, "bdbReadPriKeyField", |
| 468 |
"(Lcom/sleepycat/persist/impl/EntityInput;" + |
| 469 |
"Lcom/sleepycat/persist/impl/Format;)V", |
| 470 |
null, null); |
| 471 |
mv.visitCode(); |
| 472 |
if (priKeyField != null) { |
| 473 |
if (!genReadSimpleKeyField(mv, priKeyField)) { |
| 474 |
/* For a non-simple type, call EntityInput.readKeyObject. */ |
| 475 |
mv.visitVarInsn(ALOAD, 0); |
| 476 |
mv.visitVarInsn(ALOAD, 1); |
| 477 |
mv.visitVarInsn(ALOAD, 2); |
| 478 |
mv.visitMethodInsn |
| 479 |
(INVOKEINTERFACE, "com/sleepycat/persist/impl/EntityInput", |
| 480 |
"readKeyObject", |
| 481 |
"(Lcom/sleepycat/persist/impl/Format;)" + |
| 482 |
"Ljava/lang/Object;"); |
| 483 |
mv.visitTypeInsn(CHECKCAST, getTypeInstName(priKeyField.type)); |
| 484 |
mv.visitFieldInsn |
| 485 |
(PUTFIELD, className, priKeyField.name, |
| 486 |
priKeyField.type.getDescriptor()); |
| 487 |
} |
| 488 |
} else if (hasPersistentSuperclass) { |
| 489 |
mv.visitVarInsn(ALOAD, 0); |
| 490 |
mv.visitVarInsn(ALOAD, 1); |
| 491 |
mv.visitVarInsn(ALOAD, 2); |
| 492 |
mv.visitMethodInsn |
| 493 |
(INVOKESPECIAL, superclassName, "bdbReadPriKeyField", |
| 494 |
"(Lcom/sleepycat/persist/impl/EntityInput;" + |
| 495 |
"Lcom/sleepycat/persist/impl/Format;)V"); |
| 496 |
} |
| 497 |
mv.visitInsn(RETURN); |
| 498 |
mv.visitMaxs(3, 3); |
| 499 |
mv.visitEnd(); |
| 500 |
} |
| 501 |
|
| 502 |
/** |
| 503 |
* public void bdbWriteSecKeyFields(EntityOutput output) { |
| 504 |
* output.registerPriKeyObject(priKeyField); // if an object |
| 505 |
* super.bdbWriteSecKeyFields(EntityOutput output); // if has super |
| 506 |
* output.writeInt(secKeyField1); |
| 507 |
* output.writeObject(secKeyField2, null); |
| 508 |
* // etc |
| 509 |
* } |
| 510 |
*/ |
| 511 |
private void genBdbWriteSecKeyFields() { |
| 512 |
MethodVisitor mv = cv.visitMethod |
| 513 |
(ACC_PUBLIC, "bdbWriteSecKeyFields", |
| 514 |
"(Lcom/sleepycat/persist/impl/EntityOutput;)V", null, null); |
| 515 |
mv.visitCode(); |
| 516 |
|
| 517 |
/* |
| 518 |
* In JE 5.0, String is treated as primitive type, so String does |
| 519 |
* not need to be registered. [#19247] |
| 520 |
*/ |
| 521 |
if (priKeyField != null && |
| 522 |
isRefType(priKeyField.type) && |
| 523 |
!priKeyField.isString) { |
| 524 |
genRegisterPrimaryKey(mv, false); |
| 525 |
} |
| 526 |
if (hasPersistentSuperclass) { |
| 527 |
mv.visitVarInsn(ALOAD, 0); |
| 528 |
mv.visitVarInsn(ALOAD, 1); |
| 529 |
mv.visitMethodInsn |
| 530 |
(INVOKESPECIAL, superclassName, "bdbWriteSecKeyFields", |
| 531 |
"(Lcom/sleepycat/persist/impl/EntityOutput;)V"); |
| 532 |
} |
| 533 |
for (FieldInfo field : secKeyFields) { |
| 534 |
genWriteField(mv, field); |
| 535 |
} |
| 536 |
mv.visitInsn(RETURN); |
| 537 |
mv.visitMaxs(2, 2); |
| 538 |
mv.visitEnd(); |
| 539 |
} |
| 540 |
|
| 541 |
/** |
| 542 |
* public void bdbReadSecKeyFields(EntityInput input, |
| 543 |
* int startField, |
| 544 |
* int endField, |
| 545 |
* int superLevel) { |
| 546 |
* input.registerPriKeyObject(priKeyField); // if an object |
| 547 |
* // if has super: |
| 548 |
* if (superLevel != 0) { |
| 549 |
* super.bdbReadSecKeyFields(..., superLevel - 1); |
| 550 |
* } |
| 551 |
* if (superLevel <= 0) { |
| 552 |
* switch (startField) { |
| 553 |
* case 0: |
| 554 |
* secKeyField1 = input.readInt(); |
| 555 |
* if (endField == 0) break; |
| 556 |
* case 1: |
| 557 |
* secKeyField2 = (String) input.readObject(); |
| 558 |
* if (endField == 1) break; |
| 559 |
* case 2: |
| 560 |
* secKeyField3 = input.readInt(); |
| 561 |
* } |
| 562 |
* } |
| 563 |
* } |
| 564 |
*/ |
| 565 |
private void genBdbReadSecKeyFields() { |
| 566 |
MethodVisitor mv = cv.visitMethod |
| 567 |
(ACC_PUBLIC, "bdbReadSecKeyFields", |
| 568 |
"(Lcom/sleepycat/persist/impl/EntityInput;III)V", null, null); |
| 569 |
mv.visitCode(); |
| 570 |
if (priKeyField != null && |
| 571 |
isRefType(priKeyField.type) && |
| 572 |
!priKeyField.isString) { |
| 573 |
genRegisterPrimaryKey(mv, true); |
| 574 |
} else if (priKeyField != null && priKeyField.isString) { |
| 575 |
genRegisterPrimaryStringKey(mv); |
| 576 |
} |
| 577 |
genReadSuperKeyFields(mv, true); |
| 578 |
genReadFieldSwitch(mv, secKeyFields); |
| 579 |
mv.visitInsn(RETURN); |
| 580 |
mv.visitMaxs(5, 5); |
| 581 |
mv.visitEnd(); |
| 582 |
} |
| 583 |
|
| 584 |
/** |
| 585 |
* output.registerPriKeyObject(priKeyField); |
| 586 |
* // or |
| 587 |
* input.registerPriKeyObject(priKeyField); |
| 588 |
*/ |
| 589 |
private void genRegisterPrimaryKey(MethodVisitor mv, boolean input) { |
| 590 |
String entityInputOrOutputClass = |
| 591 |
input ? "com/sleepycat/persist/impl/EntityInput" |
| 592 |
: "com/sleepycat/persist/impl/EntityOutput"; |
| 593 |
mv.visitVarInsn(ALOAD, 1); |
| 594 |
mv.visitVarInsn(ALOAD, 0); |
| 595 |
mv.visitFieldInsn |
| 596 |
(GETFIELD, className, priKeyField.name, |
| 597 |
priKeyField.type.getDescriptor()); |
| 598 |
mv.visitMethodInsn |
| 599 |
(INVOKEINTERFACE, entityInputOrOutputClass, "registerPriKeyObject", |
| 600 |
"(Ljava/lang/Object;)V"); |
| 601 |
} |
| 602 |
|
| 603 |
/** |
| 604 |
* input.registerPriStringKeyObject(priKeyField); |
| 605 |
*/ |
| 606 |
private void genRegisterPrimaryStringKey(MethodVisitor mv) { |
| 607 |
String entityInputOrOutputClass = |
| 608 |
"com/sleepycat/persist/impl/EntityInput"; |
| 609 |
mv.visitVarInsn(ALOAD, 1); |
| 610 |
mv.visitVarInsn(ALOAD, 0); |
| 611 |
mv.visitFieldInsn |
| 612 |
(GETFIELD, className, priKeyField.name, |
| 613 |
priKeyField.type.getDescriptor()); |
| 614 |
mv.visitMethodInsn |
| 615 |
(INVOKEINTERFACE, entityInputOrOutputClass, |
| 616 |
"registerPriStringKeyObject", "(Ljava/lang/Object;)V"); |
| 617 |
} |
| 618 |
|
| 619 |
/** |
| 620 |
* public void bdbWriteNonKeyFields(EntityOutput output) { |
| 621 |
* // like bdbWriteSecKeyFields but does not call registerPriKeyObject |
| 622 |
* } |
| 623 |
*/ |
| 624 |
private void genBdbWriteNonKeyFields() { |
| 625 |
MethodVisitor mv = cv.visitMethod |
| 626 |
(ACC_PUBLIC, "bdbWriteNonKeyFields", |
| 627 |
"(Lcom/sleepycat/persist/impl/EntityOutput;)V", null, null); |
| 628 |
mv.visitCode(); |
| 629 |
if (!isCompositeKey) { |
| 630 |
if (hasPersistentSuperclass) { |
| 631 |
mv.visitVarInsn(ALOAD, 0); |
| 632 |
mv.visitVarInsn(ALOAD, 1); |
| 633 |
mv.visitMethodInsn |
| 634 |
(INVOKESPECIAL, superclassName, "bdbWriteNonKeyFields", |
| 635 |
"(Lcom/sleepycat/persist/impl/EntityOutput;)V"); |
| 636 |
} |
| 637 |
for (FieldInfo field : nonKeyFields) { |
| 638 |
genWriteField(mv, field); |
| 639 |
} |
| 640 |
} |
| 641 |
mv.visitInsn(RETURN); |
| 642 |
mv.visitMaxs(2, 2); |
| 643 |
mv.visitEnd(); |
| 644 |
} |
| 645 |
|
| 646 |
/** |
| 647 |
* public void bdbReadNonKeyFields(EntityInput input, |
| 648 |
* int startField, |
| 649 |
* int endField, |
| 650 |
* int superLevel) { |
| 651 |
* // like bdbReadSecKeyFields but does not call registerPriKeyObject |
| 652 |
* } |
| 653 |
*/ |
| 654 |
private void genBdbReadNonKeyFields() { |
| 655 |
MethodVisitor mv = cv.visitMethod |
| 656 |
(ACC_PUBLIC, "bdbReadNonKeyFields", |
| 657 |
"(Lcom/sleepycat/persist/impl/EntityInput;III)V", null, null); |
| 658 |
mv.visitCode(); |
| 659 |
if (!isCompositeKey) { |
| 660 |
genReadSuperKeyFields(mv, false); |
| 661 |
genReadFieldSwitch(mv, nonKeyFields); |
| 662 |
} |
| 663 |
mv.visitInsn(RETURN); |
| 664 |
mv.visitMaxs(5, 5); |
| 665 |
mv.visitEnd(); |
| 666 |
} |
| 667 |
|
| 668 |
/** |
| 669 |
* public void bdbWriteCompositeKeyFields(EntityOutput output, |
| 670 |
* Format[] formats) { |
| 671 |
* output.writeInt(compositeKeyField1); |
| 672 |
* output.writeKeyObject(compositeKeyField2, formats[1]); |
| 673 |
* // etc |
| 674 |
* } |
| 675 |
*/ |
| 676 |
private void genBdbWriteCompositeKeyFields() { |
| 677 |
MethodVisitor mv = cv.visitMethod |
| 678 |
(ACC_PUBLIC, "bdbWriteCompositeKeyFields", |
| 679 |
"(Lcom/sleepycat/persist/impl/EntityOutput;" + |
| 680 |
"[Lcom/sleepycat/persist/impl/Format;)V", |
| 681 |
null, null); |
| 682 |
mv.visitCode(); |
| 683 |
if (isCompositeKey) { |
| 684 |
for (int i = 0; i < nonKeyFields.size(); i += 1) { |
| 685 |
FieldInfo field = nonKeyFields.get(i); |
| 686 |
if (!genWriteSimpleKeyField(mv, field)) { |
| 687 |
/* For a non-simple type, call writeKeyObject. */ |
| 688 |
mv.visitVarInsn(ALOAD, 1); |
| 689 |
mv.visitVarInsn(ALOAD, 0); |
| 690 |
mv.visitFieldInsn |
| 691 |
(GETFIELD, className, field.name, |
| 692 |
field.type.getDescriptor()); |
| 693 |
mv.visitVarInsn(ALOAD, 2); |
| 694 |
if (i <= Byte.MAX_VALUE) { |
| 695 |
mv.visitIntInsn(BIPUSH, i); |
| 696 |
} else { |
| 697 |
mv.visitLdcInsn(new Integer(i)); |
| 698 |
} |
| 699 |
mv.visitInsn(AALOAD); |
| 700 |
mv.visitMethodInsn |
| 701 |
(INVOKEINTERFACE, |
| 702 |
"com/sleepycat/persist/impl/EntityOutput", |
| 703 |
"writeKeyObject", |
| 704 |
"(Ljava/lang/Object;" + |
| 705 |
"Lcom/sleepycat/persist/impl/Format;)V"); |
| 706 |
} |
| 707 |
} |
| 708 |
} |
| 709 |
mv.visitInsn(RETURN); |
| 710 |
mv.visitMaxs(3, 3); |
| 711 |
mv.visitEnd(); |
| 712 |
} |
| 713 |
|
| 714 |
/** |
| 715 |
* public void bdbReadCompositeKeyFields(EntityInput input, |
| 716 |
* Format[] formats) { |
| 717 |
* compositeKeyField1 = input.readInt(); |
| 718 |
* compositeKeyField2 = input.readKeyObject(formats[1]); |
| 719 |
* } |
| 720 |
*/ |
| 721 |
private void genBdbReadCompositeKeyFields() { |
| 722 |
MethodVisitor mv = cv.visitMethod |
| 723 |
(ACC_PUBLIC, "bdbReadCompositeKeyFields", |
| 724 |
"(Lcom/sleepycat/persist/impl/EntityInput;" + |
| 725 |
"[Lcom/sleepycat/persist/impl/Format;)V", |
| 726 |
null, null); |
| 727 |
mv.visitCode(); |
| 728 |
if (isCompositeKey) { |
| 729 |
for (int i = 0; i < nonKeyFields.size(); i += 1) { |
| 730 |
FieldInfo field = nonKeyFields.get(i); |
| 731 |
/* Ignore non-simple (illegal) types for composite key. */ |
| 732 |
if (!genReadSimpleKeyField(mv, field)) { |
| 733 |
/* For a non-simple type, call readKeyObject. */ |
| 734 |
mv.visitVarInsn(ALOAD, 0); |
| 735 |
mv.visitVarInsn(ALOAD, 1); |
| 736 |
mv.visitVarInsn(ALOAD, 2); |
| 737 |
if (i <= Byte.MAX_VALUE) { |
| 738 |
mv.visitIntInsn(BIPUSH, i); |
| 739 |
} else { |
| 740 |
mv.visitLdcInsn(new Integer(i)); |
| 741 |
} |
| 742 |
mv.visitInsn(AALOAD); |
| 743 |
mv.visitMethodInsn |
| 744 |
(INVOKEINTERFACE, |
| 745 |
"com/sleepycat/persist/impl/EntityInput", |
| 746 |
"readKeyObject", |
| 747 |
"(Lcom/sleepycat/persist/impl/Format;)" + |
| 748 |
"Ljava/lang/Object;"); |
| 749 |
mv.visitTypeInsn(CHECKCAST, getTypeInstName(field.type)); |
| 750 |
mv.visitFieldInsn |
| 751 |
(PUTFIELD, className, field.name, |
| 752 |
field.type.getDescriptor()); |
| 753 |
} |
| 754 |
} |
| 755 |
} |
| 756 |
mv.visitInsn(RETURN); |
| 757 |
mv.visitMaxs(5, 5); |
| 758 |
mv.visitEnd(); |
| 759 |
} |
| 760 |
|
| 761 |
/** |
| 762 |
* output.writeInt(field); // and other primitives |
| 763 |
* // or |
| 764 |
* output.writeObject(field, null); |
| 765 |
*/ |
| 766 |
private void genWriteField(MethodVisitor mv, FieldInfo field) { |
| 767 |
mv.visitVarInsn(ALOAD, 1); |
| 768 |
mv.visitVarInsn(ALOAD, 0); |
| 769 |
mv.visitFieldInsn |
| 770 |
(GETFIELD, className, field.name, field.type.getDescriptor()); |
| 771 |
int sort = field.type.getSort(); |
| 772 |
if (field.isString) { |
| 773 |
|
| 774 |
/* |
| 775 |
* In JE 5.0, we treat String as primitive, and will not store |
| 776 |
* format ID for String data. [#19247] |
| 777 |
*/ |
| 778 |
mv.visitMethodInsn |
| 779 |
(INVOKEINTERFACE, "com/sleepycat/persist/impl/EntityOutput", |
| 780 |
"writeString", |
| 781 |
"(Ljava/lang/String;)Lcom/sleepycat/bind/tuple/TupleOutput;"); |
| 782 |
mv.visitInsn(POP); |
| 783 |
} else if (sort == Type.OBJECT || sort == Type.ARRAY) { |
| 784 |
mv.visitInsn(ACONST_NULL); |
| 785 |
mv.visitMethodInsn |
| 786 |
(INVOKEINTERFACE, "com/sleepycat/persist/impl/EntityOutput", |
| 787 |
"writeObject", |
| 788 |
"(Ljava/lang/Object;Lcom/sleepycat/persist/impl/Format;)V"); |
| 789 |
} else { |
| 790 |
genWritePrimitive(mv, sort); |
| 791 |
} |
| 792 |
} |
| 793 |
|
| 794 |
/** |
| 795 |
* Generates writing of a simple type key field, or returns false if the |
| 796 |
* key field is not a simple type (i.e., it is a composite key type). |
| 797 |
* |
| 798 |
* output.writeInt(theField); // and other primitives |
| 799 |
* // or |
| 800 |
* output.writeInt(theField.intValue()); // and other simple types |
| 801 |
* // or returns false |
| 802 |
*/ |
| 803 |
private boolean genWriteSimpleKeyField(MethodVisitor mv, FieldInfo field) { |
| 804 |
if (genWritePrimitiveField(mv, field)) { |
| 805 |
return true; |
| 806 |
} |
| 807 |
String fieldClassName = field.type.getClassName(); |
| 808 |
if (!isSimpleRefType(fieldClassName)) { |
| 809 |
return false; |
| 810 |
} |
| 811 |
mv.visitVarInsn(ALOAD, 1); |
| 812 |
mv.visitVarInsn(ALOAD, 0); |
| 813 |
mv.visitFieldInsn |
| 814 |
(GETFIELD, className, field.name, field.type.getDescriptor()); |
| 815 |
Integer sort = PRIMITIVE_WRAPPERS.get(fieldClassName); |
| 816 |
if (sort != null) { |
| 817 |
genUnwrapPrimitive(mv, sort); |
| 818 |
genWritePrimitive(mv, sort); |
| 819 |
} else if (fieldClassName.equals(Date.class.getName())) { |
| 820 |
mv.visitMethodInsn |
| 821 |
(INVOKEVIRTUAL, "java/util/Date", "getTime", "()J"); |
| 822 |
genWritePrimitive(mv, Type.LONG); |
| 823 |
} else if (fieldClassName.equals(String.class.getName())) { |
| 824 |
mv.visitMethodInsn |
| 825 |
(INVOKEINTERFACE, "com/sleepycat/persist/impl/EntityOutput", |
| 826 |
"writeString", |
| 827 |
"(Ljava/lang/String;)Lcom/sleepycat/bind/tuple/TupleOutput;"); |
| 828 |
mv.visitInsn(POP); |
| 829 |
} else if (fieldClassName.equals(BigInteger.class.getName())) { |
| 830 |
mv.visitMethodInsn |
| 831 |
(INVOKEINTERFACE, "com/sleepycat/persist/impl/EntityOutput", |
| 832 |
"writeBigInteger", |
| 833 |
"(Ljava/math/BigInteger;)Lcom/sleepycat/bind/tuple/TupleOutput;"); |
| 834 |
mv.visitInsn(POP); |
| 835 |
} else if (fieldClassName.equals(BigDecimal.class.getName())) { |
| 836 |
mv.visitMethodInsn |
| 837 |
(INVOKEINTERFACE, "com/sleepycat/persist/impl/EntityOutput", |
| 838 |
"writeSortedBigDecimal", |
| 839 |
"(Ljava/math/BigDecimal;)Lcom/sleepycat/bind/tuple/TupleOutput;"); |
| 840 |
mv.visitInsn(POP); |
| 841 |
} else { |
| 842 |
throw DbCompat.unexpectedState(fieldClassName); |
| 843 |
} |
| 844 |
return true; |
| 845 |
} |
| 846 |
|
| 847 |
private boolean genWritePrimitiveField(MethodVisitor mv, FieldInfo field) { |
| 848 |
int sort = field.type.getSort(); |
| 849 |
if (sort == Type.OBJECT || sort == Type.ARRAY) { |
| 850 |
return false; |
| 851 |
} |
| 852 |
mv.visitVarInsn(ALOAD, 1); |
| 853 |
mv.visitVarInsn(ALOAD, 0); |
| 854 |
mv.visitFieldInsn |
| 855 |
(GETFIELD, className, field.name, field.type.getDescriptor()); |
| 856 |
genWritePrimitive(mv, sort); |
| 857 |
return true; |
| 858 |
} |
| 859 |
|
| 860 |
/** |
| 861 |
* // if has super: |
| 862 |
* if (superLevel != 0) { |
| 863 |
* super.bdbReadXxxKeyFields(..., superLevel - 1); |
| 864 |
* } |
| 865 |
*/ |
| 866 |
private void genReadSuperKeyFields(MethodVisitor mv, |
| 867 |
boolean areSecKeyFields) { |
| 868 |
if (hasPersistentSuperclass) { |
| 869 |
Label next = new Label(); |
| 870 |
mv.visitVarInsn(ILOAD, 4); |
| 871 |
mv.visitJumpInsn(IFEQ, next); |
| 872 |
mv.visitVarInsn(ALOAD, 0); |
| 873 |
mv.visitVarInsn(ALOAD, 1); |
| 874 |
mv.visitVarInsn(ILOAD, 2); |
| 875 |
mv.visitVarInsn(ILOAD, 3); |
| 876 |
mv.visitVarInsn(ILOAD, 4); |
| 877 |
mv.visitInsn(ICONST_1); |
| 878 |
mv.visitInsn(ISUB); |
| 879 |
String name = areSecKeyFields ? "bdbReadSecKeyFields" |
| 880 |
: "bdbReadNonKeyFields"; |
| 881 |
mv.visitMethodInsn |
| 882 |
(INVOKESPECIAL, superclassName, name, |
| 883 |
"(Lcom/sleepycat/persist/impl/EntityInput;III)V"); |
| 884 |
mv.visitLabel(next); |
| 885 |
} |
| 886 |
} |
| 887 |
|
| 888 |
/** |
| 889 |
* public void bdbReadXxxKeyFields(EntityInput input, |
| 890 |
* int startField, |
| 891 |
* int endField, |
| 892 |
* int superLevel) { |
| 893 |
* // ... |
| 894 |
* if (superLevel <= 0) { |
| 895 |
* switch (startField) { |
| 896 |
* case 0: |
| 897 |
* keyField1 = input.readInt(); |
| 898 |
* if (endField == 0) break; |
| 899 |
* case 1: |
| 900 |
* keyField2 = (String) input.readObject(); |
| 901 |
* if (endField == 1) break; |
| 902 |
* case 2: |
| 903 |
* keyField3 = input.readInt(); |
| 904 |
* } |
| 905 |
* } |
| 906 |
*/ |
| 907 |
private void genReadFieldSwitch(MethodVisitor mv, List<FieldInfo> fields) { |
| 908 |
int nFields = fields.size(); |
| 909 |
if (nFields > 0) { |
| 910 |
mv.visitVarInsn(ILOAD, 4); |
| 911 |
Label pastSwitch = new Label(); |
| 912 |
mv.visitJumpInsn(IFGT, pastSwitch); |
| 913 |
Label[] labels = new Label[nFields]; |
| 914 |
for (int i = 0; i < nFields; i += 1) { |
| 915 |
labels[i] = new Label(); |
| 916 |
} |
| 917 |
mv.visitVarInsn(ILOAD, 2); |
| 918 |
mv.visitTableSwitchInsn(0, nFields - 1, pastSwitch, labels); |
| 919 |
for (int i = 0; i < nFields; i += 1) { |
| 920 |
FieldInfo field = fields.get(i); |
| 921 |
mv.visitLabel(labels[i]); |
| 922 |
genReadField(mv, field); |
| 923 |
if (i < nFields - 1) { |
| 924 |
Label nextCase = labels[i + 1]; |
| 925 |
mv.visitVarInsn(ILOAD, 3); |
| 926 |
if (i == 0) { |
| 927 |
mv.visitJumpInsn(IFNE, nextCase); |
| 928 |
} else { |
| 929 |
switch (i) { |
| 930 |
case 1: |
| 931 |
mv.visitInsn(ICONST_1); |
| 932 |
break; |
| 933 |
case 2: |
| 934 |
mv.visitInsn(ICONST_2); |
| 935 |
break; |
| 936 |
case 3: |
| 937 |
mv.visitInsn(ICONST_3); |
| 938 |
break; |
| 939 |
case 4: |
| 940 |
mv.visitInsn(ICONST_4); |
| 941 |
break; |
| 942 |
case 5: |
| 943 |
mv.visitInsn(ICONST_5); |
| 944 |
break; |
| 945 |
default: |
| 946 |
mv.visitIntInsn(BIPUSH, i); |
| 947 |
} |
| 948 |
mv.visitJumpInsn(IF_ICMPNE, nextCase); |
| 949 |
} |
| 950 |
mv.visitJumpInsn(GOTO, pastSwitch); |
| 951 |
} |
| 952 |
} |
| 953 |
mv.visitLabel(pastSwitch); |
| 954 |
} |
| 955 |
} |
| 956 |
|
| 957 |
/** |
| 958 |
* field = input.readInt(); // and other primitives |
| 959 |
* // or |
| 960 |
* field = (FieldClass) input.readObject(); |
| 961 |
*/ |
| 962 |
private void genReadField(MethodVisitor mv, FieldInfo field) { |
| 963 |
mv.visitVarInsn(ALOAD, 0); |
| 964 |
mv.visitVarInsn(ALOAD, 1); |
| 965 |
if (field.isString) { |
| 966 |
|
| 967 |
/* |
| 968 |
* In JE 5.0, we treat String as primitive, and will not store |
| 969 |
* format ID for String data. [#19247] |
| 970 |
*/ |
| 971 |
mv.visitMethodInsn |
| 972 |
(INVOKEINTERFACE, "com/sleepycat/persist/impl/EntityInput", |
| 973 |
"readStringObject", "()Ljava/lang/Object;"); |
| 974 |
mv.visitTypeInsn(CHECKCAST, getTypeInstName(field.type)); |
| 975 |
} else if (isRefType(field.type)) { |
| 976 |
mv.visitMethodInsn |
| 977 |
(INVOKEINTERFACE, "com/sleepycat/persist/impl/EntityInput", |
| 978 |
"readObject", "()Ljava/lang/Object;"); |
| 979 |
mv.visitTypeInsn(CHECKCAST, getTypeInstName(field.type)); |
| 980 |
} else { |
| 981 |
genReadPrimitive(mv, field.type.getSort()); |
| 982 |
} |
| 983 |
mv.visitFieldInsn |
| 984 |
(PUTFIELD, className, field.name, field.type.getDescriptor()); |
| 985 |
} |
| 986 |
|
| 987 |
/** |
| 988 |
* Generates reading of a simple type key field, or returns false if the |
| 989 |
* key field is not a simple type (i.e., it is a composite key type). |
| 990 |
* |
| 991 |
* field = input.readInt(); // and other primitives |
| 992 |
* // or |
| 993 |
* field = Integer.valueOf(input.readInt()); // and other simple types |
| 994 |
* // or returns false |
| 995 |
*/ |
| 996 |
private boolean genReadSimpleKeyField(MethodVisitor mv, FieldInfo field) { |
| 997 |
if (genReadPrimitiveField(mv, field)) { |
| 998 |
return true; |
| 999 |
} |
| 1000 |
String fieldClassName = field.type.getClassName(); |
| 1001 |
if (!isSimpleRefType(fieldClassName)) { |
| 1002 |
return false; |
| 1003 |
} |
| 1004 |
Integer sort = PRIMITIVE_WRAPPERS.get(fieldClassName); |
| 1005 |
if (sort != null) { |
| 1006 |
mv.visitVarInsn(ALOAD, 0); |
| 1007 |
mv.visitVarInsn(ALOAD, 1); |
| 1008 |
genReadPrimitive(mv, sort); |
| 1009 |
genWrapPrimitive(mv, sort); |
| 1010 |
} else if (fieldClassName.equals(Date.class.getName())) { |
| 1011 |
/* Date is a special case because we use NEW instead of valueOf. */ |
| 1012 |
mv.visitVarInsn(ALOAD, 0); |
| 1013 |
mv.visitTypeInsn(NEW, "java/util/Date"); |
| 1014 |
mv.visitInsn(DUP); |
| 1015 |
mv.visitVarInsn(ALOAD, 1); |
| 1016 |
genReadPrimitive(mv, Type.LONG); |
| 1017 |
mv.visitMethodInsn |
| 1018 |
(INVOKESPECIAL, "java/util/Date", "<init>", "(J)V"); |
| 1019 |
} else if (fieldClassName.equals(String.class.getName())) { |
| 1020 |
mv.visitVarInsn(ALOAD, 0); |
| 1021 |
mv.visitVarInsn(ALOAD, 1); |
| 1022 |
mv.visitMethodInsn |
| 1023 |
(INVOKEINTERFACE, "com/sleepycat/persist/impl/EntityInput", |
| 1024 |
"readString", "()Ljava/lang/String;"); |
| 1025 |
} else if (fieldClassName.equals(BigInteger.class.getName())) { |
| 1026 |
mv.visitVarInsn(ALOAD, 0); |
| 1027 |
mv.visitVarInsn(ALOAD, 1); |
| 1028 |
mv.visitMethodInsn |
| 1029 |
(INVOKEINTERFACE, "com/sleepycat/persist/impl/EntityInput", |
| 1030 |
"readBigInteger", "()Ljava/math/BigInteger;"); |
| 1031 |
} else if (fieldClassName.equals(BigDecimal.class.getName())) { |
| 1032 |
mv.visitVarInsn(ALOAD, 0); |
| 1033 |
mv.visitVarInsn(ALOAD, 1); |
| 1034 |
mv.visitMethodInsn |
| 1035 |
(INVOKEINTERFACE, "com/sleepycat/persist/impl/EntityInput", |
| 1036 |
"readSortedBigDecimal", "()Ljava/math/BigDecimal;"); |
| 1037 |
} else { |
| 1038 |
throw DbCompat.unexpectedState(fieldClassName); |
| 1039 |
} |
| 1040 |
mv.visitFieldInsn |
| 1041 |
(PUTFIELD, className, field.name, field.type.getDescriptor()); |
| 1042 |
return true; |
| 1043 |
} |
| 1044 |
|
| 1045 |
private boolean genReadPrimitiveField(MethodVisitor mv, FieldInfo field) { |
| 1046 |
int sort = field.type.getSort(); |
| 1047 |
if (sort == Type.OBJECT || sort == Type.ARRAY) { |
| 1048 |
return false; |
| 1049 |
} |
| 1050 |
mv.visitVarInsn(ALOAD, 0); |
| 1051 |
mv.visitVarInsn(ALOAD, 1); |
| 1052 |
genReadPrimitive(mv, sort); |
| 1053 |
mv.visitFieldInsn |
| 1054 |
(PUTFIELD, className, field.name, field.type.getDescriptor()); |
| 1055 |
return true; |
| 1056 |
} |
| 1057 |
|
| 1058 |
/** |
| 1059 |
* public Object bdbGetField(Object o, |
| 1060 |
* int field, |
| 1061 |
* int superLevel, |
| 1062 |
* boolean isSecField) { |
| 1063 |
* if (superLevel > 0) { |
| 1064 |
* // if has superclass: |
| 1065 |
* return super.bdbGetField |
| 1066 |
* (o, field, superLevel - 1, isSecField); |
| 1067 |
* } else if (isSecField) { |
| 1068 |
* switch (field) { |
| 1069 |
* case 0: |
| 1070 |
* return Integer.valueOf(f2); |
| 1071 |
* case 1: |
| 1072 |
* return f3; |
| 1073 |
* case 2: |
| 1074 |
* return f4; |
| 1075 |
* } |
| 1076 |
* } else { |
| 1077 |
* switch (field) { |
| 1078 |
* case 0: |
| 1079 |
* return Integer.valueOf(f5); |
| 1080 |
* case 1: |
| 1081 |
* return f6; |
| 1082 |
* case 2: |
| 1083 |
* return f7; |
| 1084 |
* } |
| 1085 |
* } |
| 1086 |
* return null; |
| 1087 |
* } |
| 1088 |
*/ |
| 1089 |
private void genBdbGetField() { |
| 1090 |
MethodVisitor mv = cv.visitMethod |
| 1091 |
(ACC_PUBLIC, "bdbGetField", |
| 1092 |
"(Ljava/lang/Object;IIZ)Ljava/lang/Object;", null, null); |
| 1093 |
mv.visitCode(); |
| 1094 |
mv.visitVarInsn(ILOAD, 3); |
| 1095 |
Label l0 = new Label(); |
| 1096 |
mv.visitJumpInsn(IFLE, l0); |
| 1097 |
Label l1 = new Label(); |
| 1098 |
if (hasPersistentSuperclass) { |
| 1099 |
mv.visitVarInsn(ALOAD, 0); |
| 1100 |
mv.visitVarInsn(ALOAD, 1); |
| 1101 |
mv.visitVarInsn(ILOAD, 2); |
| 1102 |
mv.visitVarInsn(ILOAD, 3); |
| 1103 |
mv.visitInsn(ICONST_1); |
| 1104 |
mv.visitInsn(ISUB); |
| 1105 |
mv.visitVarInsn(ILOAD, 4); |
| 1106 |
mv.visitMethodInsn |
| 1107 |
(INVOKESPECIAL, className, "bdbGetField", |
| 1108 |
"(Ljava/lang/Object;IIZ)Ljava/lang/Object;"); |
| 1109 |
mv.visitInsn(ARETURN); |
| 1110 |
} else { |
| 1111 |
mv.visitJumpInsn(GOTO, l1); |
| 1112 |
} |
| 1113 |
mv.visitLabel(l0); |
| 1114 |
mv.visitVarInsn(ILOAD, 4); |
| 1115 |
Label l2 = new Label(); |
| 1116 |
mv.visitJumpInsn(IFEQ, l2); |
| 1117 |
genGetFieldSwitch(mv, secKeyFields, l1); |
| 1118 |
mv.visitLabel(l2); |
| 1119 |
genGetFieldSwitch(mv, nonKeyFields, l1); |
| 1120 |
mv.visitLabel(l1); |
| 1121 |
mv.visitInsn(ACONST_NULL); |
| 1122 |
mv.visitInsn(ARETURN); |
| 1123 |
mv.visitMaxs(1, 5); |
| 1124 |
mv.visitEnd(); |
| 1125 |
} |
| 1126 |
|
| 1127 |
/** |
| 1128 |
* mv.visitVarInsn(ILOAD, 2); |
| 1129 |
* Label l0 = new Label(); |
| 1130 |
* Label l1 = new Label(); |
| 1131 |
* Label l2 = new Label(); |
| 1132 |
* mv.visitTableSwitchInsn(0, 2, TheDefLabel, new Label[] { l0, l1, l2 }); |
| 1133 |
* mv.visitLabel(l0); |
| 1134 |
* mv.visitVarInsn(ALOAD, 0); |
| 1135 |
* mv.visitFieldInsn(GETFIELD, TheClassName, "f2", "I"); |
| 1136 |
* mv.visitMethodInsn(INVOKESTATIC, "java/lang/Integer", "valueOf", |
| 1137 |
* "(I)Ljava/lang/Integer;"); |
| 1138 |
* mv.visitInsn(ARETURN); |
| 1139 |
* mv.visitLabel(l1); |
| 1140 |
* mv.visitVarInsn(ALOAD, 0); |
| 1141 |
* mv.visitFieldInsn(GETFIELD, TheClassName, "f3", "Ljava/lang/String;"); |
| 1142 |
* mv.visitInsn(ARETURN); |
| 1143 |
* mv.visitLabel(l2); |
| 1144 |
* mv.visitVarInsn(ALOAD, 0); |
| 1145 |
* mv.visitFieldInsn(GETFIELD, TheClassName, "f4", "Ljava/lang/String;"); |
| 1146 |
* mv.visitInsn(ARETURN); |
| 1147 |
*/ |
| 1148 |
private void genGetFieldSwitch(MethodVisitor mv, |
| 1149 |
List<FieldInfo> fields, |
| 1150 |
Label defaultLabel) { |
| 1151 |
int nFields = fields.size(); |
| 1152 |
if (nFields == 0) { |
| 1153 |
mv.visitJumpInsn(GOTO, defaultLabel); |
| 1154 |
return; |
| 1155 |
} |
| 1156 |
Label[] labels = new Label[nFields]; |
| 1157 |
for (int i = 0; i < nFields; i += 1) { |
| 1158 |
labels[i] = new Label(); |
| 1159 |
} |
| 1160 |
mv.visitVarInsn(ILOAD, 2); |
| 1161 |
mv.visitTableSwitchInsn(0, nFields - 1, defaultLabel, labels); |
| 1162 |
for (int i = 0; i < nFields; i += 1) { |
| 1163 |
FieldInfo field = fields.get(i); |
| 1164 |
mv.visitLabel(labels[i]); |
| 1165 |
mv.visitVarInsn(ALOAD, 0); |
| 1166 |
mv.visitFieldInsn |
| 1167 |
(GETFIELD, className, field.name, field.type.getDescriptor()); |
| 1168 |
if (!isRefType(field.type)) { |
| 1169 |
genWrapPrimitive(mv, field.type.getSort()); |
| 1170 |
} |
| 1171 |
mv.visitInsn(ARETURN); |
| 1172 |
} |
| 1173 |
} |
| 1174 |
|
| 1175 |
/** |
| 1176 |
* public void bdbSetField(Object o, |
| 1177 |
* int field, |
| 1178 |
* int superLevel, |
| 1179 |
* boolean isSecField, |
| 1180 |
* Object value) { |
| 1181 |
* if (superLevel > 0) { |
| 1182 |
* // if has superclass: |
| 1183 |
* super.bdbSetField |
| 1184 |
* (o, field, superLevel - 1, isSecField, value); |
| 1185 |
* } else if (isSecField) { |
| 1186 |
* switch (field) { |
| 1187 |
* case 0: |
| 1188 |
* f2 = ((Integer) value).intValue(); |
| 1189 |
* case 1: |
| 1190 |
* f3 = (String) value; |
| 1191 |
* case 2: |
| 1192 |
* f4 = (String) value; |
| 1193 |
* } |
| 1194 |
* } else { |
| 1195 |
* switch (field) { |
| 1196 |
* case 0: |
| 1197 |
* f5 = ((Integer) value).intValue(); |
| 1198 |
* case 1: |
| 1199 |
* f6 = (String) value; |
| 1200 |
* case 2: |
| 1201 |
* f7 = (String) value; |
| 1202 |
* } |
| 1203 |
* } |
| 1204 |
* } |
| 1205 |
*/ |
| 1206 |
private void genBdbSetField() { |
| 1207 |
MethodVisitor mv = cv.visitMethod |
| 1208 |
(ACC_PUBLIC, "bdbSetField", |
| 1209 |
"(Ljava/lang/Object;IIZLjava/lang/Object;)V", null, null); |
| 1210 |
mv.visitCode(); |
| 1211 |
mv.visitVarInsn(ILOAD, 3); |
| 1212 |
Label l0 = new Label(); |
| 1213 |
mv.visitJumpInsn(IFLE, l0); |
| 1214 |
if (hasPersistentSuperclass) { |
| 1215 |
mv.visitVarInsn(ALOAD, 0); |
| 1216 |
mv.visitVarInsn(ALOAD, 1); |
| 1217 |
mv.visitVarInsn(ILOAD, 2); |
| 1218 |
mv.visitVarInsn(ILOAD, 3); |
| 1219 |
mv.visitInsn(ICONST_1); |
| 1220 |
mv.visitInsn(ISUB); |
| 1221 |
mv.visitVarInsn(ILOAD, 4); |
| 1222 |
mv.visitVarInsn(ALOAD, 5); |
| 1223 |
mv.visitMethodInsn |
| 1224 |
(INVOKESPECIAL, className, "bdbSetField", |
| 1225 |
"(Ljava/lang/Object;IIZLjava/lang/Object;)V"); |
| 1226 |
} |
| 1227 |
mv.visitInsn(RETURN); |
| 1228 |
mv.visitLabel(l0); |
| 1229 |
mv.visitVarInsn(ILOAD, 4); |
| 1230 |
Label l2 = new Label(); |
| 1231 |
mv.visitJumpInsn(IFEQ, l2); |
| 1232 |
Label l1 = new Label(); |
| 1233 |
genSetFieldSwitch(mv, secKeyFields, l1); |
| 1234 |
mv.visitLabel(l2); |
| 1235 |
genSetFieldSwitch(mv, nonKeyFields, l1); |
| 1236 |
mv.visitLabel(l1); |
| 1237 |
mv.visitInsn(RETURN); |
| 1238 |
mv.visitMaxs(2, 6); |
| 1239 |
mv.visitEnd(); |
| 1240 |
} |
| 1241 |
|
| 1242 |
/** |
| 1243 |
* public void bdbSetPriField(Object o, Object value) { |
| 1244 |
* if (priKeyField != null) { |
| 1245 |
* thisField = (TheFieldClass) value; |
| 1246 |
* } else if (super != null) { |
| 1247 |
* // if has superclass: |
| 1248 |
* super.bdbSetPriField(o, value) |
| 1249 |
* } |
| 1250 |
* } |
| 1251 |
*/ |
| 1252 |
private void genBdbSetPriField() { |
| 1253 |
MethodVisitor mv = cv.visitMethod |
| 1254 |
(ACC_PUBLIC, "bdbSetPriField", |
| 1255 |
"(Ljava/lang/Object;Ljava/lang/Object;)V", null, null); |
| 1256 |
mv.visitCode(); |
| 1257 |
if (priKeyField != null) { |
| 1258 |
mv.visitVarInsn(ALOAD, 0); |
| 1259 |
mv.visitVarInsn(ALOAD, 2); |
| 1260 |
if (isRefType(priKeyField.type)) { |
| 1261 |
mv.visitTypeInsn(CHECKCAST, getTypeInstName(priKeyField.type)); |
| 1262 |
} else { |
| 1263 |
int sort = priKeyField.type.getSort(); |
| 1264 |
mv.visitTypeInsn |
| 1265 |
(CHECKCAST, |
| 1266 |
getPrimitiveWrapperClass(sort).replace('.', '/')); |
| 1267 |
genUnwrapPrimitive(mv, sort); |
| 1268 |
} |
| 1269 |
mv.visitFieldInsn |
| 1270 |
(PUTFIELD, className, priKeyField.name, |
| 1271 |
priKeyField.type.getDescriptor()); |
| 1272 |
} else if (hasPersistentSuperclass) { |
| 1273 |
mv.visitVarInsn(ALOAD, 0); |
| 1274 |
mv.visitVarInsn(ALOAD, 1); |
| 1275 |
mv.visitVarInsn(ALOAD, 2); |
| 1276 |
mv.visitMethodInsn |
| 1277 |
(INVOKESPECIAL, superclassName, "bdbSetPriField", |
| 1278 |
"(Ljava/lang/Object;Ljava/lang/Object;)V"); |
| 1279 |
} |
| 1280 |
mv.visitInsn(RETURN); |
| 1281 |
mv.visitMaxs(3, 3); |
| 1282 |
mv.visitEnd(); |
| 1283 |
} |
| 1284 |
|
| 1285 |
/** |
| 1286 |
* mv.visitVarInsn(ILOAD, 2); |
| 1287 |
* Label l0 = new Label(); |
| 1288 |
* Label l1 = new Label(); |
| 1289 |
* Label l2 = new Label(); |
| 1290 |
* mv.visitTableSwitchInsn(0, 2, TheDefLabel, new Label[] { l0, l1, l2 }); |
| 1291 |
* mv.visitLabel(l0); |
| 1292 |
* mv.visitVarInsn(ALOAD, 0); |
| 1293 |
* mv.visitVarInsn(ALOAD, 5); |
| 1294 |
* mv.visitTypeInsn(CHECKCAST, "java/lang/Integer"); |
| 1295 |
* mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Integer", "intValue", |
| 1296 |
* "()I"); |
| 1297 |
* mv.visitFieldInsn(PUTFIELD, TheClassName, "f2", "I"); |
| 1298 |
* mv.visitLabel(l1); |
| 1299 |
* mv.visitVarInsn(ALOAD, 0); |
| 1300 |
* mv.visitVarInsn(ALOAD, 5); |
| 1301 |
* mv.visitTypeInsn(CHECKCAST, "java/lang/String"); |
| 1302 |
* mv.visitFieldInsn(PUTFIELD, TheClassName, "f3", "Ljava/lang/String;"); |
| 1303 |
* mv.visitLabel(l2); |
| 1304 |
* mv.visitVarInsn(ALOAD, 0); |
| 1305 |
* mv.visitVarInsn(ALOAD, 5); |
| 1306 |
* mv.visitTypeInsn(CHECKCAST, "java/lang/String"); |
| 1307 |
* mv.visitFieldInsn(PUTFIELD, TheClassName, "f4", "Ljava/lang/String;"); |
| 1308 |
*/ |
| 1309 |
private void genSetFieldSwitch(MethodVisitor mv, |
| 1310 |
List<FieldInfo> fields, |
| 1311 |
Label defaultLabel) { |
| 1312 |
int nFields = fields.size(); |
| 1313 |
if (nFields == 0) { |
| 1314 |
mv.visitJumpInsn(GOTO, defaultLabel); |
| 1315 |
return; |
| 1316 |
} |
| 1317 |
Label[] labels = new Label[nFields]; |
| 1318 |
for (int i = 0; i < nFields; i += 1) { |
| 1319 |
labels[i] = new Label(); |
| 1320 |
} |
| 1321 |
mv.visitVarInsn(ILOAD, 2); |
| 1322 |
mv.visitTableSwitchInsn(0, nFields - 1, defaultLabel, labels); |
| 1323 |
for (int i = 0; i < nFields; i += 1) { |
| 1324 |
FieldInfo field = fields.get(i); |
| 1325 |
mv.visitLabel(labels[i]); |
| 1326 |
mv.visitVarInsn(ALOAD, 0); |
| 1327 |
mv.visitVarInsn(ALOAD, 5); |
| 1328 |
if (isRefType(field.type)) { |
| 1329 |
mv.visitTypeInsn(CHECKCAST, getTypeInstName(field.type)); |
| 1330 |
} else { |
| 1331 |
int sort = field.type.getSort(); |
| 1332 |
mv.visitTypeInsn |
| 1333 |
(CHECKCAST, |
| 1334 |
getPrimitiveWrapperClass(sort).replace('.', '/')); |
| 1335 |
genUnwrapPrimitive(mv, sort); |
| 1336 |
} |
| 1337 |
mv.visitFieldInsn |
| 1338 |
(PUTFIELD, className, field.name, field.type.getDescriptor()); |
| 1339 |
mv.visitInsn(RETURN); |
| 1340 |
} |
| 1341 |
} |
| 1342 |
|
| 1343 |
private void genWritePrimitive(MethodVisitor mv, int sort) { |
| 1344 |
switch (sort) { |
| 1345 |
case Type.BOOLEAN: |
| 1346 |
mv.visitMethodInsn |
| 1347 |
(INVOKEINTERFACE, "com/sleepycat/persist/impl/EntityOutput", |
| 1348 |
"writeBoolean", "(Z)Lcom/sleepycat/bind/tuple/TupleOutput;"); |
| 1349 |
break; |
| 1350 |
case Type.CHAR: |
| 1351 |
mv.visitMethodInsn |
| 1352 |
(INVOKEINTERFACE, "com/sleepycat/persist/impl/EntityOutput", |
| 1353 |
"writeChar", "(I)Lcom/sleepycat/bind/tuple/TupleOutput;"); |
| 1354 |
break; |
| 1355 |
case Type.BYTE: |
| 1356 |
mv.visitMethodInsn |
| 1357 |
(INVOKEINTERFACE, "com/sleepycat/persist/impl/EntityOutput", |
| 1358 |
"writeByte", "(I)Lcom/sleepycat/bind/tuple/TupleOutput;"); |
| 1359 |
break; |
| 1360 |
case Type.SHORT: |
| 1361 |
mv.visitMethodInsn |
| 1362 |
(INVOKEINTERFACE, "com/sleepycat/persist/impl/EntityOutput", |
| 1363 |
"writeShort", "(I)Lcom/sleepycat/bind/tuple/TupleOutput;"); |
| 1364 |
break; |
| 1365 |
case Type.INT: |
| 1366 |
mv.visitMethodInsn |
| 1367 |
(INVOKEINTERFACE, "com/sleepycat/persist/impl/EntityOutput", |
| 1368 |
"writeInt", "(I)Lcom/sleepycat/bind/tuple/TupleOutput;"); |
| 1369 |
break; |
| 1370 |
case Type.LONG: |
| 1371 |
mv.visitMethodInsn |
| 1372 |
(INVOKEINTERFACE, "com/sleepycat/persist/impl/EntityOutput", |
| 1373 |
"writeLong", "(J)Lcom/sleepycat/bind/tuple/TupleOutput;"); |
| 1374 |
break; |
| 1375 |
case Type.FLOAT: |
| 1376 |
mv.visitMethodInsn |
| 1377 |
(INVOKEINTERFACE, "com/sleepycat/persist/impl/EntityOutput", |
| 1378 |
"writeSortedFloat", |
| 1379 |
"(F)Lcom/sleepycat/bind/tuple/TupleOutput;"); |
| 1380 |
break; |
| 1381 |
case Type.DOUBLE: |
| 1382 |
mv.visitMethodInsn |
| 1383 |
(INVOKEINTERFACE, "com/sleepycat/persist/impl/EntityOutput", |
| 1384 |
"writeSortedDouble", |
| 1385 |
"(D)Lcom/sleepycat/bind/tuple/TupleOutput;"); |
| 1386 |
break; |
| 1387 |
default: |
| 1388 |
throw DbCompat.unexpectedState(String.valueOf(sort)); |
| 1389 |
} |
| 1390 |
/* The write methods always return 'this' and we always discard it. */ |
| 1391 |
mv.visitInsn(POP); |
| 1392 |
} |
| 1393 |
|
| 1394 |
private void genReadPrimitive(MethodVisitor mv, int sort) { |
| 1395 |
switch (sort) { |
| 1396 |
case Type.BOOLEAN: |
| 1397 |
mv.visitMethodInsn |
| 1398 |
(INVOKEINTERFACE, "com/sleepycat/persist/impl/EntityInput", |
| 1399 |
"readBoolean", "()Z"); |
| 1400 |
break; |
| 1401 |
case Type.CHAR: |
| 1402 |
mv.visitMethodInsn |
| 1403 |
(INVOKEINTERFACE, "com/sleepycat/persist/impl/EntityInput", |
| 1404 |
"readChar", "()C"); |
| 1405 |
break; |
| 1406 |
case Type.BYTE: |
| 1407 |
mv.visitMethodInsn |
| 1408 |
(INVOKEINTERFACE, "com/sleepycat/persist/impl/EntityInput", |
| 1409 |
"readByte", "()B"); |
| 1410 |
break; |
| 1411 |
case Type.SHORT: |
| 1412 |
mv.visitMethodInsn |
| 1413 |
(INVOKEINTERFACE, "com/sleepycat/persist/impl/EntityInput", |
| 1414 |
"readShort", "()S"); |
| 1415 |
break; |
| 1416 |
case Type.INT: |
| 1417 |
mv.visitMethodInsn |
| 1418 |
(INVOKEINTERFACE, "com/sleepycat/persist/impl/EntityInput", |
| 1419 |
"readInt", "()I"); |
| 1420 |
break; |
| 1421 |
case Type.LONG: |
| 1422 |
mv.visitMethodInsn |
| 1423 |
(INVOKEINTERFACE, "com/sleepycat/persist/impl/EntityInput", |
| 1424 |
"readLong", "()J"); |
| 1425 |
break; |
| 1426 |
case Type.FLOAT: |
| 1427 |
mv.visitMethodInsn |
| 1428 |
(INVOKEINTERFACE, "com/sleepycat/persist/impl/EntityInput", |
| 1429 |
"readSortedFloat", "()F"); |
| 1430 |
break; |
| 1431 |
case Type.DOUBLE: |
| 1432 |
mv.visitMethodInsn |
| 1433 |
(INVOKEINTERFACE, "com/sleepycat/persist/impl/EntityInput", |
| 1434 |
"readSortedDouble", "()D"); |
| 1435 |
break; |
| 1436 |
default: |
| 1437 |
throw DbCompat.unexpectedState(String.valueOf(sort)); |
| 1438 |
} |
| 1439 |
} |
| 1440 |
|
| 1441 |
private void genWrapPrimitive(MethodVisitor mv, int sort) { |
| 1442 |
switch (sort) { |
| 1443 |
case Type.BOOLEAN: |
| 1444 |
mv.visitMethodInsn |
| 1445 |
(INVOKESTATIC, "java/lang/Boolean", "valueOf", |
| 1446 |
"(Z)Ljava/lang/Boolean;"); |
| 1447 |
break; |
| 1448 |
case Type.CHAR: |
| 1449 |
mv.visitMethodInsn |
| 1450 |
(INVOKESTATIC, "java/lang/Character", "valueOf", |
| 1451 |
"(C)Ljava/lang/Character;"); |
| 1452 |
break; |
| 1453 |
case Type.BYTE: |
| 1454 |
mv.visitMethodInsn |
| 1455 |
(INVOKESTATIC, "java/lang/Byte", "valueOf", |
| 1456 |
"(B)Ljava/lang/Byte;"); |
| 1457 |
break; |
| 1458 |
case Type.SHORT: |
| 1459 |
mv.visitMethodInsn |
| 1460 |
(INVOKESTATIC, "java/lang/Short", "valueOf", |
| 1461 |
"(S)Ljava/lang/Short;"); |
| 1462 |
break; |
| 1463 |
case Type.INT: |
| 1464 |
mv.visitMethodInsn |
| 1465 |
(INVOKESTATIC, "java/lang/Integer", "valueOf", |
| 1466 |
"(I)Ljava/lang/Integer;"); |
| 1467 |
break; |
| 1468 |
case Type.LONG: |
| 1469 |
mv.visitMethodInsn |
| 1470 |
(INVOKESTATIC, "java/lang/Long", "valueOf", |
| 1471 |
"(J)Ljava/lang/Long;"); |
| 1472 |
break; |
| 1473 |
case Type.FLOAT: |
| 1474 |
mv.visitMethodInsn |
| 1475 |
(INVOKESTATIC, "java/lang/Float", "valueOf", |
| 1476 |
"(F)Ljava/lang/Float;"); |
| 1477 |
break; |
| 1478 |
case Type.DOUBLE: |
| 1479 |
mv.visitMethodInsn |
| 1480 |
(INVOKESTATIC, "java/lang/Double", "valueOf", |
| 1481 |
"(D)Ljava/lang/Double;"); |
| 1482 |
break; |
| 1483 |
default: |
| 1484 |
throw DbCompat.unexpectedState(String.valueOf(sort)); |
| 1485 |
} |
| 1486 |
} |
| 1487 |
|
| 1488 |
private void genUnwrapPrimitive(MethodVisitor mv, int sort) { |
| 1489 |
switch (sort) { |
| 1490 |
case Type.BOOLEAN: |
| 1491 |
mv.visitMethodInsn |
| 1492 |
(INVOKEVIRTUAL, "java/lang/Boolean", "booleanValue", "()Z"); |
| 1493 |
break; |
| 1494 |
case Type.CHAR: |
| 1495 |
mv.visitMethodInsn |
| 1496 |
(INVOKEVIRTUAL, "java/lang/Character", "charValue", "()C"); |
| 1497 |
break; |
| 1498 |
case Type.BYTE: |
| 1499 |
mv.visitMethodInsn |
| 1500 |
(INVOKEVIRTUAL, "java/lang/Byte", "byteValue", "()B"); |
| 1501 |
break; |
| 1502 |
case Type.SHORT: |
| 1503 |
mv.visitMethodInsn |
| 1504 |
(INVOKEVIRTUAL, "java/lang/Short", "shortValue", "()S"); |
| 1505 |
break; |
| 1506 |
case Type.INT: |
| 1507 |
mv.visitMethodInsn |
| 1508 |
(INVOKEVIRTUAL, "java/lang/Integer", "intValue", "()I"); |
| 1509 |
break; |
| 1510 |
case Type.LONG: |
| 1511 |
mv.visitMethodInsn |
| 1512 |
(INVOKEVIRTUAL, "java/lang/Long", "longValue", "()J"); |
| 1513 |
break; |
| 1514 |
case Type.FLOAT: |
| 1515 |
mv.visitMethodInsn |
| 1516 |
(INVOKEVIRTUAL, "java/lang/Float", "floatValue", "()F"); |
| 1517 |
break; |
| 1518 |
case Type.DOUBLE: |
| 1519 |
mv.visitMethodInsn |
| 1520 |
(INVOKEVIRTUAL, "java/lang/Double", "doubleValue", "()D"); |
| 1521 |
break; |
| 1522 |
default: |
| 1523 |
throw DbCompat.unexpectedState(String.valueOf(sort)); |
| 1524 |
} |
| 1525 |
} |
| 1526 |
|
| 1527 |
/** |
| 1528 |
* Returns the type name for a visitTypeInsn operand, which is the internal |
| 1529 |
* name for an object type and the descriptor for an array type. Must not |
| 1530 |
* be called for a non-reference type. |
| 1531 |
*/ |
| 1532 |
private static String getTypeInstName(Type type) { |
| 1533 |
if (type.getSort() == Type.OBJECT) { |
| 1534 |
return type.getInternalName(); |
| 1535 |
} else if (type.getSort() == Type.ARRAY) { |
| 1536 |
return type.getDescriptor(); |
| 1537 |
} else { |
| 1538 |
throw DbCompat.unexpectedState(); |
| 1539 |
} |
| 1540 |
} |
| 1541 |
|
| 1542 |
/** |
| 1543 |
* Call this method before comparing a non-reference operand to zero as an |
| 1544 |
* int, for example, with IFNE, IFEQ, IFLT, etc. If the operand is a long, |
| 1545 |
* float or double, this method will compare it to zero and leave the |
| 1546 |
* result as an int operand. |
| 1547 |
*/ |
| 1548 |
private static void genBeforeCompareToZero(MethodVisitor mv, Type type) { |
| 1549 |
switch (type.getSort()) { |
| 1550 |
case Type.LONG: |
| 1551 |
mv.visitInsn(LCONST_0); |
| 1552 |
mv.visitInsn(LCMP); |
| 1553 |
break; |
| 1554 |
case Type.FLOAT: |
| 1555 |
mv.visitInsn(FCONST_0); |
| 1556 |
mv.visitInsn(FCMPL); |
| 1557 |
break; |
| 1558 |
case Type.DOUBLE: |
| 1559 |
mv.visitInsn(DCONST_0); |
| 1560 |
mv.visitInsn(DCMPL); |
| 1561 |
break; |
| 1562 |
} |
| 1563 |
} |
| 1564 |
|
| 1565 |
/** |
| 1566 |
* Returns true if the given class is a primitive wrapper, Date or String. |
| 1567 |
*/ |
| 1568 |
static boolean isSimpleRefType(String className) { |
| 1569 |
return (PRIMITIVE_WRAPPERS.containsKey(className) || |
| 1570 |
className.equals(BigInteger.class.getName()) || |
| 1571 |
className.equals(BigDecimal.class.getName()) || |
| 1572 |
className.equals(Date.class.getName()) || |
| 1573 |
className.equals(String.class.getName())); |
| 1574 |
} |
| 1575 |
|
| 1576 |
/** |
| 1577 |
* Returns the wrapper class for a primitive. |
| 1578 |
*/ |
| 1579 |
static String getPrimitiveWrapperClass(int primitiveSort) { |
| 1580 |
for (Map.Entry<String, Integer> entry : |
| 1581 |
PRIMITIVE_WRAPPERS.entrySet()) { |
| 1582 |
if (entry.getValue() == primitiveSort) { |
| 1583 |
return entry.getKey(); |
| 1584 |
} |
| 1585 |
} |
| 1586 |
throw DbCompat.unexpectedState(String.valueOf(primitiveSort)); |
| 1587 |
} |
| 1588 |
|
| 1589 |
/** |
| 1590 |
* Returns true if the given type is an object or array. |
| 1591 |
*/ |
| 1592 |
private static boolean isRefType(Type type) { |
| 1593 |
int sort = type.getSort(); |
| 1594 |
return (sort == Type.OBJECT || sort == Type.ARRAY); |
| 1595 |
} |
| 1596 |
|
| 1597 |
/** |
| 1598 |
* Returns whether a string array contains a given string. |
| 1599 |
*/ |
| 1600 |
private static boolean containsString(String[] a, String s) { |
| 1601 |
if (a != null) { |
| 1602 |
for (String t : a) { |
| 1603 |
if (s.equals(t)) { |
| 1604 |
return true; |
| 1605 |
} |
| 1606 |
} |
| 1607 |
} |
| 1608 |
return false; |
| 1609 |
} |
| 1610 |
|
| 1611 |
/** |
| 1612 |
* Appends a string to a string array. |
| 1613 |
*/ |
| 1614 |
private static String[] appendString(String[] a, String s) { |
| 1615 |
if (a != null) { |
| 1616 |
int len = a.length; |
| 1617 |
String[] a2 = new String[len + 1]; |
| 1618 |
System.arraycopy(a, 0, a2, 0, len); |
| 1619 |
a2[len] = s; |
| 1620 |
return a2; |
| 1621 |
} else { |
| 1622 |
return new String[] { s }; |
| 1623 |
} |
| 1624 |
} |
| 1625 |
|
| 1626 |
/** |
| 1627 |
* Aborts the enhancement process when we determine that enhancement is |
| 1628 |
* unnecessary or not possible. |
| 1629 |
*/ |
| 1630 |
private NotPersistentException abort() { |
| 1631 |
return NOT_PERSISTENT; |
| 1632 |
} |
| 1633 |
|
| 1634 |
private static class FieldInfo extends FieldVisitor { |
| 1635 |
|
| 1636 |
FieldVisitor parent; |
| 1637 |
String name; |
| 1638 |
Type type; |
| 1639 |
OrderInfo order; |
| 1640 |
boolean isPriKey; |
| 1641 |
boolean isSecKey; |
| 1642 |
boolean isTransient; |
| 1643 |
boolean isString = false; |
| 1644 |
|
| 1645 |
FieldInfo(FieldVisitor parent, |
| 1646 |
String name, |
| 1647 |
String desc, |
| 1648 |
boolean isTransient) { |
| 1649 |
super(ASM4); |
| 1650 |
this.parent = parent; |
| 1651 |
this.name = name; |
| 1652 |
this.isTransient = isTransient; |
| 1653 |
type = Type.getType(desc); |
| 1654 |
if (type.getClassName().equals(String.class.getName())) { |
| 1655 |
isString = true; |
| 1656 |
} |
| 1657 |
} |
| 1658 |
|
| 1659 |
public AnnotationVisitor visitAnnotation(String desc, |
| 1660 |
boolean visible) { |
| 1661 |
AnnotationVisitor ret = parent.visitAnnotation(desc, visible); |
| 1662 |
if (desc.equals |
| 1663 |
("Lcom/sleepycat/persist/model/KeyField;")) { |
| 1664 |
order = new OrderInfo(ret); |
| 1665 |
ret = order; |
| 1666 |
} else if (desc.equals |
| 1667 |
("Lcom/sleepycat/persist/model/PrimaryKey;")) { |
| 1668 |
isPriKey = true; |
| 1669 |
} else if (desc.equals |
| 1670 |
("Lcom/sleepycat/persist/model/SecondaryKey;")) { |
| 1671 |
isSecKey = true; |
| 1672 |
} else if (desc.equals |
| 1673 |
("Lcom/sleepycat/persist/model/NotPersistent;")) { |
| 1674 |
isTransient = true; |
| 1675 |
} else if (desc.equals |
| 1676 |
("Lcom/sleepycat/persist/model/NotTransient;")) { |
| 1677 |
isTransient = false; |
| 1678 |
} |
| 1679 |
return ret; |
| 1680 |
} |
| 1681 |
|
| 1682 |
public void visitAttribute(Attribute attr) { |
| 1683 |
parent.visitAttribute(attr); |
| 1684 |
} |
| 1685 |
|
| 1686 |
public void visitEnd() { |
| 1687 |
parent.visitEnd(); |
| 1688 |
} |
| 1689 |
|
| 1690 |
@Override |
| 1691 |
public String toString() { |
| 1692 |
String label; |
| 1693 |
if (isPriKey) { |
| 1694 |
label = "PrimaryKey"; |
| 1695 |
} else if (isSecKey) { |
| 1696 |
label = "SecondaryKey"; |
| 1697 |
} else if (order != null) { |
| 1698 |
label = "CompositeKeyField " + order.value; |
| 1699 |
} else { |
| 1700 |
label = "NonKeyField"; |
| 1701 |
} |
| 1702 |
return "[" + label + ' ' + name + ' ' + type + ']'; |
| 1703 |
} |
| 1704 |
} |
| 1705 |
|
| 1706 |
private static class OrderInfo extends AnnotationInfo { |
| 1707 |
|
| 1708 |
int value; |
| 1709 |
|
| 1710 |
OrderInfo(AnnotationVisitor parent) { |
| 1711 |
super(parent); |
| 1712 |
} |
| 1713 |
|
| 1714 |
@Override |
| 1715 |
public void visit(String name, Object value) { |
| 1716 |
if (name.equals("value")) { |
| 1717 |
this.value = (Integer) value; |
| 1718 |
} |
| 1719 |
parent.visit(name, value); |
| 1720 |
} |
| 1721 |
} |
| 1722 |
|
| 1723 |
private static abstract class AnnotationInfo extends AnnotationVisitor { |
| 1724 |
|
| 1725 |
AnnotationVisitor parent; |
| 1726 |
|
| 1727 |
AnnotationInfo(AnnotationVisitor parent) { |
| 1728 |
super(ASM4); |
| 1729 |
this.parent = parent; |
| 1730 |
} |
| 1731 |
|
| 1732 |
public void visit(String name, Object value) { |
| 1733 |
parent.visit(name, value); |
| 1734 |
} |
| 1735 |
|
| 1736 |
public AnnotationVisitor visitAnnotation(String name, String desc) { |
| 1737 |
return parent.visitAnnotation(name, desc); |
| 1738 |
} |
| 1739 |
|
| 1740 |
public AnnotationVisitor visitArray(String name) { |
| 1741 |
return parent.visitArray(name); |
| 1742 |
} |
| 1743 |
|
| 1744 |
public void visitEnum(String name, String desc, String value) { |
| 1745 |
parent.visitEnum(name, desc, value); |
| 1746 |
} |
| 1747 |
|
| 1748 |
public void visitEnd() { |
| 1749 |
parent.visitEnd(); |
| 1750 |
} |
| 1751 |
} |
| 1752 |
} |