org.datanucleus.enhancer.asm.method.InitClass 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.ASMClassEnhancer;
import org.datanucleus.enhancer.asm.ASMClassMethod;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
/**
* Method to generate a static initialisation block for the class using ASM.
*
* static
* {
* jdoFieldNames = __jdoFieldNamesInit();
* jdoFieldTypes = __jdoFieldTypesInit();
* jdoFieldFlags = __jdoFieldFlagsInit();
* jdoInheritedFieldCount = __jdoGetInheritedFieldCount();
* jdoPersistenceCapableSuperclass = __jdoPersistenceCapableSuperclassInit();
* JDOImplHelper.registerClass(___jdo$loadClass("mydomain.MyClass"),
* jdoFieldNames, jdoFieldTypes, jdoFieldFlags,
* jdoPersistenceCapableSuperclass, new MyClass());
* }
*
*/
public class InitClass extends ASMClassMethod
{
public static InitClass getInstance(ASMClassEnhancer enhancer)
{
return new InitClass(enhancer, "",
Opcodes.ACC_STATIC,
null, 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 InitClass(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();
addInitialiseInstructions(visitor);
visitor.visitInsn(Opcodes.RETURN);
visitor.visitMaxs(7, 0);
visitor.visitEnd();
}
/**
* Convenience method to add the initialise instructions to the supplied MethodVisitor.
* Available as a separate method so that the initialise instructions can be added to an existing
* static class initialise block (where the class already has one).
* @param mv MethodVisitor to use
*/
public void addInitialiseInstructions(MethodVisitor mv)
{
mv.visitMethodInsn(Opcodes.INVOKESTATIC, getClassEnhancer().getASMClassName(),
enhancer.getFieldNamesInitMethodName(), "()[Ljava/lang/String;");
mv.visitFieldInsn(Opcodes.PUTSTATIC, getClassEnhancer().getASMClassName(),
enhancer.getFieldNamesFieldName(), "[Ljava/lang/String;");
mv.visitMethodInsn(Opcodes.INVOKESTATIC, getClassEnhancer().getASMClassName(),
enhancer.getFieldTypesInitMethodName(), "()[Ljava/lang/Class;");
mv.visitFieldInsn(Opcodes.PUTSTATIC, getClassEnhancer().getASMClassName(),
enhancer.getFieldTypesFieldName(), "[Ljava/lang/Class;");
mv.visitMethodInsn(Opcodes.INVOKESTATIC, getClassEnhancer().getASMClassName(),
enhancer.getFieldFlagsInitMethodName(), "()[B");
mv.visitFieldInsn(Opcodes.PUTSTATIC, getClassEnhancer().getASMClassName(),
enhancer.getFieldFlagsFieldName(), "[B");
mv.visitMethodInsn(Opcodes.INVOKESTATIC, getClassEnhancer().getASMClassName(),
enhancer.getGetInheritedFieldCountMethodName(), "()I");
mv.visitFieldInsn(Opcodes.PUTSTATIC, getClassEnhancer().getASMClassName(),
enhancer.getInheritedFieldCountFieldName(), "I");
mv.visitMethodInsn(Opcodes.INVOKESTATIC, getClassEnhancer().getASMClassName(),
enhancer.getPersistableSuperclassInitMethodName(), "()Ljava/lang/Class;");
mv.visitFieldInsn(Opcodes.PUTSTATIC, getClassEnhancer().getASMClassName(),
enhancer.getPersistableSuperclassFieldName(), "Ljava/lang/Class;");
mv.visitLdcInsn(getClassEnhancer().className);
mv.visitMethodInsn(Opcodes.INVOKESTATIC, getClassEnhancer().getASMClassName(),
enhancer.getLoadClassMethodName(), "(Ljava/lang/String;)Ljava/lang/Class;");
mv.visitFieldInsn(Opcodes.GETSTATIC, getClassEnhancer().getASMClassName(),
enhancer.getFieldNamesFieldName(), "[Ljava/lang/String;");
mv.visitFieldInsn(Opcodes.GETSTATIC, getClassEnhancer().getASMClassName(),
enhancer.getFieldTypesFieldName(), "[Ljava/lang/Class;");
mv.visitFieldInsn(Opcodes.GETSTATIC, getClassEnhancer().getASMClassName(),
enhancer.getFieldFlagsFieldName(), "[B");
mv.visitFieldInsn(Opcodes.GETSTATIC, getClassEnhancer().getASMClassName(),
enhancer.getPersistableSuperclassFieldName(), "Ljava/lang/Class;");
if (enhancer.getClassMetaData().isAbstract())
{
mv.visitInsn(Opcodes.ACONST_NULL);
}
else
{
mv.visitTypeInsn(Opcodes.NEW, getClassEnhancer().getASMClassName());
mv.visitInsn(Opcodes.DUP);
mv.visitMethodInsn(Opcodes.INVOKESPECIAL, getClassEnhancer().getASMClassName(),
"", "()V");
}
mv.visitMethodInsn(Opcodes.INVOKESTATIC, enhancer.getImplHelperAsmClassName(), "registerClass",
"(Ljava/lang/Class;" +
"[Ljava/lang/String;" +
"[Ljava/lang/Class;" +
"[BLjava/lang/Class;" +
"L" + enhancer.getPersistableAsmClassName() + ";)V");
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy