Example usage for org.objectweb.asm MethodVisitor visitCode

List of usage examples for org.objectweb.asm MethodVisitor visitCode

Introduction

In this page you can find the example usage for org.objectweb.asm MethodVisitor visitCode.

Prototype

public void visitCode() 

Source Link

Document

Starts the visit of the method's code, if any (i.e.

Usage

From source file:org.apache.pig.builtin.InvokerGenerator.java

License:Apache License

private void makeConstructor(ClassWriter cw) {
    MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
    mv.visitCode();
    mv.visitVarInsn(ALOAD, 0);//from   w  ww.ja  v a 2s .co m
    mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V");
    mv.visitInsn(RETURN);
    mv.visitMaxs(1, 1);
    mv.visitEnd();
}

From source file:org.apache.s4.core.gen.OverloadDispatcherGenerator.java

License:Apache License

public Class<?> generate() {
    Random rand = new Random(System.currentTimeMillis());
    String dispatcherClassName = "OverloadDispatcher" + (Math.abs(rand.nextInt() % 3256));

    // class headers
    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
    // CheckClassAdapter cw = new CheckClassAdapter(cw1);
    cw.visit(V1_6, ACC_PUBLIC + ACC_SUPER, dispatcherClassName, null, Type.getInternalName(Object.class),
            new String[] { Type.getInternalName(OverloadDispatcher.class) });

    // constructor
    MethodVisitor mv1 = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
    mv1.visitCode();
    Label l0 = new Label();
    mv1.visitLabel(l0);// ww  w . java  2  s. c  om
    mv1.visitVarInsn(ALOAD, 0);
    mv1.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V");
    Label l1 = new Label();
    mv1.visitLabel(l1);
    mv1.visitInsn(RETURN);
    Label l2 = new Label();
    mv1.visitLabel(l2);
    mv1.visitLocalVariable("this", "Lio/s4/core/" + dispatcherClassName + ";", null, l0, l2, 0);
    mv1.visitMaxs(1, 1);

    mv1.visitEnd();

    // dispatch input events method
    generateEventDispatchMethod(cw, "dispatchEvent", inputEventHierarchies, "onEvent");
    // dispatch output events method
    generateEventDispatchMethod(cw, "dispatchTrigger", outputEventHierarchies, "onTrigger");

    cw.visitEnd();

    if (DUMP) {
        try {
            LoggerFactory.getLogger(getClass())
                    .debug("Dumping generated overload dispatcher class for PE of class [" + targetClass + "]");
            Files.write(cw.toByteArray(),
                    new File(System.getProperty("java.io.tmpdir") + "/" + dispatcherClassName + ".class"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return new OverloadDispatcherClassLoader(targetClass.getClassLoader())
            .loadClassFromBytes(dispatcherClassName, cw.toByteArray());

}

From source file:org.apache.s4.core.gen.OverloadDispatcherGenerator.java

License:Apache License

private void generateEventDispatchMethod(ClassWriter cw, String dispatchMethodName,
        List<Hierarchy> eventHierarchies, String processEventMethodName) {
    MethodVisitor mv2 = cw.visitMethod(ACC_PUBLIC, dispatchMethodName,
            "(" + Type.getType(ProcessingElement.class).getDescriptor()
                    + Type.getType(Event.class).getDescriptor() + ")V",
            null, null);/*from w  w w. ja va2s .co  m*/
    mv2.visitCode();
    Label l3 = new Label();
    mv2.visitLabel(l3);
    mv2.visitVarInsn(ALOAD, 1);
    mv2.visitTypeInsn(CHECKCAST, Type.getInternalName(targetClass));
    mv2.visitVarInsn(ASTORE, 3);
    boolean first = true;
    Label aroundLabel = new Label();
    for (Hierarchy hierarchy : eventHierarchies) {
        if (first) {
            Label l4 = new Label();
            mv2.visitLabel(l4);
        }
        mv2.visitVarInsn(ALOAD, 2);
        mv2.visitTypeInsn(INSTANCEOF, Type.getInternalName(hierarchy.getTop()));

        Label l5 = new Label();
        mv2.visitJumpInsn(IFEQ, l5);

        Label l6 = new Label();
        mv2.visitLabel(l6);
        mv2.visitVarInsn(ALOAD, 3);
        mv2.visitVarInsn(ALOAD, 2);
        mv2.visitTypeInsn(CHECKCAST, Type.getInternalName(hierarchy.getTop()));
        mv2.visitMethodInsn(INVOKEVIRTUAL, Type.getInternalName(targetClass), processEventMethodName,
                "(" + Type.getDescriptor(hierarchy.getTop()) + ")V");
        mv2.visitJumpInsn(Opcodes.GOTO, aroundLabel);
        mv2.visitLabel(l5);

        if (first) {
            mv2.visitFrame(F_APPEND, 1, new Object[] { Type.getInternalName(targetClass) }, 0, null);
            first = false;
        } else {
            mv2.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
        }
    }
    addErrorLogStatement(mv2);
    if (eventHierarchies.size() > 0) {
        mv2.visitLabel(aroundLabel);
        mv2.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
    }
    mv2.visitInsn(RETURN);
    Label l8 = new Label();
    mv2.visitLabel(l8);
    mv2.visitLocalVariable("pe", Type.getDescriptor(ProcessingElement.class), null, l3, l8, 1);
    mv2.visitLocalVariable("event", Type.getDescriptor(Event.class), null, l3, l8, 2);
    mv2.visitLocalVariable("typedPE", Type.getDescriptor(targetClass), null, l3, l8, 3);
    mv2.visitMaxs(4, 4);
    mv2.visitEnd();
}

From source file:org.apache.tuscany.sca.interfacedef.java.jaxrs.RootResourceClassGenerator.java

License:Apache License

private static void generateMethod(ClassWriter cw, String interfaceName, String className, Method method,
        String consumes, String produces) {
    String methodDescriptor = Type.getMethodDescriptor(method);

    String signatureString = getSignature(method);

    MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, method.getName(), methodDescriptor, signatureString,
            getExceptionInternalNames(method));

    mv.visitCode();
    mv.visitFieldInsn(GETSTATIC, className, DELEGATE_FIELD, getSignature(interfaceName));
    Class<?>[] paramTypes = method.getParameterTypes();
    int index = 1;
    for (int i = 0; i < paramTypes.length; i++) {
        String signature = Type.getDescriptor(paramTypes[i]);
        mv.visitVarInsn(CodeGenerationHelper.getLoadOPCode(signature), index);
        if (paramTypes[i] == long.class || paramTypes[i] == double.class) {
            index += 2; // Increase the index by 2 for the 64bit numbers
        } else {//  w  ww .  ja va  2s  .  c  o  m
            index++;
        }
    }
    mv.visitMethodInsn(INVOKEINTERFACE, interfaceName, method.getName(), methodDescriptor);

    Class<?> returnType = method.getReturnType();
    mv.visitInsn(CodeGenerationHelper.getReturnOPCode(Type.getDescriptor(returnType)));
    int size = paramTypes.length + 1;
    mv.visitMaxs(size, size);
    mv.visitEnd();
}

From source file:org.apache.tuscany.sca.interfacedef.java.jaxrs.RootResourceClassGenerator.java

License:Apache License

private static void declareConstructor(ClassWriter cw, String className) {
    MethodVisitor mv;
    mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
    mv.visitCode();
    Label l0 = new Label();
    mv.visitLabel(l0);/*from www .  j  a  v  a 2s.co m*/
    // mv.visitLineNumber(37, l0);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V");
    mv.visitInsn(RETURN);
    Label l1 = new Label();
    mv.visitLabel(l1);
    mv.visitLocalVariable("this", getSignature(className), null, l0, l1, 0);
    mv.visitMaxs(1, 1);
    mv.visitEnd();
}

From source file:org.apache.tuscany.sca.interfacedef.java.jaxws.BaseBeanGenerator.java

License:Apache License

protected void declareSetter(ClassWriter cw, String classDescriptor, String classSignature, String propName,
        String propClassSignature, String propTypeSignature) {
    if ("Ljava/util/List;".equals(propClassSignature)) {
        return;//from  www  .jav  a2  s .  com
    }
    MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "set" + capitalize(propName), "(" + propClassSignature + ")V",
            propTypeSignature == null ? null : "(" + propTypeSignature + ")V", null);
    mv.visitCode();
    Label l0 = new Label();
    mv.visitLabel(l0);
    // mv.visitLineNumber(57, l0);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitVarInsn(CodeGenerationHelper.getLoadOPCode(propClassSignature), 1);
    mv.visitFieldInsn(PUTFIELD, classDescriptor, getFieldName(propName), propClassSignature);
    Label l1 = new Label();
    mv.visitLabel(l1);
    // mv.visitLineNumber(58, l1);
    mv.visitInsn(RETURN);
    Label l2 = new Label();
    mv.visitLabel(l2);
    mv.visitLocalVariable("this", classSignature, null, l0, l2, 0);
    mv.visitLocalVariable(getFieldName(propName), propClassSignature, propTypeSignature, l0, l2, 1);
    mv.visitMaxs(3, 3);
    mv.visitEnd();

}

From source file:org.apache.tuscany.sca.interfacedef.java.jaxws.BaseBeanGenerator.java

License:Apache License

protected void decalreGetter(ClassWriter cw, String classDescriptor, String classSignature, String propName,
        String propClassSignature, String propTypeSignature) {
    String collectionImplClass = COLLECTION_CLASSES.get(propClassSignature);
    if (collectionImplClass != null) {
        decalreCollectionGetter(cw, classDescriptor, classSignature, propName, propClassSignature,
                propTypeSignature, collectionImplClass);
        return;/*from  w ww  . j  a  va2  s .  com*/
    }

    String getterName = ("Z".equals(propClassSignature) ? "is" : "get") + capitalize(propName);
    MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, getterName, "()" + propClassSignature,
            propTypeSignature == null ? null : "()" + propTypeSignature, null);
    mv.visitCode();
    Label l0 = new Label();
    mv.visitLabel(l0);
    // mv.visitLineNumber(48, l0);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, classDescriptor, getFieldName(propName), propClassSignature);
    mv.visitInsn(CodeGenerationHelper.getReturnOPCode(propClassSignature));
    Label l1 = new Label();
    mv.visitLabel(l1);
    mv.visitLocalVariable("this", classSignature, null, l0, l1, 0);
    mv.visitMaxs(2, 1);
    mv.visitEnd();
}

From source file:org.apache.tuscany.sca.interfacedef.java.jaxws.BaseBeanGenerator.java

License:Apache License

protected void decalreCollectionGetter(ClassWriter cw, String classDescriptor, String classSignature,
        String propName, String propClassSignature, String propTypeSignature, String collectionImplClass) {
    String getterName = "get" + capitalize(propName);
    String fieldName = getFieldName(propName);
    MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, getterName, "()" + propClassSignature,
            propTypeSignature == null ? null : "()" + propTypeSignature, null);
    mv.visitCode();
    Label l0 = new Label();
    mv.visitLabel(l0);/*from www  .j  av  a  2  s  . com*/
    mv.visitLineNumber(63, l0);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, classDescriptor, fieldName, propClassSignature);
    Label l1 = new Label();
    mv.visitJumpInsn(IFNONNULL, l1);
    Label l2 = new Label();
    mv.visitLabel(l2);
    mv.visitLineNumber(64, l2);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitTypeInsn(NEW, collectionImplClass);
    mv.visitInsn(DUP);
    mv.visitMethodInsn(INVOKESPECIAL, collectionImplClass, "<init>", "()V");
    mv.visitFieldInsn(PUTFIELD, classDescriptor, fieldName, propClassSignature);
    mv.visitLabel(l1);
    mv.visitLineNumber(66, l1);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, classDescriptor, fieldName, propClassSignature);
    mv.visitInsn(ARETURN);
    Label l3 = new Label();
    mv.visitLabel(l3);
    mv.visitLocalVariable("this", classSignature, null, l0, l3, 0);
    mv.visitMaxs(3, 1);
    mv.visitEnd();
}

From source file:org.apache.tuscany.sca.interfacedef.java.jaxws.BaseBeanGenerator.java

License:Apache License

protected void declareConstructor(ClassWriter cw, String classSignature) {
    MethodVisitor mv;
    mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
    mv.visitCode();
    Label l0 = new Label();
    mv.visitLabel(l0);//from  w w w .  ja  va  2s . com
    // mv.visitLineNumber(37, l0);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V");
    mv.visitInsn(RETURN);
    Label l1 = new Label();
    mv.visitLabel(l1);
    mv.visitLocalVariable("this", classSignature, null, l0, l1, 0);
    mv.visitMaxs(1, 1);
    mv.visitEnd();
}

From source file:org.ballerinalang.nativeimpl.jvm.methodvisitor.VisitCode.java

License:Open Source License

public static void visitCode(Strand strand, ObjectValue oMv) {
    MethodVisitor mv = ASMUtil.getRefArgumentNativeData(oMv);
    mv.visitCode();
}