Package org.jpox.enhancer.asm.method

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

/**********************************************************************
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.AbstractClassMetaData;
import org.objectweb.asm.Label;
import org.objectweb.asm.Opcodes;

/**
* Method to generate the method "jdoMakeDirty" using ASM.
* <pre>
* public void jdoMakeDirty(String fieldName)
* {
*     if (jdoStateManager != null)
*         jdoStateManager.makeDirty(this, fieldName);
*     if (jdoIsDetached() && fieldName != null && fieldName.indexOf('.') > 0)
*     {
*         String className = fieldName.substring(0, fieldName.lastIndexOf('.'));
*         if (className.equals(this.getClass().getName()))
*         {
*             for (int i=0;i<jdoFieldNames.length;i++)
*             {
*                 if (jdoFieldNames[i].equals(fieldName))
*                 {
*                     if (((BitSet)jdoDetachedState[2]).get(i))
*                     {
*                         ((BitSet)jdoDetachedState[3]).set(i);
*                         return;
*                     }
*                     else
*                     {
*                         throw new JDODetachedFieldAccessException
*                             ("You have just attempted to access field \"brand\" yet this field ...");
*                     }
*                 }
*             }
*         }
*         super.jdoMakeDirtyDetached(fieldName);
*     }
* }
* </pre>
* and if not detachable
* <pre>public void jdoMakeDirty(String fieldName)
* {
*     if (jdoStateManager != null)
*         jdoStateManager.makeDirty(this, fieldName);
* }
* </pre>
* @version $Revision: 1.7 $
*/
public class JdoMakeDirty extends ASMClassMethod
{
    public static JdoMakeDirty getInstance(ClassEnhancer enhancer)
    {
        return new JdoMakeDirty(enhancer, ClassEnhancer.MN_JdoMakeDirty, Opcodes.ACC_PUBLIC,
            null, new Class[] {String.class}, new String[] {"fieldName"});
    }

    /**
     * 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 JdoMakeDirty(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()
    {
        AbstractClassMetaData cmd = getClassEnhancer().getClassMetaData();
        String pcSuperclassName = cmd.getPersistenceCapableSuperclass();

        visitor.visitCode();

        Label startLabel = new Label();
        visitor.visitLabel(startLabel);
        visitor.visitVarInsn(Opcodes.ALOAD, 0);
        visitor.visitFieldInsn(Opcodes.GETFIELD, getClassEnhancer().getASMClassName(),
            ClassEnhancer.FN_StateManager, ASMUtils.CD_StateManager);
        Label l1 = new Label();
        visitor.visitJumpInsn(Opcodes.IFNULL, l1);
        visitor.visitVarInsn(Opcodes.ALOAD, 0);
        visitor.visitFieldInsn(Opcodes.GETFIELD, getClassEnhancer().getASMClassName(),
            ClassEnhancer.FN_StateManager, ASMUtils.CD_StateManager);
        visitor.visitVarInsn(Opcodes.ALOAD, 0);
        visitor.visitVarInsn(Opcodes.ALOAD, 1);
        visitor.visitMethodInsn(Opcodes.INVOKEINTERFACE, ASMUtils.ACN_StateManager,
            "makeDirty", "(" + ASMUtils.CD_PersistenceCapable + "Ljava/lang/String;" + ")V");
        visitor.visitLabel(l1);

        if (cmd.isDetachable())
        {
            // if (jdoIsDetached())
            visitor.visitVarInsn(Opcodes.ALOAD, 0);
            visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, getClassEnhancer().getASMClassName(),
                "jdoIsDetached", "()Z");
            Label l3 = new Label();
            visitor.visitJumpInsn(Opcodes.IFEQ, l3);

            // if (fieldName != null && fieldName.indexOf('.') > 0)
            visitor.visitVarInsn(Opcodes.ALOAD, 1);
            visitor.visitJumpInsn(Opcodes.IFNULL, l3);
            visitor.visitVarInsn(Opcodes.ALOAD, 1);
            visitor.visitIntInsn(Opcodes.BIPUSH, 46);
            visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/String", "indexOf", "(I)I");
            visitor.visitJumpInsn(Opcodes.IFLE, l3);

            visitor.visitVarInsn(Opcodes.ALOAD, 1);
            visitor.visitInsn(Opcodes.ICONST_0);
            visitor.visitVarInsn(Opcodes.ALOAD, 1);
            visitor.visitIntInsn(Opcodes.BIPUSH, 46);
            visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/String", "lastIndexOf", "(I)I");
            visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/String", "substring", "(II)Ljava/lang/String;");
            visitor.visitVarInsn(Opcodes.ASTORE, 2);
            Label l5 = new Label();
            visitor.visitLabel(l5);
            visitor.visitVarInsn(Opcodes.ALOAD, 2);
            visitor.visitVarInsn(Opcodes.ALOAD, 0);
            visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Object", "getClass", "()Ljava/lang/Class;");
            visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Class", "getName", "()Ljava/lang/String;");
            visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/String", "equals", "(Ljava/lang/Object;)Z");
            Label l6 = new Label();
            visitor.visitJumpInsn(Opcodes.IFEQ, l6);
            visitor.visitInsn(Opcodes.ICONST_0);
            visitor.visitVarInsn(Opcodes.ISTORE, 3);
            Label l8 = new Label();
            visitor.visitLabel(l8);
            Label l9 = new Label();
            visitor.visitJumpInsn(Opcodes.GOTO, l9);

            Label l10 = new Label();
            visitor.visitLabel(l10);
            visitor.visitFieldInsn(Opcodes.GETSTATIC, getClassEnhancer().getASMClassName(),
                "jdoFieldNames", "[Ljava/lang/String;");
            visitor.visitVarInsn(Opcodes.ILOAD, 3);
            visitor.visitInsn(Opcodes.AALOAD);
            visitor.visitVarInsn(Opcodes.ALOAD, 1);
            visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/String", "equals", "(Ljava/lang/Object;)Z");
            Label l11 = new Label();
            visitor.visitJumpInsn(Opcodes.IFEQ, l11);
            visitor.visitVarInsn(Opcodes.ALOAD, 0);
            visitor.visitFieldInsn(Opcodes.GETFIELD, getClassEnhancer().getASMClassName(),
                "jdoDetachedState", "[Ljava/lang/Object;");
            visitor.visitInsn(Opcodes.ICONST_2);
            visitor.visitInsn(Opcodes.AALOAD);
            visitor.visitTypeInsn(Opcodes.CHECKCAST, "java/util/BitSet");
            visitor.visitVarInsn(Opcodes.ILOAD, 3);
            visitor.visitFieldInsn(Opcodes.GETSTATIC, getClassEnhancer().getASMClassName(),
                "jdoInheritedFieldCount", "I");
            visitor.visitInsn(Opcodes.IADD);
            visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/util/BitSet", "get", "(I)Z");
            Label l13 = new Label();
            visitor.visitJumpInsn(Opcodes.IFEQ, l13);
            visitor.visitVarInsn(Opcodes.ALOAD, 0);
            visitor.visitFieldInsn(Opcodes.GETFIELD, getClassEnhancer().getASMClassName(),
                "jdoDetachedState", "[Ljava/lang/Object;");
            visitor.visitInsn(Opcodes.ICONST_3);
            visitor.visitInsn(Opcodes.AALOAD);
            visitor.visitTypeInsn(Opcodes.CHECKCAST, "java/util/BitSet");
            visitor.visitVarInsn(Opcodes.ILOAD, 3);
            visitor.visitFieldInsn(Opcodes.GETSTATIC, getClassEnhancer().getASMClassName(),
                "jdoInheritedFieldCount", "I");
            visitor.visitInsn(Opcodes.IADD);
            visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/util/BitSet", "set", "(I)V");
            visitor.visitJumpInsn(Opcodes.GOTO, l3);
            visitor.visitLabel(l13);
            visitor.visitTypeInsn(Opcodes.NEW, "javax/jdo/JDODetachedFieldAccessException");
            visitor.visitInsn(Opcodes.DUP);
            visitor.visitLdcInsn("You have just attempted to access a field/property that hasn't been detached. Please detach it first before performing this operation");
            visitor.visitMethodInsn(Opcodes.INVOKESPECIAL, "javax/jdo/JDODetachedFieldAccessException", "<init>", "(Ljava/lang/String;)V");
            visitor.visitInsn(Opcodes.ATHROW);

            visitor.visitLabel(l11);
            visitor.visitIincInsn(3, 1);
            visitor.visitLabel(l9);
            visitor.visitVarInsn(Opcodes.ILOAD, 3);
            visitor.visitFieldInsn(Opcodes.GETSTATIC, getClassEnhancer().getASMClassName(),
                "jdoFieldNames", "[Ljava/lang/String;");
            visitor.visitInsn(Opcodes.ARRAYLENGTH);
            visitor.visitJumpInsn(Opcodes.IF_ICMPLT, l10);
            Label l18 = new Label();
            visitor.visitLabel(l18);
            visitor.visitJumpInsn(Opcodes.GOTO, l3);
            visitor.visitLabel(l6);

            if (pcSuperclassName != null)
            {
                // Relay to the superclass to see if it has this field
                visitor.visitVarInsn(Opcodes.ALOAD, 0);
                visitor.visitVarInsn(Opcodes.ALOAD, 1);
                visitor.visitMethodInsn(Opcodes.INVOKESPECIAL, pcSuperclassName.replace('.', '/'),
                    "jdoMakeDirty", "(Ljava/lang/String;)V");
            }

            visitor.visitLabel(l3);
            visitor.visitInsn(Opcodes.RETURN);
            Label endLabel = new Label();
            visitor.visitLabel(endLabel);
            visitor.visitLocalVariable("this", getClassEnhancer().getClassDescriptor(), null, startLabel, endLabel, 0);
            visitor.visitLocalVariable(argNames[0], "Ljava/lang/String;", null, startLabel, endLabel, 1);
            visitor.visitLocalVariable("className", "Ljava/lang/String;", null, l5, l3, 2);
            visitor.visitLocalVariable("i", "I", null, l8, l18, 3);
            visitor.visitMaxs(4, 4);
        }
        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], "Ljava/lang/String;", null, startLabel, endLabel, 1);
            visitor.visitMaxs(3, 2);
        }

        visitor.visitEnd();
    }
}
TOP

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

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.