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

org.datanucleus.enhancer.asm.method.JdoReplaceField Maven / Gradle / Ivy

Go to download

DataNucleus Enhancer is a Java byte-code enhancer for use with DataNucleus.

There is a newer version: 3.1.1
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.asm.method;

import org.datanucleus.enhancer.ClassEnhancer;
import org.datanucleus.enhancer.asm.ASMClassMethod;
import org.datanucleus.enhancer.asm.ASMUtils;
import org.datanucleus.metadata.AbstractMemberMetaData;
import org.datanucleus.metadata.PropertyMetaData;
import org.objectweb.asm.Label;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.Type;

/**
 * Method to generate the method "jdoReplaceField" using ASM.
 * 
 * public void jdoReplaceField(int fieldIndex)
 * {
 *     if (jdoStateManager == null)
 *         throw new IllegalStateException("state manager is null");
 *     switch (fieldIndex)
 *     {
 *         case 0:
 *         {
 *             MyClass ref = this;
 *             ref.param1 = (MyOtherClass) ref.jdoStateManager.replacingObjectField(this, fieldIndex);
 *             break;
 *         }
 *         default:
 *             throw new IllegalArgumentException("out of field index :" + fieldIndex);
 *     }
 * }
 * 
* or with superclass *
 * public void jdoReplaceField(int fieldIndex)
 * {
 *     if (jdoStateManager == null)
 *         throw new IllegalStateException("state manager is null");
 *     switch (fieldIndex - jdoInheritedFieldCount)
 *     {
 *         case 0:
 *         {
 *             MyClass ref = this;
 *             ref.param1 = (MyOtherClass)ref.jdoStateManager.replacingStringField(this, fieldIndex);
 *             break;
 *         }
 *         default:
 *             super.jdoReplaceField(fieldIndex);
 *     }
 * }
 * 
*/ public class JdoReplaceField extends ASMClassMethod { public static JdoReplaceField getInstance(ClassEnhancer enhancer) { return new JdoReplaceField(enhancer, enhancer.getReplaceFieldMethodName(), Opcodes.ACC_PUBLIC, null, new Class[] {int.class}, new String[] {"index"}); } /** * 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 JdoReplaceField(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() { AbstractMemberMetaData fields[] = enhancer.getClassMetaData().getManagedMembers(); String pcSuperclassName = enhancer.getClassMetaData().getPersistenceCapableSuperclass(); 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(), enhancer.getStateManagerFieldName(), enhancer.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", "", "(Ljava/lang/String;)V"); visitor.visitInsn(Opcodes.ATHROW); visitor.visitLabel(l1); visitor.visitVarInsn(Opcodes.ILOAD, 1); visitor.visitFieldInsn(Opcodes.GETSTATIC, getClassEnhancer().getASMClassName(), enhancer.getInheritedFieldCountFieldName(), "I"); visitor.visitInsn(Opcodes.ISUB); Label[] fieldOptions = new Label[fields.length]; for (int i=0;i 0) { visitor.visitVarInsn(Opcodes.ALOAD, 0); visitor.visitFieldInsn(Opcodes.GETFIELD, getClassEnhancer().getASMClassName(), enhancer.getStateManagerFieldName(), enhancer.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", "", "(Ljava/lang/String;)V"); visitor.visitInsn(Opcodes.ATHROW); visitor.visitLabel(l1); visitor.visitVarInsn(Opcodes.ILOAD, 1); Label[] fieldOptions = new Label[fields.length]; for (int i=0;i", "(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", "", "(Ljava/lang/String;)V"); visitor.visitInsn(Opcodes.ATHROW); // End of switch visitor.visitLabel(endSwitchLabel); 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(); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy