Package org.jpox.enhancer.asm.method

Source Code of org.jpox.enhancer.asm.method.JdoNewObjectIdInstance1

/**********************************************************************
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.jpox.enhancer.asm.method;

import org.jpox.enhancer.ClassEnhancer;
import org.jpox.enhancer.asm.ASMClassMethod;
import org.jpox.enhancer.asm.ASMUtils;
import org.jpox.metadata.AbstractMemberMetaData;
import org.jpox.metadata.ClassMetaData;
import org.jpox.metadata.IdentityType;
import org.jpox.util.ClassUtils;
import org.objectweb.asm.Label;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.Type;

/**
* Method to generate the method "jdoNewObjectIdInstance" using ASM.
* For datastore/nondurable identity this is
* <pre>
* public final Object jdoNewObjectIdInstance()
* {
*     return null;
* }
* </pre>
* and for SingleFieldIdentity
* <pre>
* public final Object jdoNewObjectIdInstance()
* {
*     return new YYYIdentity(getClass(), id);
* }
* </pre>
* and for user-supplied object identity class
* <pre>
* public final Object jdoNewObjectIdInstance()
* {
*     return new UserPrimaryKey();
* }
* </pre>
* @version $Revision: 1.13 $
*/
public class JdoNewObjectIdInstance1 extends ASMClassMethod
{
    public static JdoNewObjectIdInstance1 getInstance(ClassEnhancer enhancer)
    {
        return new JdoNewObjectIdInstance1(enhancer, ClassEnhancer.MN_JdoNewObjectIdInstance,
            Opcodes.ACC_PUBLIC | Opcodes.ACC_FINAL,
            Object.class, null, null);
    }

    /**
     * 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 JdoNewObjectIdInstance1(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();

        Label startLabel = new Label();
        visitor.visitLabel(startLabel);

        ClassMetaData cmd = enhancer.getClassMetaData();
        if (cmd.getIdentityType() == IdentityType.APPLICATION)
        {
            // application identity
            String objectIdClass = cmd.getObjectidClass();
            int[] pkFieldNums = cmd.getPKMemberPositions();
            if (cmd.getMetaDataManager().getApiAdapter().isSingleFieldIdentityClass(objectIdClass))
            {
                // SingleFieldIdentity
                String ACN_objectIdClass = objectIdClass.replace('.', '/');
                AbstractMemberMetaData fmd = cmd.getMetaDataForManagedMemberAtAbsolutePosition(pkFieldNums[0]);

                visitor.visitTypeInsn(Opcodes.NEW, ACN_objectIdClass);
                visitor.visitInsn(Opcodes.DUP);
                visitor.visitVarInsn(Opcodes.ALOAD, 0);
                visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Object", "getClass", "()Ljava/lang/Class;");
                visitor.visitVarInsn(Opcodes.ALOAD, 0);
                if (fmd.isProperty())
                {
                    // Persistent property so use jdoGetXXX()
                    visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, getClassEnhancer().getASMClassName(),
                        "jdoGet" + fmd.getName(), "()" + Type.getDescriptor(fmd.getType()));
                }
                else
                {
                    // Persistent field so use xxx
                    visitor.visitFieldInsn(Opcodes.GETFIELD, getClassEnhancer().getASMClassName(),
                        fmd.getName(), Type.getDescriptor(fmd.getType()));
                }
                Class primitiveType = ClassUtils.getPrimitiveTypeForType(fmd.getType());
                if (primitiveType != null)
                {
                    // Using object wrapper of primitive so use wrapper constructor
                    visitor.visitMethodInsn(Opcodes.INVOKESPECIAL, ACN_objectIdClass, "<init>",
                        "(Ljava/lang/Class;" + Type.getDescriptor(fmd.getType()) + ")V");
                }
                else
                {
                    visitor.visitMethodInsn(Opcodes.INVOKESPECIAL, ACN_objectIdClass, "<init>",
                        "(Ljava/lang/Class;" + ASMUtils.getTypeDescriptorForSingleFieldIdentityGetKey(objectIdClass) + ")V");
                }

                visitor.visitInsn(Opcodes.ARETURN);

                Label endLabel = new Label();
                visitor.visitLabel(endLabel);
                visitor.visitLocalVariable("this", getClassEnhancer().getClassDescriptor(), null, startLabel, endLabel, 0);
                visitor.visitMaxs(4, 1);
            }
            else
            {
                // User-provided app identity, and compound identity
                String ACN_objectIdClass = objectIdClass.replace('.', '/');

                visitor.visitTypeInsn(Opcodes.NEW, ACN_objectIdClass);
                visitor.visitInsn(Opcodes.DUP);
                visitor.visitMethodInsn(Opcodes.INVOKESPECIAL, ACN_objectIdClass, "<init>", "()V");
                visitor.visitInsn(Opcodes.ARETURN);

                Label endLabel = new Label();
                visitor.visitLabel(endLabel);
                visitor.visitLocalVariable("this", getClassEnhancer().getClassDescriptor(), null, startLabel, endLabel, 0);
                visitor.visitMaxs(2, 1);
            }
        }
        else
        {
            // datastore/nondurable identity
            visitor.visitInsn(Opcodes.ACONST_NULL);
            visitor.visitInsn(Opcodes.ARETURN);

            Label endLabel = new Label();
            visitor.visitLabel(endLabel);
            visitor.visitLocalVariable("this", getClassEnhancer().getClassDescriptor(), null, startLabel, endLabel, 0);
            visitor.visitMaxs(1, 1);
        }

        visitor.visitEnd();
    }
}
TOP

Related Classes of org.jpox.enhancer.asm.method.JdoNewObjectIdInstance1

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.