org.datanucleus.enhancer.asm.method.JdoNewObjectIdInstance1 Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of datanucleus-enhancer
Show all versions of datanucleus-enhancer
DataNucleus Enhancer is a Java byte-code enhancer for use with DataNucleus.
/**********************************************************************
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.metadata.AbstractMemberMetaData;
import org.datanucleus.metadata.ClassMetaData;
import org.datanucleus.metadata.IdentityType;
import org.datanucleus.metadata.PropertyMetaData;
import org.datanucleus.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
*
* public Object jdoNewObjectIdInstance()
* {
* return null;
* }
*
* and for SingleFieldIdentity
*
* public Object jdoNewObjectIdInstance()
* {
* return new YYYIdentity(getClass(), id);
* }
*
* and for user-supplied object identity class
*
* public Object jdoNewObjectIdInstance()
* {
* return new UserPrimaryKey();
* }
*
*/
public class JdoNewObjectIdInstance1 extends ASMClassMethod
{
public static JdoNewObjectIdInstance1 getInstance(ClassEnhancer enhancer)
{
return new JdoNewObjectIdInstance1(enhancer, enhancer.getNewObjectIdInstanceMethodName(),
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
if (!cmd.isInstantiable())
{
// Application identity but mapped-superclass with no PK defined, so throw exception
visitor.visitTypeInsn(Opcodes.NEW, "javax/jdo/JDOFatalInternalException");
visitor.visitInsn(Opcodes.DUP);
visitor.visitLdcInsn("This class has no identity");
visitor.visitMethodInsn(Opcodes.INVOKESPECIAL, "javax/jdo/JDOFatalInternalException", "", "(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.visitMaxs(3, 1);
}
else
{
String objectIdClass = cmd.getObjectidClass();
int[] pkFieldNums = cmd.getPKMemberPositions();
if (enhancer.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 instanceof PropertyMetaData)
{
// Persistent property so use jdoGetXXX()
visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, getClassEnhancer().getASMClassName(),
enhancer.getGetMethodPrefixMethodName() + 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, "",
"(Ljava/lang/Class;" + Type.getDescriptor(fmd.getType()) + ")V");
}
else
{
visitor.visitMethodInsn(Opcodes.INVOKESPECIAL, ACN_objectIdClass, "",
"(Ljava/lang/Class;" + enhancer.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, "", "()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();
}
}