String fieldName = targetField.getName();
InstructionHandle jumpTarget;
il.append(InstructionConstants.ALOAD_0);
il.append(factory.createGetField(className, ClassEnhancer.FN_StateManager, BCELClassEnhancer.OT_StateManager));
BranchInstruction jump2 = new IFNULL(null);
il.append(jump2);
il.append(InstructionConstants.ALOAD_0);
il.append(factory.createGetField(className, ClassEnhancer.FN_StateManager, BCELClassEnhancer.OT_StateManager));
il.append(InstructionConstants.ALOAD_0);
il.append(BCELUtils.getBIPUSH(fieldConfig.getFieldId()));
if (cmd.getPersistenceCapableSuperclass() != null)
{
il.append(factory.createGetStatic(className, ClassEnhancer.FN_JdoInheritedFieldCount, Type.INT));
il.append(InstructionConstants.IADD);
}
il.append(factory.createInvoke(ClassEnhancer.CN_StateManager, "isLoaded",
Type.BOOLEAN, new Type[]{BCELClassEnhancer.OT_PersistenceCapable, Type.INT}, Constants.INVOKEINTERFACE));
BranchInstruction jump3 = new IFNE(null);
il.append(jump3);
// statemanager.getXXXfield();
il.append(InstructionConstants.ALOAD_0);
il.append(factory.createGetField(className, ClassEnhancer.FN_StateManager, BCELClassEnhancer.OT_StateManager));
il.append(InstructionConstants.ALOAD_0);
il.append(BCELUtils.getBIPUSH(fieldConfig.getFieldId()));
if (cmd.getPersistenceCapableSuperclass() != null)
{
il.append(factory.createGetStatic(className, ClassEnhancer.FN_JdoInheritedFieldCount, Type.INT));
il.append(InstructionConstants.IADD);
}
il.append(InstructionConstants.ALOAD_0);
il.append(factory.createGetField(className, fieldName, nativeType));
il.append(factory.createInvoke(ClassEnhancer.CN_StateManager, "get" + BCELUtils.getJDOMethodName(targetField.getType()) + "Field", smType, new Type[]{
BCELClassEnhancer.OT_PersistenceCapable, Type.INT, smType}, Constants.INVOKEINTERFACE));
if (nativeType instanceof ReferenceType)
{
String type = null;
if (nativeType instanceof ArrayType)
type = nativeType.getSignature();
else
type = nativeType.toString();
il.append(new CHECKCAST(constantPoolGen.addClass(type)));
}
il.append(InstructionFactory.createReturn(nativeType));
// ----detach------
if (cmd.isDetachable())
{
// jdoIsDetached()
jumpTarget = il.append(InstructionConstants.ALOAD_0);
il.append(factory.createInvoke(ClassEnhancer.CN_PersistenceCapable, ClassEnhancer.MN_JdoIsDetached,
Type.BOOLEAN, Type.NO_ARGS, Constants.INVOKEINTERFACE));
// 0
il.append(InstructionConstants.ICONST_0);
// if (jdoIsDetached() == 0)
IF_ICMPEQ ifIsDetached = new IF_ICMPEQ(null);
il.append(ifIsDetached);
// if (((BitSet)jdoDetachedState[2]).get(?) == 1)
// (BitSet)jdoDetachedState[2]
ifIsDetached.setTarget(il.append(InstructionConstants.ALOAD_0));
il.append(factory.createGetField(className, ClassEnhancer.FN_JdoDetachedState, BCELClassEnhancer.OT_ObjectArray));
il.append(InstructionConstants.ICONST_2);
il.append(InstructionConstants.AALOAD);
il.append(factory.createCheckCast(BCELClassEnhancer.OT_BitSet)); // Cast to BitSet
// the field index: 0, 1, 2...
il.append(BCELUtils.getBIPUSH(fieldConfig.getFieldId()));
if (cmd.getPersistenceCapableSuperclass() != null)
{
// add to field index the parentFieldCount
il.append(factory.createGetStatic(className, ClassEnhancer.FN_JdoInheritedFieldCount, Type.INT));
il.append(InstructionConstants.IADD);
}
// get(?)
il.append(factory.createInvoke(ClassEnhancer.CN_BitSet, "get", Type.BOOLEAN, new Type[]{Type.INT}, Constants.INVOKEVIRTUAL));
// 1
il.append(InstructionConstants.ICONST_1);
// if (jdoDetachedState[2].get(?) == 1)
IF_ICMPEQ ifLoaded = new IF_ICMPEQ(null);
il.append(ifLoaded);
// if (jdoDetachedState[3].get(?) == 1 )
// (BitSet)jdoDetachedState[3]
ifLoaded.setTarget(il.append(InstructionConstants.ALOAD_0));
il.append(factory.createGetField(className, ClassEnhancer.FN_JdoDetachedState, BCELClassEnhancer.OT_ObjectArray));
il.append(InstructionConstants.ICONST_3);
il.append(InstructionConstants.AALOAD);
il.append(factory.createCheckCast(BCELClassEnhancer.OT_BitSet)); // Cast to BitSet
// the field index: 0, 1, 2...
il.append(BCELUtils.getBIPUSH(fieldConfig.getFieldId()));
if (cmd.getPersistenceCapableSuperclass() != null)
{
// add to field index the parentFieldCount
il.append(factory.createGetStatic(className, ClassEnhancer.FN_JdoInheritedFieldCount, Type.INT));
il.append(InstructionConstants.IADD);
}
// get(?)
il.append(factory.createInvoke(ClassEnhancer.CN_BitSet, "get", Type.BOOLEAN, new Type[]{Type.INT}, Constants.INVOKEVIRTUAL));
// 1
il.append(InstructionConstants.ICONST_1);
// if (jdoDetachedState[3].get(?) == 1)
IF_ICMPEQ ifModified = new IF_ICMPEQ(null);
il.append(ifModified);
// throw new JDODetachedFieldAccessException("XXXX was not loaded.");
createThrowException(ClassEnhancer.CN_JDODetachedFieldAccessException, LOCALISER.msg("Enhancer.DetachedFieldAccess", fieldName));
// ----detach------
// return <target field>
InstructionHandle jumpTarget2 = il.append(InstructionConstants.ALOAD_0);
il.append(factory.createGetField(className, fieldName, nativeType));
il.append(InstructionFactory.createReturn(nativeType));
jump2.setTarget(jumpTarget);
jump3.setTarget(jumpTarget);
ifIsDetached.setTarget(jumpTarget2);
ifLoaded.setTarget(jumpTarget2);
ifModified.setTarget(jumpTarget2);
}
else
{
// return <target field>
jumpTarget = il.append(InstructionConstants.ALOAD_0);
il.append(factory.createGetField(className, fieldName, nativeType));
il.append(InstructionFactory.createReturn(nativeType));
jump2.setTarget(jumpTarget);
jump3.setTarget(jumpTarget);
}
}