/** * public void bdbSetField(Object o, int field, int superLevel, boolean isSecField, Object value) * { if (superLevel > 0) { // if has superclass: super.bdbSetField (o, field, superLevel - 1, * isSecField, value); } else if (isSecField) { switch (field) { case 0: f2 = ((Integer) * value).intValue(); case 1: f3 = (String) value; case 2: f4 = (String) value; } } else { switch * (field) { case 0: f5 = ((Integer) value).intValue(); case 1: f6 = (String) value; case 2: f7 = * (String) value; } } } */ private void genBdbSetField() { MethodVisitor mv = cv.visitMethod( ACC_PUBLIC, "bdbSetField", "(Ljava/lang/Object;IIZLjava/lang/Object;)V", null, null); mv.visitCode(); mv.visitVarInsn(ILOAD, 3); Label l0 = new Label(); mv.visitJumpInsn(IFLE, l0); if (hasPersistentSuperclass) { mv.visitVarInsn(ALOAD, 0); mv.visitVarInsn(ALOAD, 1); mv.visitVarInsn(ILOAD, 2); mv.visitVarInsn(ILOAD, 3); mv.visitInsn(ICONST_1); mv.visitInsn(ISUB); mv.visitVarInsn(ILOAD, 4); mv.visitVarInsn(ALOAD, 5); mv.visitMethodInsn( INVOKESPECIAL, className, "bdbSetField", "(Ljava/lang/Object;IIZLjava/lang/Object;)V"); } mv.visitInsn(RETURN); mv.visitLabel(l0); mv.visitVarInsn(ILOAD, 4); Label l2 = new Label(); mv.visitJumpInsn(IFEQ, l2); Label l1 = new Label(); genSetFieldSwitch(mv, secKeyFields, l1); mv.visitLabel(l2); genSetFieldSwitch(mv, nonKeyFields, l1); mv.visitLabel(l1); mv.visitInsn(RETURN); mv.visitMaxs(2, 6); mv.visitEnd(); }
/** * public boolean bdbIsPriKeyFieldNullOrZero() { return theField == null; // or zero or false, as * appropriate // or if no primary key but has superclass: return * super.bdbIsPriKeyFieldNullOrZero(); } */ private void genBdbIsPriKeyFieldNullOrZero() { MethodVisitor mv = cv.visitMethod(ACC_PUBLIC, "bdbIsPriKeyFieldNullOrZero", "()Z", null, null); mv.visitCode(); if (priKeyField != null) { mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, className, priKeyField.name, priKeyField.type.getDescriptor()); Label l0 = new Label(); if (isRefType(priKeyField.type)) { mv.visitJumpInsn(IFNONNULL, l0); } else { genBeforeCompareToZero(mv, priKeyField.type); mv.visitJumpInsn(IFNE, l0); } mv.visitInsn(ICONST_1); Label l1 = new Label(); mv.visitJumpInsn(GOTO, l1); mv.visitLabel(l0); mv.visitInsn(ICONST_0); mv.visitLabel(l1); } else if (hasPersistentSuperclass) { mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn(INVOKESPECIAL, superclassName, "bdbIsPriKeyFieldNullOrZero", "()Z"); } else { mv.visitInsn(ICONST_0); } mv.visitInsn(IRETURN); mv.visitMaxs(1, 1); mv.visitEnd(); }
/** * public void bdbReadXxxKeyFields(EntityInput input, int startField, int endField, int * superLevel) { // ... if (superLevel <= 0) { switch (startField) { case 0: keyField1 = * input.readInt(); if (endField == 0) break; case 1: keyField2 = (String) input.readObject(); if * (endField == 1) break; case 2: keyField3 = input.readInt(); } } */ private void genReadFieldSwitch(MethodVisitor mv, List<FieldInfo> fields) { int nFields = fields.size(); if (nFields > 0) { mv.visitVarInsn(ILOAD, 4); Label pastSwitch = new Label(); mv.visitJumpInsn(IFGT, pastSwitch); Label[] labels = new Label[nFields]; for (int i = 0; i < nFields; i += 1) { labels[i] = new Label(); } mv.visitVarInsn(ILOAD, 2); mv.visitTableSwitchInsn(0, nFields - 1, pastSwitch, labels); for (int i = 0; i < nFields; i += 1) { FieldInfo field = fields.get(i); mv.visitLabel(labels[i]); genReadField(mv, field); if (i < nFields - 1) { Label nextCase = labels[i + 1]; mv.visitVarInsn(ILOAD, 3); if (i == 0) { mv.visitJumpInsn(IFNE, nextCase); } else { switch (i) { case 1: mv.visitInsn(ICONST_1); break; case 2: mv.visitInsn(ICONST_2); break; case 3: mv.visitInsn(ICONST_3); break; case 4: mv.visitInsn(ICONST_4); break; case 5: mv.visitInsn(ICONST_5); break; default: mv.visitIntInsn(BIPUSH, i); } mv.visitJumpInsn(IF_ICMPNE, nextCase); } mv.visitJumpInsn(GOTO, pastSwitch); } } mv.visitLabel(pastSwitch); } }
/** * mv.visitVarInsn(ILOAD, 2); Label l0 = new Label(); Label l1 = new Label(); Label l2 = new * Label(); mv.visitTableSwitchInsn(0, 2, TheDefLabel, new Label[] { l0, l1, l2 }); * mv.visitLabel(l0); mv.visitVarInsn(ALOAD, 0); mv.visitVarInsn(ALOAD, 5); * mv.visitTypeInsn(CHECKCAST, "java/lang/Integer"); mv.visitMethodInsn(INVOKEVIRTUAL, * "java/lang/Integer", "intValue", "()I"); mv.visitFieldInsn(PUTFIELD, TheClassName, "f2", "I"); * mv.visitLabel(l1); mv.visitVarInsn(ALOAD, 0); mv.visitVarInsn(ALOAD, 5); * mv.visitTypeInsn(CHECKCAST, "java/lang/String"); mv.visitFieldInsn(PUTFIELD, TheClassName, * "f3", "Ljava/lang/String;"); mv.visitLabel(l2); mv.visitVarInsn(ALOAD, 0); * mv.visitVarInsn(ALOAD, 5); mv.visitTypeInsn(CHECKCAST, "java/lang/String"); * mv.visitFieldInsn(PUTFIELD, TheClassName, "f4", "Ljava/lang/String;"); */ private void genSetFieldSwitch(MethodVisitor mv, List<FieldInfo> fields, Label defaultLabel) { int nFields = fields.size(); if (nFields == 0) { mv.visitJumpInsn(GOTO, defaultLabel); return; } Label[] labels = new Label[nFields]; for (int i = 0; i < nFields; i += 1) { labels[i] = new Label(); } mv.visitVarInsn(ILOAD, 2); mv.visitTableSwitchInsn(0, nFields - 1, defaultLabel, labels); for (int i = 0; i < nFields; i += 1) { FieldInfo field = fields.get(i); mv.visitLabel(labels[i]); mv.visitVarInsn(ALOAD, 0); mv.visitVarInsn(ALOAD, 5); if (isRefType(field.type)) { mv.visitTypeInsn(CHECKCAST, getTypeInstName(field.type)); } else { int sort = field.type.getSort(); mv.visitTypeInsn(CHECKCAST, getPrimitiveWrapperClass(sort).replace('.', '/')); genUnwrapPrimitive(mv, sort); } mv.visitFieldInsn(PUTFIELD, className, field.name, field.type.getDescriptor()); mv.visitInsn(RETURN); } }
/** // if has super: if (superLevel != 0) { super.bdbReadXxxKeyFields(..., superLevel - 1); } */ private void genReadSuperKeyFields(MethodVisitor mv, boolean areSecKeyFields) { if (hasPersistentSuperclass) { Label next = new Label(); mv.visitVarInsn(ILOAD, 4); mv.visitJumpInsn(IFEQ, next); mv.visitVarInsn(ALOAD, 0); mv.visitVarInsn(ALOAD, 1); mv.visitVarInsn(ILOAD, 2); mv.visitVarInsn(ILOAD, 3); mv.visitVarInsn(ILOAD, 4); mv.visitInsn(ICONST_1); mv.visitInsn(ISUB); String name = areSecKeyFields ? "bdbReadSecKeyFields" : "bdbReadNonKeyFields"; mv.visitMethodInsn( INVOKESPECIAL, superclassName, name, "(Lcom/sleepycat/persist/impl/EntityInput;III)V"); mv.visitLabel(next); } }
/** * mv.visitVarInsn(ILOAD, 2); Label l0 = new Label(); Label l1 = new Label(); Label l2 = new * Label(); mv.visitTableSwitchInsn(0, 2, TheDefLabel, new Label[] { l0, l1, l2 }); * mv.visitLabel(l0); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, TheClassName, "f2", * "I"); mv.visitMethodInsn(INVOKESTATIC, "java/lang/Integer", "valueOf", * "(I)Ljava/lang/Integer;"); mv.visitInsn(ARETURN); mv.visitLabel(l1); mv.visitVarInsn(ALOAD, 0); * mv.visitFieldInsn(GETFIELD, TheClassName, "f3", "Ljava/lang/String;"); mv.visitInsn(ARETURN); * mv.visitLabel(l2); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, TheClassName, "f4", * "Ljava/lang/String;"); mv.visitInsn(ARETURN); */ private void genGetFieldSwitch(MethodVisitor mv, List<FieldInfo> fields, Label defaultLabel) { int nFields = fields.size(); if (nFields == 0) { mv.visitJumpInsn(GOTO, defaultLabel); return; } Label[] labels = new Label[nFields]; for (int i = 0; i < nFields; i += 1) { labels[i] = new Label(); } mv.visitVarInsn(ILOAD, 2); mv.visitTableSwitchInsn(0, nFields - 1, defaultLabel, labels); for (int i = 0; i < nFields; i += 1) { FieldInfo field = fields.get(i); mv.visitLabel(labels[i]); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, className, field.name, field.type.getDescriptor()); if (!isRefType(field.type)) { genWrapPrimitive(mv, field.type.getSort()); } mv.visitInsn(ARETURN); } }