use of org.datanucleus.metadata.PropertyMetaData in project datanucleus-core by datanucleus.
the class CopyKeyFieldsToObjectId method execute.
/**
* Method to add the contents of the class method.
*/
public void execute() {
visitor.visitCode();
ClassMetaData cmd = enhancer.getClassMetaData();
if (cmd.getIdentityType() == IdentityType.APPLICATION) {
// application identity
if (!cmd.isInstantiable()) {
// Application identity but mapped-superclass with no PK defined, so just "return"
Label startLabel = new Label();
visitor.visitLabel(startLabel);
visitor.visitInsn(Opcodes.RETURN);
Label endLabel = new Label();
visitor.visitLabel(endLabel);
visitor.visitLocalVariable("this", getClassEnhancer().getClassDescriptor(), null, startLabel, endLabel, 0);
visitor.visitLocalVariable(argNames[0], EnhanceUtils.CD_Object, null, startLabel, endLabel, 1);
visitor.visitMaxs(0, 2);
} else {
String objectIdClass = cmd.getObjectidClass();
String ACN_objectIdClass = objectIdClass.replace('.', '/');
if (IdentityUtils.isSingleFieldIdentityClass(objectIdClass)) {
// SingleFieldIdentity
Label startLabel = new Label();
visitor.visitLabel(startLabel);
visitor.visitTypeInsn(Opcodes.NEW, getNamer().getFatalInternalExceptionAsmClassName());
visitor.visitInsn(Opcodes.DUP);
visitor.visitLdcInsn("It's illegal to call dnCopyKeyFieldsToObjectId for a class with single-field identity.");
visitor.visitMethodInsn(Opcodes.INVOKESPECIAL, getNamer().getFatalInternalExceptionAsmClassName(), "<init>", "(Ljava/lang/String;)V");
visitor.visitInsn(Opcodes.ATHROW);
Label endLabel = new Label();
visitor.visitLabel(endLabel);
visitor.visitLocalVariable("this", getClassEnhancer().getClassDescriptor(), null, startLabel, endLabel, 0);
visitor.visitLocalVariable(argNames[0], EnhanceUtils.CD_Object, null, startLabel, endLabel, 1);
visitor.visitMaxs(3, 2);
} else {
// User-provided app identity, and compound identity
// Put try-catch around the field setting (for reflection cases)
Label l0 = new Label();
Label l1 = new Label();
Label l2 = new Label();
visitor.visitTryCatchBlock(l0, l1, l2, "java/lang/Exception");
Label startLabel = new Label();
visitor.visitLabel(startLabel);
visitor.visitVarInsn(Opcodes.ALOAD, 1);
visitor.visitTypeInsn(Opcodes.INSTANCEOF, ACN_objectIdClass);
Label l4 = new Label();
visitor.visitJumpInsn(Opcodes.IFNE, l4);
visitor.visitTypeInsn(Opcodes.NEW, "java/lang/ClassCastException");
visitor.visitInsn(Opcodes.DUP);
visitor.visitLdcInsn("key class is not " + objectIdClass + " or null");
visitor.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/ClassCastException", "<init>", "(Ljava/lang/String;)V");
visitor.visitInsn(Opcodes.ATHROW);
visitor.visitLabel(l4);
visitor.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
visitor.visitVarInsn(Opcodes.ALOAD, 1);
visitor.visitTypeInsn(Opcodes.CHECKCAST, ACN_objectIdClass);
visitor.visitVarInsn(Opcodes.ASTORE, 2);
visitor.visitLabel(l0);
int[] pkFieldNums = enhancer.getClassMetaData().getPKMemberPositions();
Label reflectionFieldStart = null;
for (int i = 0; i < pkFieldNums.length; i++) {
AbstractMemberMetaData fmd = enhancer.getClassMetaData().getMetaDataForManagedMemberAtAbsolutePosition(pkFieldNums[i]);
String fieldTypeDesc = Type.getDescriptor(fmd.getType());
AbstractClassMetaData acmd = enhancer.getMetaDataManager().getMetaDataForClass(fmd.getType(), enhancer.getClassLoaderResolver());
int pkFieldModifiers = ClassUtils.getModifiersForFieldOfClass(enhancer.getClassLoaderResolver(), objectIdClass, fmd.getName());
// Check if the PK field type is a PC (CompoundIdentity)
if (acmd != null && acmd.getIdentityType() != IdentityType.NONDURABLE) {
// CompoundIdentity, this field of the PK is a PC
if (fmd instanceof PropertyMetaData) {
// Persistent Property so use o.setXXX((XXX.Key)JDOHelper.getObjectId(dnGetXXX()))
visitor.visitVarInsn(Opcodes.ALOAD, 2);
visitor.visitVarInsn(Opcodes.ALOAD, 0);
visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, getClassEnhancer().getASMClassName(), getNamer().getGetMethodPrefixMethodName() + fmd.getName(), "()" + Type.getDescriptor(fmd.getType()));
// Note that we swap JDOHelper.getObjectId(obj) for ((Persistable)obj).dnGetObjectId())
visitor.visitMethodInsn(Opcodes.INVOKEINTERFACE, getNamer().getPersistableAsmClassName(), getNamer().getGetObjectIdMethodName(), "()Ljava/lang/Object;", true);
// visitor.visitMethodInsn(Opcodes.INVOKESTATIC, getNamer().getHelperAsmClassName(), "getObjectId", "(Ljava/lang/Object;)Ljava/lang/Object;");
visitor.visitTypeInsn(Opcodes.CHECKCAST, acmd.getObjectidClass().replace('.', '/'));
// TODO Use properties here
visitor.visitFieldInsn(Opcodes.PUTFIELD, ACN_objectIdClass, fmd.getName(), "L" + acmd.getObjectidClass().replace('.', '/') + ";");
} else if (Modifier.isPublic(pkFieldModifiers)) {
// Persistent Field public, so use o.xxx = (XXX.Key)JDOHelper.getObjectId(xxx);
visitor.visitVarInsn(Opcodes.ALOAD, 2);
visitor.visitVarInsn(Opcodes.ALOAD, 0);
visitor.visitFieldInsn(Opcodes.GETFIELD, getClassEnhancer().getASMClassName(), fmd.getName(), fieldTypeDesc);
// Note that we swap JDOHelper.getObjectId(obj) for ((Persistable)obj).dnGetObjectId())
visitor.visitMethodInsn(Opcodes.INVOKEINTERFACE, getNamer().getPersistableAsmClassName(), getNamer().getGetObjectIdMethodName(), "()Ljava/lang/Object;", true);
// visitor.visitMethodInsn(Opcodes.INVOKESTATIC, getNamer().getHelperAsmClassName(), "getObjectId", "(Ljava/lang/Object;)Ljava/lang/Object;");
visitor.visitTypeInsn(Opcodes.CHECKCAST, acmd.getObjectidClass().replace('.', '/'));
visitor.visitFieldInsn(Opcodes.PUTFIELD, ACN_objectIdClass, fmd.getName(), "L" + acmd.getObjectidClass().replace('.', '/') + ";");
} else {
// Persistent Field private/protected so use reflection
// TODO Use reflection here
visitor.visitVarInsn(Opcodes.ALOAD, 2);
visitor.visitVarInsn(Opcodes.ALOAD, 0);
visitor.visitFieldInsn(Opcodes.GETFIELD, getClassEnhancer().getASMClassName(), fmd.getName(), fieldTypeDesc);
// Note that we swap JDOHelper.getObjectId(obj) for ((Persistable)obj).dnGetObjectId())
visitor.visitMethodInsn(Opcodes.INVOKEINTERFACE, getNamer().getPersistableAsmClassName(), getNamer().getGetObjectIdMethodName(), "()Ljava/lang/Object;", true);
// visitor.visitMethodInsn(Opcodes.INVOKESTATIC, getNamer().getHelperAsmClassName(), "getObjectId", "(Ljava/lang/Object;)Ljava/lang/Object;");
visitor.visitTypeInsn(Opcodes.CHECKCAST, acmd.getObjectidClass().replace('.', '/'));
// TODO Use reflection here
visitor.visitFieldInsn(Opcodes.PUTFIELD, ACN_objectIdClass, fmd.getName(), "L" + acmd.getObjectidClass().replace('.', '/') + ";");
}
} else {
// Standard application-identity field
if (fmd instanceof PropertyMetaData) {
// Persistent Property so use o.setXXX(dnGetXXX())
visitor.visitVarInsn(Opcodes.ALOAD, 2);
visitor.visitVarInsn(Opcodes.ALOAD, 0);
visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, getClassEnhancer().getASMClassName(), getNamer().getGetMethodPrefixMethodName() + fmd.getName(), "()" + Type.getDescriptor(fmd.getType()));
visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, ACN_objectIdClass, ClassUtils.getJavaBeanSetterName(fmd.getName()), "(" + fieldTypeDesc + ")V");
} else if (Modifier.isPublic(pkFieldModifiers)) {
// Persistent Field public, so use o.xxx = xxx
visitor.visitVarInsn(Opcodes.ALOAD, 2);
visitor.visitVarInsn(Opcodes.ALOAD, 0);
visitor.visitFieldInsn(Opcodes.GETFIELD, getClassEnhancer().getASMClassName(), fmd.getName(), fieldTypeDesc);
visitor.visitFieldInsn(Opcodes.PUTFIELD, ACN_objectIdClass, fmd.getName(), fieldTypeDesc);
} else {
// Persistent Field private/protected so use reflection, generating
// "Field field = o.getClass().getDeclaredField("pmIDFloat");"
// "field.setAccessible(true);"
// "field.set(o, pmIDFloat);"
visitor.visitVarInsn(Opcodes.ALOAD, 2);
visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Object", "getClass", "()Ljava/lang/Class;");
visitor.visitLdcInsn(fmd.getName());
visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Class", "getDeclaredField", "(Ljava/lang/String;)Ljava/lang/reflect/Field;");
visitor.visitVarInsn(Opcodes.ASTORE, 3);
if (reflectionFieldStart == null) {
reflectionFieldStart = new Label();
visitor.visitLabel(reflectionFieldStart);
}
visitor.visitVarInsn(Opcodes.ALOAD, 3);
visitor.visitInsn(Opcodes.ICONST_1);
visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/reflect/Field", "setAccessible", "(Z)V");
visitor.visitVarInsn(Opcodes.ALOAD, 3);
visitor.visitVarInsn(Opcodes.ALOAD, 2);
visitor.visitVarInsn(Opcodes.ALOAD, 0);
visitor.visitFieldInsn(Opcodes.GETFIELD, getClassEnhancer().getASMClassName(), fmd.getName(), fieldTypeDesc);
if (fmd.getTypeName().equals("boolean")) {
visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/reflect/Field", "setBoolean", "(Ljava/lang/Object;Z)V");
} else if (fmd.getTypeName().equals("byte")) {
visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/reflect/Field", "setByte", "(Ljava/lang/Object;B)V");
} else if (fmd.getTypeName().equals("char")) {
visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/reflect/Field", "setChar", "(Ljava/lang/Object;C)V");
} else if (fmd.getTypeName().equals("double")) {
visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/reflect/Field", "setDouble", "(Ljava/lang/Object;D)V");
} else if (fmd.getTypeName().equals("float")) {
visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/reflect/Field", "setFloat", "(Ljava/lang/Object;F)V");
} else if (fmd.getTypeName().equals("int")) {
visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/reflect/Field", "setInt", "(Ljava/lang/Object;I)V");
} else if (fmd.getTypeName().equals("long")) {
visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/reflect/Field", "setLong", "(Ljava/lang/Object;J)V");
} else if (fmd.getTypeName().equals("short")) {
visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/reflect/Field", "setShort", "(Ljava/lang/Object;S)V");
} else {
visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/reflect/Field", "set", "(Ljava/lang/Object;Ljava/lang/Object;)V");
}
}
}
}
// catch of the try-catch
visitor.visitLabel(l1);
Label l16 = new Label();
visitor.visitJumpInsn(Opcodes.GOTO, l16);
visitor.visitLabel(l2);
visitor.visitFrame(Opcodes.F_FULL, 3, new Object[] { getClassEnhancer().getASMClassName(), "java/lang/Object", ACN_objectIdClass }, 1, new Object[] { "java/lang/Exception" });
visitor.visitVarInsn(Opcodes.ASTORE, 3);
visitor.visitLabel(l16);
visitor.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
visitor.visitInsn(Opcodes.RETURN);
Label endLabel = new Label();
visitor.visitLabel(endLabel);
visitor.visitLocalVariable("this", getClassEnhancer().getClassDescriptor(), null, startLabel, endLabel, 0);
visitor.visitLocalVariable(argNames[0], EnhanceUtils.CD_Object, null, startLabel, endLabel, 1);
visitor.visitLocalVariable("o", "L" + ACN_objectIdClass + ";", null, l0, endLabel, 2);
if (reflectionFieldStart != null) {
visitor.visitLocalVariable("field", "Ljava/lang/reflect/Field;", null, reflectionFieldStart, l2, 3);
visitor.visitMaxs(3, 4);
} else {
visitor.visitMaxs(3, 3);
}
}
}
} else {
// datastore/nondurable identity
Label startLabel = new Label();
visitor.visitLabel(startLabel);
visitor.visitInsn(Opcodes.RETURN);
Label l1 = new Label();
visitor.visitLabel(l1);
visitor.visitLocalVariable("this", getClassEnhancer().getClassDescriptor(), null, startLabel, l1, 0);
visitor.visitLocalVariable(argNames[0], EnhanceUtils.CD_Object, null, startLabel, l1, 1);
visitor.visitMaxs(0, 2);
}
visitor.visitEnd();
}
use of org.datanucleus.metadata.PropertyMetaData in project datanucleus-core by datanucleus.
the class CopyKeyFieldsToObjectId2 method execute.
/**
* Method to add the contents of the class method.
*/
public void execute() {
visitor.visitCode();
ClassMetaData cmd = enhancer.getClassMetaData();
if (cmd.getIdentityType() == IdentityType.APPLICATION) {
// application identity
if (!cmd.isInstantiable()) {
// Application identity but mapped-superclass with no PK defined, so just "return"
Label startLabel = new Label();
visitor.visitLabel(startLabel);
visitor.visitInsn(Opcodes.RETURN);
Label endLabel = new Label();
visitor.visitLabel(endLabel);
visitor.visitLocalVariable("this", getClassEnhancer().getClassDescriptor(), null, startLabel, endLabel, 0);
visitor.visitLocalVariable(argNames[0], getNamer().getObjectIdFieldSupplierDescriptor(), null, startLabel, endLabel, 1);
visitor.visitLocalVariable(argNames[1], EnhanceUtils.CD_Object, null, startLabel, endLabel, 2);
visitor.visitMaxs(0, 3);
} else {
String objectIdClass = cmd.getObjectidClass();
String ACN_objectIdClass = objectIdClass.replace('.', '/');
if (IdentityUtils.isSingleFieldIdentityClass(objectIdClass)) {
// SingleFieldIdentity
Label startLabel = new Label();
visitor.visitLabel(startLabel);
visitor.visitTypeInsn(Opcodes.NEW, getNamer().getFatalInternalExceptionAsmClassName());
visitor.visitInsn(Opcodes.DUP);
visitor.visitLdcInsn("It's illegal to call dnCopyKeyFieldsToObjectId for a class with single-field identity.");
visitor.visitMethodInsn(Opcodes.INVOKESPECIAL, getNamer().getFatalInternalExceptionAsmClassName(), "<init>", "(Ljava/lang/String;)V");
visitor.visitInsn(Opcodes.ATHROW);
Label endLabel = new Label();
visitor.visitLabel(endLabel);
visitor.visitLocalVariable("this", getClassEnhancer().getClassDescriptor(), null, startLabel, endLabel, 0);
visitor.visitLocalVariable(argNames[0], EnhanceUtils.CD_Object, null, startLabel, endLabel, 1);
visitor.visitLocalVariable("paramObject", "Ljava/lang/Object;", null, startLabel, endLabel, 2);
visitor.visitMaxs(3, 3);
} else {
// User-provided app identity, and compound identity
Label l0 = new Label();
Label l1 = new Label();
Label l2 = new Label();
visitor.visitTryCatchBlock(l0, l1, l2, "java/lang/Exception");
Label startLabel = new Label();
visitor.visitLabel(startLabel);
visitor.visitVarInsn(Opcodes.ALOAD, 1);
Label l4 = new Label();
visitor.visitJumpInsn(Opcodes.IFNONNULL, l4);
visitor.visitTypeInsn(Opcodes.NEW, "java/lang/IllegalArgumentException");
visitor.visitInsn(Opcodes.DUP);
visitor.visitLdcInsn("ObjectIdFieldSupplier is null");
visitor.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/IllegalArgumentException", "<init>", "(Ljava/lang/String;)V");
visitor.visitInsn(Opcodes.ATHROW);
visitor.visitLabel(l4);
visitor.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
visitor.visitVarInsn(Opcodes.ALOAD, 2);
visitor.visitTypeInsn(Opcodes.INSTANCEOF, ACN_objectIdClass);
Label l5 = new Label();
visitor.visitJumpInsn(Opcodes.IFNE, l5);
visitor.visitTypeInsn(Opcodes.NEW, "java/lang/ClassCastException");
visitor.visitInsn(Opcodes.DUP);
visitor.visitLdcInsn("oid is not instanceof " + objectIdClass);
visitor.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/ClassCastException", "<init>", "(Ljava/lang/String;)V");
visitor.visitInsn(Opcodes.ATHROW);
visitor.visitLabel(l5);
visitor.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
visitor.visitVarInsn(Opcodes.ALOAD, 2);
visitor.visitTypeInsn(Opcodes.CHECKCAST, ACN_objectIdClass);
visitor.visitVarInsn(Opcodes.ASTORE, 3);
visitor.visitLabel(l0);
// Copy the PK members using the appropriate method for each field/property
int[] pkFieldNums = enhancer.getClassMetaData().getPKMemberPositions();
Label reflectionFieldStart = null;
for (int i = 0; i < pkFieldNums.length; i++) {
AbstractMemberMetaData fmd = enhancer.getClassMetaData().getMetaDataForManagedMemberAtAbsolutePosition(pkFieldNums[i]);
String fieldTypeDesc = Type.getDescriptor(fmd.getType());
String typeMethodName = EnhanceUtils.getTypeNameForPersistableMethod(fmd.getType());
int pkFieldModifiers = ClassUtils.getModifiersForFieldOfClass(enhancer.getClassLoaderResolver(), objectIdClass, fmd.getName());
// Check if the PK field type is a PC (CompoundIdentity)
AbstractClassMetaData acmd = enhancer.getMetaDataManager().getMetaDataForClass(fmd.getType(), enhancer.getClassLoaderResolver());
if (acmd != null && acmd.getIdentityType() != IdentityType.NONDURABLE) {
// CompoundIdentity, this field of the PK is a PC
visitor.visitVarInsn(Opcodes.ALOAD, 3);
visitor.visitVarInsn(Opcodes.ALOAD, 1);
EnhanceUtils.addBIPUSHToMethod(visitor, fmd.getFieldId());
visitor.visitMethodInsn(Opcodes.INVOKEINTERFACE, getNamer().getObjectIdFieldSupplierAsmClassName(), "fetch" + typeMethodName + "Field", "(I)" + EnhanceUtils.getTypeDescriptorForEnhanceMethod(fmd.getType()));
if (typeMethodName.equals("Object")) {
visitor.visitTypeInsn(Opcodes.CHECKCAST, fmd.getTypeName().replace('.', '/'));
}
// Note that we swap JDOHelper.getObjectId(obj) for ((Persistable)obj).dnGetObjectId())
visitor.visitMethodInsn(Opcodes.INVOKEINTERFACE, getNamer().getPersistableAsmClassName(), getNamer().getGetObjectIdMethodName(), "()Ljava/lang/Object;", true);
// visitor.visitMethodInsn(Opcodes.INVOKESTATIC, getNamer().getHelperAsmClassName(), "getObjectId", "(Ljava/lang/Object;)Ljava/lang/Object;");
visitor.visitTypeInsn(Opcodes.CHECKCAST, acmd.getObjectidClass().replace('.', '/'));
// TODO Cater for property, and private field cases
visitor.visitFieldInsn(Opcodes.PUTFIELD, ACN_objectIdClass, fmd.getName(), "L" + acmd.getObjectidClass().replace('.', '/') + ";");
} else {
// Standard application-identity
if (fmd instanceof PropertyMetaData) {
// Field in PK is property, hence use setXXX in PK
visitor.visitVarInsn(Opcodes.ALOAD, 3);
visitor.visitVarInsn(Opcodes.ALOAD, 1);
EnhanceUtils.addBIPUSHToMethod(visitor, fmd.getFieldId());
visitor.visitMethodInsn(Opcodes.INVOKEINTERFACE, getNamer().getObjectIdFieldSupplierAsmClassName(), "fetch" + typeMethodName + "Field", "(I)" + EnhanceUtils.getTypeDescriptorForEnhanceMethod(fmd.getType()));
if (typeMethodName.equals("Object")) {
visitor.visitTypeInsn(Opcodes.CHECKCAST, fmd.getTypeName().replace('.', '/'));
}
visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, ACN_objectIdClass, ClassUtils.getJavaBeanSetterName(fmd.getName()), "(" + fieldTypeDesc + ")V");
} else if (Modifier.isPublic(pkFieldModifiers)) {
// Field in PK is public so update directly
visitor.visitVarInsn(Opcodes.ALOAD, 3);
visitor.visitVarInsn(Opcodes.ALOAD, 1);
EnhanceUtils.addBIPUSHToMethod(visitor, fmd.getFieldId());
visitor.visitMethodInsn(Opcodes.INVOKEINTERFACE, getNamer().getObjectIdFieldSupplierAsmClassName(), "fetch" + typeMethodName + "Field", "(I)" + EnhanceUtils.getTypeDescriptorForEnhanceMethod(fmd.getType()));
if (typeMethodName.equals("Object")) {
visitor.visitTypeInsn(Opcodes.CHECKCAST, fmd.getTypeName().replace('.', '/'));
}
visitor.visitFieldInsn(Opcodes.PUTFIELD, ACN_objectIdClass, fmd.getName(), fieldTypeDesc);
} else {
// Field in PK is protected/private so use reflection, generating
// "Field field = o.getClass().getDeclaredField("pmIDFloat");"
// "field.setAccessible(true);"
// "field.set(o, fs.fetchObjectField(1));"
visitor.visitVarInsn(Opcodes.ALOAD, 3);
visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Object", "getClass", "()Ljava/lang/Class;");
visitor.visitLdcInsn(fmd.getName());
visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Class", "getDeclaredField", "(Ljava/lang/String;)Ljava/lang/reflect/Field;");
visitor.visitVarInsn(Opcodes.ASTORE, 4);
if (reflectionFieldStart == null) {
reflectionFieldStart = new Label();
visitor.visitLabel(reflectionFieldStart);
}
visitor.visitVarInsn(Opcodes.ALOAD, 4);
visitor.visitInsn(Opcodes.ICONST_1);
visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/reflect/Field", "setAccessible", "(Z)V");
visitor.visitVarInsn(Opcodes.ALOAD, 4);
visitor.visitVarInsn(Opcodes.ALOAD, 3);
visitor.visitVarInsn(Opcodes.ALOAD, 1);
EnhanceUtils.addBIPUSHToMethod(visitor, fmd.getFieldId());
if (fmd.getTypeName().equals("boolean")) {
visitor.visitMethodInsn(Opcodes.INVOKEINTERFACE, getNamer().getObjectIdFieldSupplierAsmClassName(), "fetchBooleanField", "(I)Z");
visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/reflect/Field", "setBoolean", "(Ljava/lang/Object;Z)V");
} else if (fmd.getTypeName().equals("byte")) {
visitor.visitMethodInsn(Opcodes.INVOKEINTERFACE, getNamer().getObjectIdFieldSupplierAsmClassName(), "fetchByteField", "(I)B");
visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/reflect/Field", "setByte", "(Ljava/lang/Object;B)V");
} else if (fmd.getTypeName().equals("char")) {
visitor.visitMethodInsn(Opcodes.INVOKEINTERFACE, getNamer().getObjectIdFieldSupplierAsmClassName(), "fetchCharField", "(I)C");
visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/reflect/Field", "setChar", "(Ljava/lang/Object;C)V");
} else if (fmd.getTypeName().equals("double")) {
visitor.visitMethodInsn(Opcodes.INVOKEINTERFACE, getNamer().getObjectIdFieldSupplierAsmClassName(), "fetchDoubleField", "(I)D");
visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/reflect/Field", "setDouble", "(Ljava/lang/Object;D)V");
} else if (fmd.getTypeName().equals("float")) {
visitor.visitMethodInsn(Opcodes.INVOKEINTERFACE, getNamer().getObjectIdFieldSupplierAsmClassName(), "fetchFloatField", "(I)F");
visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/reflect/Field", "setFloat", "(Ljava/lang/Object;F)V");
} else if (fmd.getTypeName().equals("int")) {
visitor.visitMethodInsn(Opcodes.INVOKEINTERFACE, getNamer().getObjectIdFieldSupplierAsmClassName(), "fetchIntField", "(I)I");
visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/reflect/Field", "setInt", "(Ljava/lang/Object;I)V");
} else if (fmd.getTypeName().equals("long")) {
visitor.visitMethodInsn(Opcodes.INVOKEINTERFACE, getNamer().getObjectIdFieldSupplierAsmClassName(), "fetchLongField", "(I)J");
visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/reflect/Field", "setLong", "(Ljava/lang/Object;J)V");
} else if (fmd.getTypeName().equals("short")) {
visitor.visitMethodInsn(Opcodes.INVOKEINTERFACE, getNamer().getObjectIdFieldSupplierAsmClassName(), "fetchShortField", "(I)S");
visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/reflect/Field", "setShort", "(Ljava/lang/Object;S)V");
} else {
visitor.visitMethodInsn(Opcodes.INVOKEINTERFACE, getNamer().getObjectIdFieldSupplierAsmClassName(), "fetchObjectField", "(I)Ljava/lang/Object;");
visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/reflect/Field", "set", "(Ljava/lang/Object;Ljava/lang/Object;)V");
}
}
}
}
visitor.visitLabel(l1);
Label l20 = new Label();
visitor.visitJumpInsn(Opcodes.GOTO, l20);
visitor.visitLabel(l2);
visitor.visitFrame(Opcodes.F_FULL, 4, new Object[] { getClassEnhancer().getASMClassName(), getClassEnhancer().getNamer().getObjectIdFieldSupplierAsmClassName(), "java/lang/Object", ACN_objectIdClass }, 1, new Object[] { "java/lang/Exception" });
visitor.visitVarInsn(Opcodes.ASTORE, 4);
visitor.visitLabel(l20);
visitor.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
visitor.visitInsn(Opcodes.RETURN);
Label endLabel = new Label();
visitor.visitLabel(endLabel);
visitor.visitLocalVariable("this", getClassEnhancer().getClassDescriptor(), null, startLabel, endLabel, 0);
visitor.visitLocalVariable(argNames[0], getNamer().getObjectIdFieldSupplierDescriptor(), null, startLabel, endLabel, 1);
visitor.visitLocalVariable(argNames[1], EnhanceUtils.CD_Object, null, startLabel, endLabel, 2);
visitor.visitLocalVariable("o", "L" + ACN_objectIdClass + ";", null, l0, endLabel, 3);
if (reflectionFieldStart != null) {
visitor.visitLocalVariable("field", "Ljava/lang/reflect/Field;", null, reflectionFieldStart, l2, 4);
visitor.visitMaxs(4, 5);
} else {
visitor.visitMaxs(3, 4);
}
}
}
} else {
// datastore/nondurable identity
Label startLabel = new Label();
visitor.visitLabel(startLabel);
visitor.visitInsn(Opcodes.RETURN);
Label endLabel = new Label();
visitor.visitLabel(endLabel);
visitor.visitLocalVariable("this", getClassEnhancer().getClassDescriptor(), null, startLabel, endLabel, 0);
visitor.visitLocalVariable(argNames[0], "L" + getNamer().getObjectIdFieldSupplierAsmClassName() + ";", null, startLabel, endLabel, 1);
visitor.visitLocalVariable(argNames[1], EnhanceUtils.CD_Object, null, startLabel, endLabel, 2);
visitor.visitMaxs(0, 3);
}
visitor.visitEnd();
}
use of org.datanucleus.metadata.PropertyMetaData in project datanucleus-core by datanucleus.
the class ReplaceField method execute.
/**
* Method to add the contents of the class method.
*/
public void execute() {
AbstractMemberMetaData[] fields = enhancer.getClassMetaData().getManagedMembers();
String pcSuperclassName = enhancer.getClassMetaData().getPersistableSuperclass();
visitor.visitCode();
Label startLabel = new Label();
visitor.visitLabel(startLabel);
if (pcSuperclassName != null) {
if (fields.length > 0) {
visitor.visitVarInsn(Opcodes.ALOAD, 0);
visitor.visitFieldInsn(Opcodes.GETFIELD, getClassEnhancer().getASMClassName(), getNamer().getStateManagerFieldName(), getNamer().getStateManagerDescriptor());
Label l1 = new Label();
visitor.visitJumpInsn(Opcodes.IFNONNULL, l1);
visitor.visitTypeInsn(Opcodes.NEW, "java/lang/IllegalStateException");
visitor.visitInsn(Opcodes.DUP);
visitor.visitLdcInsn("state manager is null");
visitor.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/IllegalStateException", "<init>", "(Ljava/lang/String;)V");
visitor.visitInsn(Opcodes.ATHROW);
visitor.visitLabel(l1);
visitor.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
visitor.visitVarInsn(Opcodes.ILOAD, 1);
visitor.visitFieldInsn(Opcodes.GETSTATIC, getClassEnhancer().getASMClassName(), getNamer().getInheritedFieldCountFieldName(), "I");
visitor.visitInsn(Opcodes.ISUB);
Label[] fieldOptions = new Label[fields.length];
for (int i = 0; i < fields.length; i++) {
fieldOptions[i] = new Label();
}
Label defaultLabel = new Label();
Label endSwitchLabel = new Label();
// switch:
visitor.visitTableSwitchInsn(0, fields.length - 1, defaultLabel, fieldOptions);
for (int i = 0; i < fields.length; i++) {
visitor.visitLabel(fieldOptions[i]);
visitor.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
visitor.visitVarInsn(Opcodes.ALOAD, 0);
visitor.visitVarInsn(Opcodes.ALOAD, 0);
visitor.visitFieldInsn(Opcodes.GETFIELD, getClassEnhancer().getASMClassName(), getNamer().getStateManagerFieldName(), getNamer().getStateManagerDescriptor());
visitor.visitVarInsn(Opcodes.ALOAD, 0);
visitor.visitVarInsn(Opcodes.ILOAD, 1);
String methodNameType = EnhanceUtils.getTypeNameForPersistableMethod(fields[i].getType());
visitor.visitMethodInsn(Opcodes.INVOKEINTERFACE, getNamer().getStateManagerAsmClassName(), "replacing" + methodNameType + "Field", "(" + getNamer().getPersistableDescriptor() + "I)" + EnhanceUtils.getTypeDescriptorForEnhanceMethod(fields[i].getType()));
if (methodNameType.equals("Object")) {
// Check any Object types for casting
visitor.visitTypeInsn(Opcodes.CHECKCAST, fields[i].getTypeName().replace('.', '/'));
}
if (fields[i] instanceof PropertyMetaData) {
// Persistent properties so use dnSetXXX(...)
visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, getClassEnhancer().getASMClassName(), getNamer().getSetMethodPrefixMethodName() + fields[i].getName(), "(" + Type.getDescriptor(fields[i].getType()) + ")V");
} else {
// Persistent field so use xxx = ...
visitor.visitFieldInsn(Opcodes.PUTFIELD, getClassEnhancer().getASMClassName(), fields[i].getName(), Type.getDescriptor(fields[i].getType()));
}
visitor.visitJumpInsn(Opcodes.GOTO, endSwitchLabel);
}
// default:
visitor.visitLabel(defaultLabel);
visitor.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
visitor.visitVarInsn(Opcodes.ALOAD, 0);
visitor.visitVarInsn(Opcodes.ILOAD, 1);
visitor.visitMethodInsn(Opcodes.INVOKESPECIAL, pcSuperclassName.replace('.', '/'), getNamer().getReplaceFieldMethodName(), "(I)V");
// End of switch
visitor.visitLabel(endSwitchLabel);
visitor.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
visitor.visitInsn(Opcodes.RETURN);
Label endLabel = new Label();
visitor.visitLabel(endLabel);
visitor.visitLocalVariable("this", getClassEnhancer().getClassDescriptor(), null, startLabel, endLabel, 0);
visitor.visitLocalVariable(argNames[0], "I", null, startLabel, endLabel, 1);
visitor.visitMaxs(4, 2);
} else {
visitor.visitVarInsn(Opcodes.ALOAD, 0);
visitor.visitVarInsn(Opcodes.ILOAD, 1);
visitor.visitMethodInsn(Opcodes.INVOKESPECIAL, pcSuperclassName.replace('.', '/'), getNamer().getReplaceFieldMethodName(), "(I)V");
visitor.visitInsn(Opcodes.RETURN);
Label endLabel = new Label();
visitor.visitLabel(endLabel);
visitor.visitLocalVariable("this", getClassEnhancer().getClassDescriptor(), null, startLabel, endLabel, 0);
visitor.visitLocalVariable(argNames[0], "I", null, startLabel, endLabel, 1);
visitor.visitMaxs(2, 2);
}
} else {
if (fields.length > 0) {
visitor.visitVarInsn(Opcodes.ALOAD, 0);
visitor.visitFieldInsn(Opcodes.GETFIELD, getClassEnhancer().getASMClassName(), getNamer().getStateManagerFieldName(), getNamer().getStateManagerDescriptor());
Label l1 = new Label();
visitor.visitJumpInsn(Opcodes.IFNONNULL, l1);
visitor.visitTypeInsn(Opcodes.NEW, "java/lang/IllegalStateException");
visitor.visitInsn(Opcodes.DUP);
visitor.visitLdcInsn("state manager is null");
visitor.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/IllegalStateException", "<init>", "(Ljava/lang/String;)V");
visitor.visitInsn(Opcodes.ATHROW);
visitor.visitLabel(l1);
visitor.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
visitor.visitVarInsn(Opcodes.ILOAD, 1);
Label[] fieldOptions = new Label[fields.length];
for (int i = 0; i < fields.length; i++) {
fieldOptions[i] = new Label();
}
Label defaultLabel = new Label();
Label endSwitchLabel = new Label();
// switch:
visitor.visitTableSwitchInsn(0, fields.length - 1, defaultLabel, fieldOptions);
for (int i = 0; i < fields.length; i++) {
visitor.visitLabel(fieldOptions[i]);
visitor.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
visitor.visitVarInsn(Opcodes.ALOAD, 0);
visitor.visitVarInsn(Opcodes.ALOAD, 0);
visitor.visitFieldInsn(Opcodes.GETFIELD, getClassEnhancer().getASMClassName(), getNamer().getStateManagerFieldName(), getNamer().getStateManagerDescriptor());
visitor.visitVarInsn(Opcodes.ALOAD, 0);
visitor.visitVarInsn(Opcodes.ILOAD, 1);
String methodNameType = EnhanceUtils.getTypeNameForPersistableMethod(fields[i].getType());
visitor.visitMethodInsn(Opcodes.INVOKEINTERFACE, getNamer().getStateManagerAsmClassName(), "replacing" + methodNameType + "Field", "(" + getNamer().getPersistableDescriptor() + "I)" + EnhanceUtils.getTypeDescriptorForEnhanceMethod(fields[i].getType()));
if (methodNameType.equals("Object")) {
// Check any Object types for casting
visitor.visitTypeInsn(Opcodes.CHECKCAST, fields[i].getTypeName().replace('.', '/'));
}
if (fields[i] instanceof PropertyMetaData) {
// Persistent properties so use dnSetXXX(...)
visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, getClassEnhancer().getASMClassName(), getNamer().getSetMethodPrefixMethodName() + fields[i].getName(), "(" + Type.getDescriptor(fields[i].getType()) + ")V");
} else {
// Persistent field so use xxx = ...
visitor.visitFieldInsn(Opcodes.PUTFIELD, getClassEnhancer().getASMClassName(), fields[i].getName(), Type.getDescriptor(fields[i].getType()));
}
visitor.visitJumpInsn(Opcodes.GOTO, endSwitchLabel);
}
// default:
visitor.visitLabel(defaultLabel);
visitor.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
visitor.visitTypeInsn(Opcodes.NEW, "java/lang/IllegalArgumentException");
visitor.visitInsn(Opcodes.DUP);
visitor.visitTypeInsn(Opcodes.NEW, "java/lang/StringBuffer");
visitor.visitInsn(Opcodes.DUP);
visitor.visitLdcInsn("out of field index :");
visitor.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/StringBuffer", "<init>", "(Ljava/lang/String;)V");
visitor.visitVarInsn(Opcodes.ILOAD, 1);
visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/StringBuffer", "append", "(I)Ljava/lang/StringBuffer;");
visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/StringBuffer", "toString", "()Ljava/lang/String;");
visitor.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/IllegalArgumentException", "<init>", "(Ljava/lang/String;)V");
visitor.visitInsn(Opcodes.ATHROW);
// End of switch
visitor.visitLabel(endSwitchLabel);
visitor.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
visitor.visitInsn(Opcodes.RETURN);
Label endLabel = new Label();
visitor.visitLabel(endLabel);
visitor.visitLocalVariable("this", getClassEnhancer().getClassDescriptor(), null, startLabel, endLabel, 0);
visitor.visitLocalVariable(argNames[0], "I", null, startLabel, endLabel, 1);
visitor.visitMaxs(5, 2);
} else {
visitor.visitInsn(Opcodes.RETURN);
Label endLabel = new Label();
visitor.visitLabel(endLabel);
visitor.visitLocalVariable("this", getClassEnhancer().getClassDescriptor(), null, startLabel, endLabel, 0);
visitor.visitLocalVariable(argNames[0], "I", null, startLabel, endLabel, 1);
visitor.visitMaxs(0, 2);
}
}
visitor.visitEnd();
}
use of org.datanucleus.metadata.PropertyMetaData in project datanucleus-core by datanucleus.
the class CopyKeyFieldsFromObjectId method execute.
/**
* Method to add the contents of the class method.
*/
public void execute() {
visitor.visitCode();
ClassMetaData cmd = enhancer.getClassMetaData();
if (cmd.getIdentityType() == IdentityType.APPLICATION) {
// application identity
if (!cmd.isInstantiable()) {
// Application identity but mapped-superclass with no PK defined, so just "return"
Label startLabel = new Label();
visitor.visitLabel(startLabel);
visitor.visitInsn(Opcodes.RETURN);
Label endLabel = new Label();
visitor.visitLabel(endLabel);
visitor.visitLocalVariable("this", getClassEnhancer().getClassDescriptor(), null, startLabel, endLabel, 0);
visitor.visitLocalVariable(argNames[0], getNamer().getObjectIdFieldConsumerDescriptor(), null, startLabel, endLabel, 1);
visitor.visitLocalVariable(argNames[1], EnhanceUtils.CD_Object, null, startLabel, endLabel, 2);
visitor.visitMaxs(0, 3);
} else {
int[] pkFieldNums = cmd.getPKMemberPositions();
String objectIdClass = cmd.getObjectidClass();
String ACN_objectIdClass = objectIdClass.replace('.', '/');
if (IdentityUtils.isSingleFieldIdentityClass(objectIdClass)) {
// SingleFieldIdentity
Label startLabel = new Label();
visitor.visitLabel(startLabel);
// if (fc == null) throw new IllegalArgumentException("...");
visitor.visitVarInsn(Opcodes.ALOAD, 1);
Label l1 = new Label();
visitor.visitJumpInsn(Opcodes.IFNONNULL, l1);
visitor.visitTypeInsn(Opcodes.NEW, "java/lang/IllegalArgumentException");
visitor.visitInsn(Opcodes.DUP);
visitor.visitLdcInsn("ObjectIdFieldConsumer is null");
visitor.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/IllegalArgumentException", "<init>", "(Ljava/lang/String;)V");
visitor.visitInsn(Opcodes.ATHROW);
// if (!(oid instanceof LongIdentity)) throw new ClassCastException("...");
visitor.visitLabel(l1);
visitor.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
visitor.visitVarInsn(Opcodes.ALOAD, 2);
visitor.visitTypeInsn(Opcodes.INSTANCEOF, ACN_objectIdClass);
Label l5 = new Label();
visitor.visitJumpInsn(Opcodes.IFNE, l5);
visitor.visitTypeInsn(Opcodes.NEW, "java/lang/ClassCastException");
visitor.visitInsn(Opcodes.DUP);
visitor.visitLdcInsn("oid is not instanceof " + objectIdClass);
visitor.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/ClassCastException", "<init>", "(Ljava/lang/String;)V");
visitor.visitInsn(Opcodes.ATHROW);
// XXXIdentity o = (XXXIdentity) oid;
visitor.visitLabel(l5);
visitor.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
visitor.visitVarInsn(Opcodes.ALOAD, 2);
visitor.visitTypeInsn(Opcodes.CHECKCAST, ACN_objectIdClass);
visitor.visitVarInsn(Opcodes.ASTORE, 3);
// fc.storeXXXField(1, o.getKey());
Label l9 = new Label();
visitor.visitLabel(l9);
visitor.visitVarInsn(Opcodes.ALOAD, 1);
EnhanceUtils.addBIPUSHToMethod(visitor, pkFieldNums[0]);
AbstractMemberMetaData fmd = cmd.getMetaDataForManagedMemberAtAbsolutePosition(pkFieldNums[0]);
Class primitiveType = ClassUtils.getPrimitiveTypeForType(fmd.getType());
if (primitiveType != null) {
// The PK field is a primitive wrapper so create wrapper from getKey()
String ACN_fieldType = fmd.getTypeName().replace('.', '/');
String getKeyReturnDesc = Type.getDescriptor(primitiveType);
visitor.visitVarInsn(Opcodes.ALOAD, 3);
visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, ACN_objectIdClass, "getKey", "()" + getKeyReturnDesc);
visitor.visitMethodInsn(Opcodes.INVOKESTATIC, ACN_fieldType, "valueOf", "(" + getKeyReturnDesc + ")L" + ACN_fieldType + ";");
visitor.visitMethodInsn(Opcodes.INVOKEINTERFACE, getNamer().getObjectIdFieldConsumerAsmClassName(), "storeObjectField", "(I" + EnhanceUtils.CD_Object + ")V");
} else {
// PK field isn't a primitive wrapper
visitor.visitVarInsn(Opcodes.ALOAD, 3);
visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, ACN_objectIdClass, "getKey", "()" + getNamer().getTypeDescriptorForSingleFieldIdentityGetKey(objectIdClass));
visitor.visitMethodInsn(Opcodes.INVOKEINTERFACE, getNamer().getObjectIdFieldConsumerAsmClassName(), "store" + getNamer().getTypeNameForUseWithSingleFieldIdentity(objectIdClass) + "Field", "(I" + getNamer().getTypeDescriptorForSingleFieldIdentityGetKey(objectIdClass) + ")V");
}
visitor.visitInsn(Opcodes.RETURN);
Label endLabel = new Label();
visitor.visitLabel(endLabel);
visitor.visitLocalVariable("this", getClassEnhancer().getClassDescriptor(), null, startLabel, endLabel, 0);
visitor.visitLocalVariable(argNames[0], getNamer().getObjectIdFieldConsumerDescriptor(), null, startLabel, endLabel, 1);
visitor.visitLocalVariable(argNames[1], EnhanceUtils.CD_Object, null, startLabel, endLabel, 2);
visitor.visitLocalVariable("o", getNamer().getSingleFieldIdentityDescriptor(objectIdClass), null, l9, endLabel, 3);
visitor.visitMaxs(3, 4);
} else {
// User-provided app identity, and compound identity
// Put try-catch around the field setting (for reflection cases)
Label l0 = new Label();
Label l1 = new Label();
Label l2 = new Label();
visitor.visitTryCatchBlock(l0, l1, l2, "java/lang/Exception");
Label startLabel = new Label();
visitor.visitLabel(startLabel);
visitor.visitVarInsn(Opcodes.ALOAD, 1);
Label l4 = new Label();
visitor.visitJumpInsn(Opcodes.IFNONNULL, l4);
visitor.visitTypeInsn(Opcodes.NEW, "java/lang/IllegalArgumentException");
visitor.visitInsn(Opcodes.DUP);
visitor.visitLdcInsn("ObjectIdFieldConsumer is null");
visitor.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/IllegalArgumentException", "<init>", "(Ljava/lang/String;)V");
visitor.visitInsn(Opcodes.ATHROW);
visitor.visitLabel(l4);
visitor.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
visitor.visitVarInsn(Opcodes.ALOAD, 2);
visitor.visitTypeInsn(Opcodes.INSTANCEOF, ACN_objectIdClass);
Label l5 = new Label();
visitor.visitJumpInsn(Opcodes.IFNE, l5);
visitor.visitTypeInsn(Opcodes.NEW, "java/lang/ClassCastException");
visitor.visitInsn(Opcodes.DUP);
visitor.visitLdcInsn("oid is not instanceof " + objectIdClass);
visitor.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/ClassCastException", "<init>", "(Ljava/lang/String;)V");
visitor.visitInsn(Opcodes.ATHROW);
visitor.visitLabel(l5);
visitor.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
visitor.visitVarInsn(Opcodes.ALOAD, 2);
visitor.visitTypeInsn(Opcodes.CHECKCAST, ACN_objectIdClass);
visitor.visitVarInsn(Opcodes.ASTORE, 3);
visitor.visitLabel(l0);
// Copy the PK members using the appropriate method for each field/property
Label reflectionFieldStart = null;
for (int i = 0; i < pkFieldNums.length; i++) {
AbstractMemberMetaData fmd = enhancer.getClassMetaData().getMetaDataForManagedMemberAtAbsolutePosition(pkFieldNums[i]);
String fieldTypeDesc = Type.getDescriptor(fmd.getType());
String typeMethodName = EnhanceUtils.getTypeNameForPersistableMethod(fmd.getType());
int pkFieldModifiers = ClassUtils.getModifiersForFieldOfClass(enhancer.getClassLoaderResolver(), objectIdClass, fmd.getName());
// Check if the PK field type is a PC (CompoundIdentity)
AbstractClassMetaData acmd = enhancer.getMetaDataManager().getMetaDataForClass(fmd.getType(), enhancer.getClassLoaderResolver());
if (acmd != null && acmd.getIdentityType() != IdentityType.NONDURABLE) {
// CompoundIdentity, this field of the PK is a PC
visitor.visitVarInsn(Opcodes.ALOAD, 1);
EnhanceUtils.addBIPUSHToMethod(visitor, fmd.getFieldId());
visitor.visitVarInsn(Opcodes.ALOAD, 0);
visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, getClassEnhancer().getASMClassName(), getNamer().getGetExecutionContextMethodName(), "()L" + getNamer().getExecutionContextAsmClassName() + ";");
visitor.visitVarInsn(Opcodes.ALOAD, 3);
// TODO Cater for property, or private field cases
visitor.visitFieldInsn(Opcodes.GETFIELD, ACN_objectIdClass, fmd.getName(), "L" + acmd.getObjectidClass().replace('.', '/') + ";");
visitor.visitInsn(Opcodes.ICONST_0);
visitor.visitMethodInsn(Opcodes.INVOKEINTERFACE, getNamer().getExecutionContextAsmClassName(), "findObject", "(Ljava/lang/Object;Z)Ljava/lang/Object;");
visitor.visitMethodInsn(Opcodes.INVOKEINTERFACE, getNamer().getObjectIdFieldConsumerAsmClassName(), "storeObjectField", "(ILjava/lang/Object;)V");
} else {
// Standard application-identity
if (fmd instanceof PropertyMetaData) {
// Field in PK is property, hence use getXXX in PK
visitor.visitVarInsn(Opcodes.ALOAD, 1);
EnhanceUtils.addBIPUSHToMethod(visitor, fmd.getFieldId());
visitor.visitVarInsn(Opcodes.ALOAD, 3);
visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, ACN_objectIdClass, ClassUtils.getJavaBeanGetterName(fmd.getName(), fmd.getTypeName().equals("boolean")), "()" + Type.getDescriptor(fmd.getType()));
visitor.visitMethodInsn(Opcodes.INVOKEINTERFACE, getNamer().getObjectIdFieldConsumerAsmClassName(), "store" + typeMethodName + "Field", "(I" + EnhanceUtils.getTypeDescriptorForEnhanceMethod(fmd.getType()) + ")V");
} else if (Modifier.isPublic(pkFieldModifiers)) {
// Field in PK is public so access directly
visitor.visitVarInsn(Opcodes.ALOAD, 1);
EnhanceUtils.addBIPUSHToMethod(visitor, fmd.getFieldId());
visitor.visitVarInsn(Opcodes.ALOAD, 3);
visitor.visitFieldInsn(Opcodes.GETFIELD, ACN_objectIdClass, fmd.getName(), fieldTypeDesc);
visitor.visitMethodInsn(Opcodes.INVOKEINTERFACE, getNamer().getObjectIdFieldConsumerAsmClassName(), "store" + typeMethodName + "Field", "(I" + EnhanceUtils.getTypeDescriptorForEnhanceMethod(fmd.getType()) + ")V");
} else {
// Field in PK is protected/private so use reflection, generating
// "Field field = o.getClass().getDeclaredField("pmIDFloat");"
// "field.setAccessible(true);"
// "fc.storeObjectField(1, field.get(o));"
visitor.visitVarInsn(Opcodes.ALOAD, 3);
visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Object", "getClass", "()Ljava/lang/Class;");
visitor.visitLdcInsn(fmd.getName());
visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Class", "getDeclaredField", "(Ljava/lang/String;)Ljava/lang/reflect/Field;");
visitor.visitVarInsn(Opcodes.ASTORE, 4);
if (reflectionFieldStart == null) {
reflectionFieldStart = new Label();
visitor.visitLabel(reflectionFieldStart);
}
visitor.visitVarInsn(Opcodes.ALOAD, 4);
visitor.visitInsn(Opcodes.ICONST_1);
visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/reflect/Field", "setAccessible", "(Z)V");
visitor.visitVarInsn(Opcodes.ALOAD, 1);
EnhanceUtils.addBIPUSHToMethod(visitor, fmd.getFieldId());
visitor.visitVarInsn(Opcodes.ALOAD, 4);
visitor.visitVarInsn(Opcodes.ALOAD, 3);
if (fmd.getTypeName().equals("boolean")) {
visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/reflect/Field", "getBoolean", "(Ljava/lang/Object;)Z");
} else if (fmd.getTypeName().equals("byte")) {
visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/reflect/Field", "getByte", "(Ljava/lang/Object;)B");
} else if (fmd.getTypeName().equals("char")) {
visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/reflect/Field", "getChar", "(Ljava/lang/Object;)C");
} else if (fmd.getTypeName().equals("double")) {
visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/reflect/Field", "getDouble", "(Ljava/lang/Object;)D");
} else if (fmd.getTypeName().equals("float")) {
visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/reflect/Field", "getFloat", "(Ljava/lang/Object;)F");
} else if (fmd.getTypeName().equals("int")) {
visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/reflect/Field", "getInt", "(Ljava/lang/Object;)I");
} else if (fmd.getTypeName().equals("long")) {
visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/reflect/Field", "getLong", "(Ljava/lang/Object;)L");
} else if (fmd.getTypeName().equals("short")) {
visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/reflect/Field", "getShort", "(Ljava/lang/Object;)S");
} else if (fmd.getTypeName().equals("java.lang.String")) {
visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/reflect/Field", "get", "(Ljava/lang/Object;)Ljava/lang/Object;");
visitor.visitTypeInsn(Opcodes.CHECKCAST, "java/lang/String");
} else {
visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/reflect/Field", "get", "(Ljava/lang/Object;)Ljava/lang/Object;");
}
visitor.visitMethodInsn(Opcodes.INVOKEINTERFACE, getNamer().getObjectIdFieldConsumerAsmClassName(), "store" + typeMethodName + "Field", "(I" + EnhanceUtils.getTypeDescriptorForEnhanceMethod(fmd.getType()) + ")V");
}
}
}
// catch of the try-catch
visitor.visitLabel(l1);
Label l20 = new Label();
visitor.visitJumpInsn(Opcodes.GOTO, l20);
visitor.visitLabel(l2);
visitor.visitFrame(Opcodes.F_FULL, 4, new Object[] { getClassEnhancer().getASMClassName(), getClassEnhancer().getNamer().getObjectIdFieldConsumerAsmClassName(), "java/lang/Object", ACN_objectIdClass }, 1, new Object[] { "java/lang/Exception" });
visitor.visitVarInsn(Opcodes.ASTORE, 4);
visitor.visitLabel(l20);
visitor.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
visitor.visitInsn(Opcodes.RETURN);
Label endLabel = new Label();
visitor.visitLabel(endLabel);
visitor.visitLocalVariable("this", getClassEnhancer().getClassDescriptor(), null, startLabel, endLabel, 0);
visitor.visitLocalVariable(argNames[0], getNamer().getObjectIdFieldConsumerDescriptor(), null, startLabel, endLabel, 1);
visitor.visitLocalVariable(argNames[1], EnhanceUtils.CD_Object, null, startLabel, endLabel, 2);
visitor.visitLocalVariable("o", "L" + ACN_objectIdClass + ";", null, l0, endLabel, 3);
if (reflectionFieldStart != null) {
visitor.visitLocalVariable("field", "Ljava/lang/reflect/Field;", null, reflectionFieldStart, l2, 4);
visitor.visitMaxs(4, 5);
} else {
visitor.visitMaxs(4, 4);
}
}
}
} else {
// datastore/nondurable identity
Label startLabel = new Label();
visitor.visitLabel(startLabel);
visitor.visitInsn(Opcodes.RETURN);
Label endLabel = new Label();
visitor.visitLabel(endLabel);
visitor.visitLocalVariable("this", getClassEnhancer().getClassDescriptor(), null, startLabel, endLabel, 0);
visitor.visitLocalVariable(argNames[0], "L" + getNamer().getObjectIdFieldConsumerAsmClassName() + ";", null, startLabel, endLabel, 1);
visitor.visitLocalVariable(argNames[1], EnhanceUtils.CD_Object, null, startLabel, endLabel, 2);
visitor.visitMaxs(0, 3);
}
visitor.visitEnd();
}
use of org.datanucleus.metadata.PropertyMetaData in project datanucleus-api-jdo by datanucleus.
the class EmbeddedMetadataImpl method newPropertyMetadata.
/* (non-Javadoc)
* @see javax.jdo.metadata.EmbeddedMetadata#newPropertyMetadata(java.lang.String)
*/
public PropertyMetadata newPropertyMetadata(String name) {
PropertyMetaData internalPmd = getInternal().newPropertyMetaData(name);
PropertyMetadataImpl pmd = new PropertyMetadataImpl(internalPmd);
pmd.parent = this;
return pmd;
}
Aggregations