Example usage for org.objectweb.asm MethodVisitor visitLabel

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

Introduction

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

Prototype

public void visitLabel(final Label label) 

Source Link

Document

Visits a label.

Usage

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();/*w w  w  .  jav a2  s .  com*/
    Label l0 = new Label();
    mv1.visitLabel(l0);
    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 .  java 2  s .  c  o 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 declareConstructor(ClassWriter cw, String className) {
    MethodVisitor mv;
    mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
    mv.visitCode();//from   w ww  .j a v a  2  s .c  o  m
    Label l0 = new Label();
    mv.visitLabel(l0);
    // 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;//  w ww. j a  v a2  s. co  m
    }
    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;//  w  w  w  . j  av  a2s  . c  o m
    }

    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();/*  w w w.jav  a2 s .  co  m*/
    Label l0 = new Label();
    mv.visitLabel(l0);
    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();//w w  w .ja  v  a 2  s.  c  o m
    Label l0 = new Label();
    mv.visitLabel(l0);
    // 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.VisitLabel.java

License:Open Source License

public static void visitLabel(Strand strand, ObjectValue oMv, ObjectValue oLabel) {
    MethodVisitor mv = ASMUtil.getRefArgumentNativeData(oMv);
    Label label = ASMUtil.getRefArgumentNativeData(oLabel);
    mv.visitLabel(label);
}

From source file:org.batoo.jpa.core.impl.instance.Enhancer.java

License:Open Source License

private static void createContainerConstructor(final String enhancingClassName, final String enhancedClassName,
        final String descEnhancer, final ClassWriter cw) {
    final MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, Enhancer.CONSTRUCTOR_INIT,
            Enhancer.makeDescription(Void.TYPE, Class.class, SessionImpl.class, Object.class, Boolean.TYPE),
            null, null);//from ww  w  .j a v a2s .  c o m
    mv.visitCode();

    final Label l0 = new Label();
    mv.visitLabel(l0);
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, enhancingClassName, Enhancer.CONSTRUCTOR_INIT,
            Enhancer.makeDescription(Void.TYPE));

    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitVarInsn(Opcodes.ALOAD, 1);
    mv.visitFieldInsn(Opcodes.PUTFIELD, enhancedClassName, Enhancer.FIELD_ENHANCED_TYPE,
            Enhancer.DESCRIPTOR_CLASS);

    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitVarInsn(Opcodes.ALOAD, 2);
    mv.visitFieldInsn(Opcodes.PUTFIELD, enhancedClassName, Enhancer.FIELD_ENHANCED_SESSION,
            Enhancer.DESCRIPTOR_SESSION);

    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitVarInsn(Opcodes.ALOAD, 3);
    mv.visitFieldInsn(Opcodes.PUTFIELD, enhancedClassName, Enhancer.FIELD_ENHANCED_ID,
            Enhancer.DESCRIPTOR_OBJECT);

    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitVarInsn(Opcodes.ILOAD, 4);
    mv.visitFieldInsn(Opcodes.PUTFIELD, enhancedClassName, Enhancer.FIELD_ENHANCED_INITIALIZED,
            Enhancer.DESCRIPTOR_BOOLEAN);

    mv.visitInsn(Opcodes.RETURN);

    final Label l1 = new Label();
    mv.visitLabel(l1);
    mv.visitLocalVariable(Enhancer.THIS, descEnhancer, null, l0, l1, 0);
    mv.visitLocalVariable("type", Enhancer.DESCRIPTOR_CLASS, null, l0, l1, 1);
    mv.visitLocalVariable("session", Enhancer.DESCRIPTOR_SESSION, null, l0, l1, 2);
    mv.visitLocalVariable("id", Enhancer.DESCRIPTOR_OBJECT, null, l0, l1, 3);
    mv.visitLocalVariable("initialized", Enhancer.DESCRIPTOR_BOOLEAN, null, l0, l1, 4);
    mv.visitMaxs(0, 0);

    mv.visitEnd();
}

From source file:org.batoo.jpa.core.impl.instance.Enhancer.java

License:Open Source License

private static void createMethodCheck(final String enhancedClassName, final String descEnhancer,
        final ClassWriter cw) {
    final MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PRIVATE, Enhancer.METHOD_ENHANCED_CHECK,
            Enhancer.makeDescription(Void.TYPE), null, null);
    mv.visitCode();/*from   w ww.  j  av  a  2 s.c  om*/

    final Label lCheckInternal = new Label();
    final Label lCheckInitialized = new Label();
    final Label lReturn = new Label();
    final Label lFind = new Label();
    final Label lInitialized = new Label();
    final Label lChanged = new Label();
    final Label lOut = new Label();

    // if (!this.__enhanced__$$__internal) { return }
    mv.visitLabel(lCheckInternal);
    mv.visitFrame(Opcodes.F_NEW, 1, new Object[] { enhancedClassName }, 0, new Object[] {});
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitFieldInsn(Opcodes.GETFIELD, enhancedClassName, Enhancer.FIELD_ENHANCED_INTERNAL,
            Enhancer.DESCRIPTOR_BOOLEAN);
    mv.visitJumpInsn(Opcodes.IFEQ, lCheckInitialized);
    mv.visitInsn(Opcodes.RETURN);

    // if (!this.__enhanced__$$__initialized) {
    mv.visitLabel(lCheckInitialized);
    mv.visitFrame(Opcodes.F_NEW, 1, new Object[] { enhancedClassName }, 0, new Object[] {});
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitFieldInsn(Opcodes.GETFIELD, enhancedClassName, Enhancer.FIELD_ENHANCED_INITIALIZED,
            Enhancer.DESCRIPTOR_BOOLEAN);
    mv.visitJumpInsn(Opcodes.IFNE, lChanged);

    //     if (this.__enhanced_$$__session == null)
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitFieldInsn(Opcodes.GETFIELD, enhancedClassName, Enhancer.FIELD_ENHANCED_SESSION,
            Enhancer.DESCRIPTOR_SESSION);
    mv.visitJumpInsn(Opcodes.IFNONNULL, lFind);

    //         throw new PersistenceException("No session to initialize the instance");
    mv.visitTypeInsn(Opcodes.NEW, Enhancer.INTERNAL_PERSISTENCE_EXCEPTION);
    mv.visitInsn(Opcodes.DUP);
    mv.visitLdcInsn("No session to initialize the instance");
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, Enhancer.INTERNAL_PERSISTENCE_EXCEPTION,
            Enhancer.CONSTRUCTOR_INIT, Enhancer.makeDescription(Void.TYPE, String.class));
    mv.visitInsn(Opcodes.ATHROW);

    //       this.__enhanced_$$__session.getEntityManager().find(this.__enhanced_$$__type, this.__enhanced__$$__id);
    mv.visitLabel(lFind);
    mv.visitFrame(Opcodes.F_NEW, 1, new Object[] { enhancedClassName }, 0, new Object[] {});
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitFieldInsn(Opcodes.GETFIELD, enhancedClassName, Enhancer.FIELD_ENHANCED_SESSION,
            Enhancer.DESCRIPTOR_SESSION);
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Enhancer.INTERNAL_SESSION, Enhancer.METHOD_GET_ENTITY_MANAGER,
            Enhancer.makeDescription(EntityManagerImpl.class));
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitFieldInsn(Opcodes.GETFIELD, enhancedClassName, Enhancer.FIELD_ENHANCED_TYPE,
            Enhancer.DESCRIPTOR_CLASS);
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitFieldInsn(Opcodes.GETFIELD, enhancedClassName, Enhancer.FIELD_ENHANCED_ID,
            Enhancer.DESCRIPTOR_OBJECT);
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Enhancer.INTERNAL_ENTITY_MANAGER, Enhancer.METHOD_FIND,
            Enhancer.makeDescription(Object.class, Class.class, Object.class));
    mv.visitInsn(Opcodes.POP);

    //   this.__enhanced__$$__initialized = true;
    mv.visitLabel(lInitialized);
    mv.visitFrame(Opcodes.F_NEW, 1, new Object[] { enhancedClassName }, 0, new Object[] {});
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitInsn(Opcodes.ICONST_1);
    mv.visitFieldInsn(Opcodes.PUTFIELD, enhancedClassName, Enhancer.FIELD_ENHANCED_INITIALIZED,
            Enhancer.DESCRIPTOR_BOOLEAN);

    // if (this.__enhanced_$$__session != null)
    mv.visitLabel(lChanged);
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitFieldInsn(Opcodes.GETFIELD, enhancedClassName, Enhancer.FIELD_ENHANCED_SESSION,
            Enhancer.DESCRIPTOR_SESSION);
    mv.visitJumpInsn(Opcodes.IFNULL, lReturn);

    //     this.__enhanced__$$__managedInstance.changed();
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitFieldInsn(Opcodes.GETFIELD, enhancedClassName, Enhancer.FIELD_ENHANCED_MANAGED_INSTANCE,
            Enhancer.DESCRIPTOR_MANAGED_INSTANCE);
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Enhancer.INTERNAL_MANAGED_INSTANCE, Enhancer.METHOD_CHANGED,
            Enhancer.makeDescription(Void.TYPE));

    // return;
    mv.visitLabel(lReturn);
    mv.visitFrame(Opcodes.F_NEW, 1, new Object[] { enhancedClassName }, 0, new Object[] {});
    mv.visitInsn(Opcodes.RETURN);

    mv.visitLabel(lOut);
    mv.visitLocalVariable(Enhancer.THIS, descEnhancer, null, lCheckInternal, lOut, 0);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
}