use of org.apache.xbean.asm9.MethodVisitor in project tomee by apache.
the class MakeTxLookup method createNewHibernateStrategy.
private static void createNewHibernateStrategy(final File basedir, final String target, final String abstractJtaPlatformPackage) throws Exception {
final ClassWriter cw = new ClassWriter(0);
MethodVisitor mv;
cw.visit(V1_6, ACC_PUBLIC + ACC_SUPER, target.replace('.', '/'), null, abstractJtaPlatformPackage + "/AbstractJtaPlatform", null);
{
mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
mv.visitCode();
mv.visitVarInsn(ALOAD, 0);
mv.visitMethodInsn(INVOKESPECIAL, abstractJtaPlatformPackage + "/AbstractJtaPlatform", "<init>", "()V", false);
mv.visitInsn(RETURN);
mv.visitMaxs(1, 1);
mv.visitEnd();
}
{
mv = cw.visitMethod(ACC_PROTECTED, "locateTransactionManager", "()Ljavax/transaction/TransactionManager;", null, null);
mv.visitCode();
mv.visitMethodInsn(INVOKESTATIC, "org/apache/openejb/OpenEJB", "getTransactionManager", "()Ljavax/transaction/TransactionManager;", false);
mv.visitInsn(ARETURN);
mv.visitMaxs(1, 1);
mv.visitEnd();
}
{
mv = cw.visitMethod(ACC_PROTECTED, "locateUserTransaction", "()Ljavax/transaction/UserTransaction;", null, null);
mv.visitCode();
final Label l0 = new Label();
final Label l1 = new Label();
final Label l2 = new Label();
mv.visitTryCatchBlock(l0, l1, l2, "javax/naming/NamingException");
mv.visitLabel(l0);
mv.visitMethodInsn(INVOKESTATIC, "org/apache/openejb/loader/SystemInstance", "get", "()Lorg/apache/openejb/loader/SystemInstance;", false);
mv.visitLdcInsn(Type.getType("Lorg/apache/openejb/spi/ContainerSystem;"));
mv.visitMethodInsn(INVOKEVIRTUAL, "org/apache/openejb/loader/SystemInstance", "getComponent", "(Ljava/lang/Class;)Ljava/lang/Object;", false);
mv.visitTypeInsn(CHECKCAST, "org/apache/openejb/spi/ContainerSystem");
mv.visitMethodInsn(INVOKEINTERFACE, "org/apache/openejb/spi/ContainerSystem", "getJNDIContext", "()Ljavax/naming/Context;", true);
mv.visitLdcInsn("comp/UserTransaction");
mv.visitMethodInsn(INVOKEINTERFACE, "javax/naming/Context", "lookup", "(Ljava/lang/String;)Ljava/lang/Object;", true);
mv.visitTypeInsn(CHECKCAST, "javax/transaction/UserTransaction");
mv.visitLabel(l1);
mv.visitInsn(ARETURN);
mv.visitLabel(l2);
mv.visitFrame(Opcodes.F_SAME1, 0, null, 1, new Object[] { "javax/naming/NamingException" });
mv.visitVarInsn(ASTORE, 1);
mv.visitInsn(ACONST_NULL);
mv.visitInsn(ARETURN);
mv.visitMaxs(2, 2);
mv.visitEnd();
}
cw.visitEnd();
write(basedir, cw, target.replace('.', '/'));
}
use of org.apache.xbean.asm9.MethodVisitor in project tomee by apache.
the class Cmp2Generator method createCmrGetter.
/**
* Create a getter method for the CMR field. This
* will used the accessor object initialized into the
* name + "Cmr" field that was already generated and
* initialized at object creation. The accessor
* object manages the object relationship.
*
* @param cmrField The field we're generating.
*/
private void createCmrGetter(final CmrField cmrField) {
// no back reference. We don't generate a getter method for this
if (cmrField.isSynthetic()) {
return;
}
final String methodName = getterName(cmrField.getName());
final MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, methodName, "()" + cmrField.getProxyDescriptor(), null, null);
mv.visitCode();
mv.visitVarInsn(ALOAD, 0);
mv.visitFieldInsn(GETFIELD, implClassName, cmrField.getName() + "Cmr", cmrField.getAccessorDescriptor());
mv.visitVarInsn(ALOAD, 0);
mv.visitFieldInsn(GETFIELD, implClassName, cmrField.getName(), cmrField.getDescriptor());
// return this.${cmrField.name}Cmr.get(this.${cmdField.name});
// this takes the value stored in the CMR field (which might be a single value or
// a Set or Collection), and hands it to the appropriate accessor.
mv.visitMethodInsn(INVOKEVIRTUAL, cmrField.getAccessorInternalName(), "get", cmrField.getCmrStyle().getGetterDescriptor(), false);
// to the target class before returning.
if (cmrField.getCmrStyle() == CmrStyle.SINGLE) {
mv.visitTypeInsn(CHECKCAST, cmrField.getProxyType().getInternalName());
}
mv.visitInsn(ARETURN);
mv.visitMaxs(0, 0);
mv.visitEnd();
}
use of org.apache.xbean.asm9.MethodVisitor in project tomee by apache.
the class Cmp2Generator method createOpenEJB_deleted.
private void createOpenEJB_deleted() {
final MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "OpenEJB_deleted", "()V", null, null);
mv.visitCode();
/* if (deleted) return; */
mv.visitVarInsn(ALOAD, 0);
mv.visitFieldInsn(GETFIELD, implClassName, DELETED, "Z");
final Label notDeleted = new Label();
mv.visitJumpInsn(IFEQ, notDeleted);
mv.visitInsn(RETURN);
mv.visitLabel(notDeleted);
// deleted = true;
mv.visitVarInsn(ALOAD, 0);
mv.visitInsn(ICONST_1);
mv.visitFieldInsn(PUTFIELD, implClassName, DELETED, "Z");
for (final CmrField cmrField : cmrFields) {
// ${cmrField.accessor}.delete(${cmrField.name});
createOpenEJB_deleted(mv, cmrField);
}
// return;
mv.visitInsn(RETURN);
mv.visitMaxs(0, 0);
mv.visitEnd();
}
use of org.apache.xbean.asm9.MethodVisitor in project tomee by apache.
the class Cmp2Generator method createSelectMethod.
/**
* Generate a concrete implementation of an abstract
* ejbSelectxxxx method.
*
* @param selectMethod The abstract definition for the method we're generating.
*/
private void createSelectMethod(final Method selectMethod) {
final Class<?> returnType = selectMethod.getReturnType();
final Method executeMethod = EjbSelect.getSelectMethod(returnType);
final MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, selectMethod.getName(), Type.getMethodDescriptor(selectMethod), null, getExceptionTypes(selectMethod));
mv.visitCode();
// push deploymentInfo
mv.visitFieldInsn(GETSTATIC, implClassName, "deploymentInfo", "Ljava/lang/Object;");
// push method signature
mv.visitLdcInsn(getSelectMethodSignature(selectMethod));
// one of the primitives.
if (!returnType.isPrimitive()) {
mv.visitLdcInsn(returnType.getName());
}
// new Object[]
mv.visitIntInsn(BIPUSH, selectMethod.getParameterTypes().length);
mv.visitTypeInsn(ANEWARRAY, "java/lang/Object");
// object[i] = arg${i}
int i = 0;
for (final Class<?> parameterType : selectMethod.getParameterTypes()) {
// push arguement i on stack
mv.visitInsn(DUP);
bipush(mv, i);
mv.visitVarInsn(Type.getType(parameterType).getOpcode(ILOAD), i + 1);
// convert argument on stack to an Object
Convert.toObjectFrom(mv, parameterType);
// store it into the array
mv.visitInsn(AASTORE);
if (long.class.equals(parameterType) || double.class.equals(parameterType)) {
// longs and doubles are double wide
i = i + 2;
} else {
i++;
}
}
// EjbSelect.execute_xxxx(deploymentInfo, signature, [returnType.] args[]);
mv.visitMethodInsn(INVOKESTATIC, Type.getInternalName(executeMethod.getDeclaringClass()), executeMethod.getName(), Type.getMethodDescriptor(executeMethod), false);
// needs to match the type returned from the method call
if (!Void.TYPE.equals(returnType)) {
// stack already
if (!returnType.isPrimitive()) {
// convert return type
Convert.fromObjectTo(mv, returnType);
}
// And generate the appropriate return for the type
mv.visitInsn(Type.getReturnType(selectMethod).getOpcode(IRETURN));
} else {
// void return is just a RETURN.
mv.visitInsn(RETURN);
}
// close method
mv.visitMaxs(0, 0);
mv.visitEnd();
}
use of org.apache.xbean.asm9.MethodVisitor in project tomee by apache.
the class Cmp2Generator method createConstructor.
private void createConstructor() {
final MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
mv.visitCode();
mv.visitVarInsn(ALOAD, 0);
mv.visitMethodInsn(INVOKESPECIAL, beanClassName, "<init>", "()V", false);
for (final CmrField cmrField : cmrFields) {
initCmrFields(mv, cmrField);
}
mv.visitInsn(RETURN);
mv.visitMaxs(0, 0);
mv.visitEnd();
}
Aggregations