All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.datanucleus.enhancer.methods.CopyKeyFieldsToObjectId2 Maven / Gradle / Ivy

Go to download

DataNucleus Core provides the primary components of a heterogenous Java persistence solution. It supports persistence API's being layered on top of the core functionality.

There is a newer version: 6.0.7
Show newest version
/**********************************************************************
Copyright (c) 2007 Andy Jefferson and others. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Contributors:
    ...
**********************************************************************/
package org.datanucleus.enhancer.methods;

import java.lang.reflect.Modifier;

import org.datanucleus.asm.Label;
import org.datanucleus.asm.Opcodes;
import org.datanucleus.asm.Type;
import org.datanucleus.enhancer.ClassEnhancer;
import org.datanucleus.enhancer.ClassMethod;
import org.datanucleus.enhancer.EnhanceUtils;
import org.datanucleus.identity.IdentityUtils;
import org.datanucleus.metadata.AbstractClassMetaData;
import org.datanucleus.metadata.AbstractMemberMetaData;
import org.datanucleus.metadata.ClassMetaData;
import org.datanucleus.metadata.IdentityType;
import org.datanucleus.metadata.PropertyMetaData;
import org.datanucleus.util.ClassUtils;

/**
 * Method to generate the method "dnCopyKeyFieldsToObjectId" using ASM.
 * For datastore/nondurable identity
 * 
 * public void dnCopyKeyFieldsToObjectId(Persistable.ObjectIdFieldSupplier objectidfieldsupplier,
 *        Object object)
 * {
 * }
 * 
* and for SingleFieldIdentity *
 * public void dnCopyKeyFieldsToObjectId(Persistable.ObjectIdFieldSupplier fs, Object oid)
 * {
 *     throw new JDOFatalInternalException("It's illegal to call ...");
 * }
 * 
* and for user-supplied app identity *
 * public void dnCopyKeyFieldsToObjectId(Persistable.ObjectIdFieldSupplier fs, Object oid)
 * {
 *     if (fs == null)
 *         throw new IllegalArgumentException("ObjectIdFieldSupplier is null");
 *     if (oid instanceof UserPrimaryKey != true)
 *         throw new ClassCastException("oid is not instanceof mydomain.UserPrimaryKey");
 *     UserPrimaryKey o = (UserPrimaryKey) oid;
 *     o.zzz1 = fs.fetchYYY1Field(1);
 *     o.zzz2 = fs.fetchYYY2Field(2);
 * }
 * 
* and for CompoundIdentity *
 * public void dnCopyKeyFieldsToObjectId(Persistable.ObjectIdFieldSupplier fs, Object oid)
 * {
 *     if (fs == null)
 *         throw new IllegalArgumentException("ObjectIdFieldSupplier is null");
 *     if (oid instanceof UserPrimaryKey != true)
 *         throw new ClassCastException("oid is not instanceof mydomain.UserPrimaryKey");
 *     UserPrimaryKey o = (UserPrimaryKey) oid;
 *     o.zzz1 = fs.fetchYYYField(1);
 *     o.zzz2 = ((YYY2.Key)JDOHelper.getObjectId((YYY2)fs.fetchObjectField(2)));
 * }
 * 
*/ public class CopyKeyFieldsToObjectId2 extends ClassMethod { public static CopyKeyFieldsToObjectId2 getInstance(ClassEnhancer enhancer) { return new CopyKeyFieldsToObjectId2(enhancer, enhancer.getNamer().getCopyKeyFieldsToObjectIdMethodName(), Opcodes.ACC_PUBLIC/* | Opcodes.ACC_FINAL*/, null, new Class[] {enhancer.getNamer().getObjectIdFieldSupplierClass(), Object.class}, new String[] { "fs", "oid" }); } /** * Constructor. * @param enhancer ClassEnhancer * @param name Name of method * @param access Access type * @param returnType Return type * @param argTypes Argument types * @param argNames Argument names */ public CopyKeyFieldsToObjectId2(ClassEnhancer enhancer, String name, int access, Object returnType, Object[] argTypes, String[] argNames) { super(enhancer, name, access, returnType, argTypes, argNames); } /** * 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(), "", "(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", "", "(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", "", "(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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy