Search in sources :

Example 26 with MethodVisitor

use of org.apache.xbean.asm6.MethodVisitor in project component-runtime by Talend.

the class PluginGenerator method addConstructor.

private void addConstructor(final ClassWriter writer) {
    final MethodVisitor constructor = writer.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
    constructor.visitCode();
    constructor.visitVarInsn(ALOAD, 0);
    constructor.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
    constructor.visitInsn(RETURN);
    constructor.visitMaxs(1, 1);
    constructor.visitEnd();
}
Also used : MethodVisitor(org.apache.xbean.asm6.MethodVisitor)

Example 27 with MethodVisitor

use of org.apache.xbean.asm6.MethodVisitor in project component-runtime by Talend.

the class PluginGenerator method createProcessor.

private byte[] createProcessor(final JarOutputStream outputStream, final String packageName) throws IOException {
    final String className = packageName + "/AProcessor.class";
    outputStream.putNextEntry(new ZipEntry(className));
    final ClassWriter writer = new ClassWriter(COMPUTE_FRAMES);
    final AnnotationVisitor processorAnnotation = writer.visitAnnotation(Type.getDescriptor(Processor.class), true);
    processorAnnotation.visit("family", "comp");
    processorAnnotation.visit("name", "proc");
    processorAnnotation.visitEnd();
    writer.visit(V1_8, ACC_PUBLIC + ACC_SUPER, className.substring(0, className.length() - ".class".length()), null, Type.getInternalName(Object.class), new String[] { Serializable.class.getName().replace(".", "/") });
    writer.visitSource(className.replace(".class", ".java"), null);
    addConstructor(writer);
    // generate a processor
    final MethodVisitor emitMethod = writer.visitMethod(ACC_PUBLIC, "emit", "(L" + packageName + "/AModel;)L" + packageName + "/AModel;", null, new String[0]);
    emitMethod.visitAnnotation(Type.getDescriptor(ElementListener.class), true).visitEnd();
    emitMethod.visitCode();
    emitMethod.visitTypeInsn(NEW, packageName + "/AModel");
    emitMethod.visitInsn(DUP);
    emitMethod.visitMethodInsn(INVOKESPECIAL, packageName + "/AModel", "<init>", "()V", false);
    emitMethod.visitInsn(ARETURN);
    emitMethod.visitInsn(ARETURN);
    emitMethod.visitMaxs(1, 1);
    emitMethod.visitEnd();
    writer.visitEnd();
    return writer.toByteArray();
}
Also used : Processor(org.talend.sdk.component.api.processor.Processor) ZipEntry(java.util.zip.ZipEntry) AnnotationVisitor(org.apache.xbean.asm6.AnnotationVisitor) ClassWriter(org.apache.xbean.asm6.ClassWriter) MethodVisitor(org.apache.xbean.asm6.MethodVisitor)

Example 28 with MethodVisitor

use of org.apache.xbean.asm6.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();
}
Also used : MethodVisitor(org.apache.xbean.asm9.MethodVisitor)

Example 29 with MethodVisitor

use of org.apache.xbean.asm6.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();
}
Also used : Label(org.apache.xbean.asm9.Label) MethodVisitor(org.apache.xbean.asm9.MethodVisitor)

Example 30 with MethodVisitor

use of org.apache.xbean.asm6.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();
}
Also used : Method(java.lang.reflect.Method) MethodVisitor(org.apache.xbean.asm9.MethodVisitor)

Aggregations

MethodVisitor (org.apache.xbean.asm9.MethodVisitor)30 MethodVisitor (jodd.asm5.MethodVisitor)6 MethodVisitor (org.apache.xbean.asm6.MethodVisitor)6 ClassWriter (org.apache.xbean.asm9.ClassWriter)6 Method (java.lang.reflect.Method)5 Label (org.apache.xbean.asm9.Label)5 Type (org.apache.xbean.asm9.Type)3 ArrayList (java.util.ArrayList)2 Arrays.asList (java.util.Arrays.asList)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 ZipEntry (java.util.zip.ZipEntry)2 EmptyClassVisitor (jodd.asm.EmptyClassVisitor)2 EmptyMethodVisitor (jodd.asm.EmptyMethodVisitor)2 AnnotationVisitor (org.apache.xbean.asm6.AnnotationVisitor)2 ClassWriter (org.apache.xbean.asm6.ClassWriter)2 AnnotationVisitor (org.apache.xbean.asm9.AnnotationVisitor)2 InterceptorHandler (org.talend.sdk.component.api.service.interceptor.InterceptorHandler)2 IOException (java.io.IOException)1