Example usage for org.objectweb.asm MethodVisitor visitLineNumber

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

Introduction

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

Prototype

public void visitLineNumber(final int line, final Label start) 

Source Link

Document

Visits a line number declaration.

Usage

From source file:com.weibo.lodil.mmap.impl.GenerateHugeArrays.java

License:Apache License

private static void appendToStringHashCodeEqualsCopyOf(final TypeModel tm, final ClassWriter cw,
        final String name2, final boolean hasListField) {
    final String name = tm.bcType();
    MethodVisitor mv;
    {// w  w  w  . j av a2 s  . c  o m
        mv = cw.visitMethod(ACC_PUBLIC, "toString", "()Ljava/lang/String;", null, null);
        mv.visitCode();
        final Label l0 = new Label();
        mv.visitLabel(l0);
        mv.visitLineNumber(176, l0);
        mv.visitTypeInsn(NEW, "java/lang/StringBuilder");
        mv.visitInsn(DUP);
        mv.visitMethodInsn(INVOKESPECIAL, "java/lang/StringBuilder", "<init>", "()V");
        mv.visitLdcInsn(tm.type().getSimpleName() + "{");
        mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append",
                "(Ljava/lang/String;)Ljava/lang/StringBuilder;");

        String sep = "";
        for (final FieldModel fm : tm.fields()) {
            final boolean text = CharSequence.class.isAssignableFrom(tm.type());

            mv.visitLdcInsn(sep + fm.fieldName() + "=" + (text ? "'" : ""));
            mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append",
                    "(Ljava/lang/String;)Ljava/lang/StringBuilder;");
            mv.visitVarInsn(ALOAD, 0);
            mv.visitMethodInsn(INVOKEVIRTUAL, name2, "get" + fm.titleFieldName(), "()" + fm.bcLFieldType());
            String appendType = "Ljava/lang/Object;";
            final Class fmType = fm.type();
            if (fmType.isPrimitive()) {
                if ((fmType == byte.class) || (fmType == short.class)) {
                    appendType = "I";
                } else {
                    appendType = fm.bcLFieldType();
                }
            }
            mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append",
                    "(" + appendType + ")Ljava/lang/StringBuilder;");
            sep = text ? "', " : ", ";
        }
        if (sep.startsWith("'")) {
            mv.visitIntInsn(BIPUSH, 39);
            mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append",
                    "(C)Ljava/lang/StringBuilder;");
        }
        mv.visitIntInsn(BIPUSH, 125);
        mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(C)Ljava/lang/StringBuilder;");
        mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "toString", "()Ljava/lang/String;");
        mv.visitInsn(ARETURN);
        final Label l1 = new Label();
        mv.visitLabel(l1);
        mv.visitLocalVariable("this", "L" + name2 + ";", null, l0, l1, 0);
        mv.visitMaxs(3, 1);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PUBLIC, "equals", "(Ljava/lang/Object;)Z", null, null);
        mv.visitCode();
        final Label l0 = new Label();
        mv.visitLabel(l0);
        mv.visitLineNumber(194, l0);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitVarInsn(ALOAD, 1);
        final Label l1 = new Label();
        mv.visitJumpInsn(IF_ACMPNE, l1);
        mv.visitInsn(ICONST_1);
        mv.visitInsn(IRETURN);
        mv.visitLabel(l1);
        mv.visitLineNumber(195, l1);
        mv.visitVarInsn(ALOAD, 1);
        final Label l2 = new Label();
        mv.visitJumpInsn(IFNULL, l2);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Object", "getClass", "()Ljava/lang/Class;");
        mv.visitVarInsn(ALOAD, 1);
        mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Object", "getClass", "()Ljava/lang/Class;");
        final Label l3 = new Label();
        mv.visitJumpInsn(IF_ACMPEQ, l3);
        mv.visitLabel(l2);
        mv.visitInsn(ICONST_0);
        mv.visitInsn(IRETURN);
        mv.visitLabel(l3);
        mv.visitLineNumber(197, l3);
        mv.visitVarInsn(ALOAD, 1);
        mv.visitTypeInsn(CHECKCAST, name2);
        mv.visitVarInsn(ASTORE, 2);
        final Label l4 = new Label();
        mv.visitLabel(l4);
        mv.visitLineNumber(199, l4);
        final FieldModel[] fieldModels = tm.fields().clone();
        Arrays.sort(fieldModels, new Comparator<FieldModel>() {
            // reverse sort the preferences to optimise the
            public int compare(final FieldModel o1, final FieldModel o2) {
                return o2.equalsPreference() - o1.equalsPreference();
            }
        });
        for (final FieldModel fm : fieldModels) {
            // System.out.println(fm.fieldName());
            mv.visitVarInsn(ALOAD, 0);
            mv.visitMethodInsn(INVOKEVIRTUAL, name2, "get" + fm.titleFieldName(), "()" + fm.bcLFieldType());
            mv.visitVarInsn(ALOAD, 2);
            mv.visitMethodInsn(INVOKEVIRTUAL, name2, "get" + fm.titleFieldName(), "()" + fm.bcLFieldType());
            final Label l5 = new Label();
            if (fm.isCallsNotEquals()) {
                mv.visitMethodInsn(INVOKESTATIC, fm.bcLModelType(), "notEquals",
                        "(" + fm.bcLSetType() + fm.bcLSetType() + ")Z");
                mv.visitJumpInsn(IFEQ, l5);
            } else {
                mv.visitJumpInsn(IF_ICMPEQ, l5);
            }
            mv.visitInsn(ICONST_0);
            mv.visitInsn(IRETURN);
            mv.visitLabel(l5);
        }

        mv.visitInsn(ICONST_1);
        mv.visitInsn(IRETURN);
        final Label l17 = new Label();
        mv.visitLabel(l17);
        mv.visitLocalVariable("this", "L" + name2 + ";", null, l0, l17, 0);
        mv.visitLocalVariable("o", "Ljava/lang/Object;", null, l0, l17, 1);
        mv.visitLocalVariable("that", "L" + name2 + ";", null, l4, l17, 2);
        mv.visitMaxs(4, 3);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PUBLIC, "hashCode", "()I", null, null);
        mv.visitCode();
        final Label l0 = new Label();
        mv.visitLabel(l0);
        int count = 0;
        for (final FieldModel fm : tm.fields()) {
            // if (count > 5) break;
            // System.out.println(fm.fieldName());
            if (count > 0) {
                mv.visitIntInsn(BIPUSH, 31);
                mv.visitInsn(IMUL);
            }
            mv.visitVarInsn(ALOAD, 0);
            mv.visitMethodInsn(INVOKEVIRTUAL, name2, "get" + fm.titleFieldName(), "()" + fm.bcLFieldType());
            if (fm.isCallsHashCode()) {
                mv.visitMethodInsn(INVOKESTATIC, fm.bcLModelType(), "hashCode", "(" + fm.bcLSetType() + ")I");
            }

            if (count > 0) {
                mv.visitInsn(IADD);
            }
            count++;
        }
        mv.visitInsn(IRETURN);
        final Label l3 = new Label();
        mv.visitLabel(l3);
        mv.visitLocalVariable("this", "L" + name2 + ";", null, l0, l3, 0);
        mv.visitMaxs(6, 1);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PUBLIC, "copyOf", "(L" + name + ";)V", null, null);
        mv.visitCode();
        final Label l0 = new Label();
        mv.visitLabel(l0);
        mv.visitLineNumber(240, l0);

        boolean copySimpleValues = false;
        for (final FieldModel fm : tm.fields()) {
            if (!fm.copySimpleValue() || !hasListField) {
                // if (true) {
                mv.visitVarInsn(ALOAD, 0);
                mv.visitVarInsn(ALOAD, 1);
                mv.visitMethodInsn(INVOKEINTERFACE, name, "get" + fm.titleFieldName(),
                        "()" + fm.bcLFieldType());
                mv.visitMethodInsn(INVOKEVIRTUAL, name2, "set" + fm.titleFieldName(),
                        "(" + fm.bcLFieldType() + ")V");
            } else {
                copySimpleValues = true;
            }
        }
        final Label l4 = new Label();
        final Label l6 = new Label();
        if (copySimpleValues) {
            final Label l3 = new Label();
            mv.visitLabel(l3);
            mv.visitLineNumber(243, l3);
            mv.visitVarInsn(ALOAD, 1);
            mv.visitTypeInsn(INSTANCEOF, name2);
            mv.visitJumpInsn(IFEQ, l4);
            final Label l5 = new Label();
            mv.visitLabel(l5);
            mv.visitLineNumber(238, l5);
            mv.visitVarInsn(ALOAD, 1);
            mv.visitTypeInsn(CHECKCAST, name2);
            mv.visitVarInsn(ASTORE, 2);
            mv.visitLabel(l6);
            mv.visitLineNumber(239, l6);
            mv.visitVarInsn(ALOAD, 2);
            mv.visitFieldInsn(GETFIELD, name2, "container", "L" + collections + "impl/AbstractHugeContainer;");
            mv.visitVarInsn(ALOAD, 0);
            mv.visitFieldInsn(GETFIELD, name2, "container", "L" + collections + "impl/AbstractHugeContainer;");
            mv.visitJumpInsn(IF_ACMPNE, l4);
            for (final FieldModel fm : tm.fields()) {
                if (fm.copySimpleValue()) {
                    mv.visitVarInsn(ALOAD, 0);
                    mv.visitFieldInsn(GETFIELD, name2, "allocation", "L" + name + "Allocation;");
                    mv.visitFieldInsn(GETFIELD, name + "Allocation", "m_" + fm.fieldName(), fm.bcLStoreType());
                    mv.visitVarInsn(ALOAD, 0);
                    mv.visitFieldInsn(GETFIELD, name2, "offset", "I");
                    mv.visitVarInsn(ALOAD, 2);
                    mv.visitFieldInsn(GETFIELD, name2, "allocation", "L" + name + "Allocation;");
                    mv.visitFieldInsn(GETFIELD, name + "Allocation", "m_" + fm.fieldName(), fm.bcLStoreType());
                    mv.visitVarInsn(ALOAD, 2);
                    mv.visitFieldInsn(GETFIELD, name2, "offset", "I");
                    mv.visitMethodInsn(INVOKEVIRTUAL, fm.bcStoreType(), "get", "(I)" + fm.bcLStoredType());
                    mv.visitMethodInsn(INVOKEVIRTUAL, fm.bcStoreType(), "put",
                            "(I" + fm.bcLStoredType() + ")" + fm.bcLStoreType());
                    mv.visitInsn(POP);
                }
            }
        }
        final Label l16 = new Label();
        mv.visitLabel(l16);
        mv.visitLineNumber(238, l16);
        final Label l17 = new Label();
        mv.visitJumpInsn(GOTO, l17);
        mv.visitLabel(l4);
        mv.visitLineNumber(241, l4);
        for (final FieldModel fm : tm.fields()) {
            if (fm.copySimpleValue()) {
                mv.visitVarInsn(ALOAD, 0);
                mv.visitVarInsn(ALOAD, 1);
                mv.visitMethodInsn(INVOKEINTERFACE, name, "get" + fm.titleFieldName(),
                        "()" + fm.bcLFieldType());
                mv.visitMethodInsn(INVOKEVIRTUAL, name2, "set" + fm.titleFieldName(),
                        "(" + fm.bcLFieldType() + ")V");
            }
        }
        mv.visitLabel(l17);
        mv.visitLineNumber(251, l17);
        mv.visitInsn(RETURN);
        final Label l27 = new Label();
        mv.visitLabel(l27);
        mv.visitLocalVariable("this", "L" + name2 + ";", null, l0, l27, 0);
        mv.visitLocalVariable("t", "L" + name + ";", null, l0, l27, 1);
        if (copySimpleValues) {
            mv.visitLocalVariable("mte", "L" + name + "Element;", null, l6, l4, 2);
        }
        mv.visitMaxs(4, 3);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PUBLIC + ACC_BRIDGE + ACC_SYNTHETIC, "copyOf", "(Ljava/lang/Object;)V", null,
                null);
        mv.visitCode();
        final Label l0 = new Label();
        mv.visitLabel(l0);
        mv.visitLineNumber(275, l0);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitVarInsn(ALOAD, 1);
        mv.visitTypeInsn(CHECKCAST, name);
        mv.visitMethodInsn(INVOKEVIRTUAL, name2, "copyOf", "(L" + name + ";)V");
        mv.visitInsn(RETURN);
        final Label l1 = new Label();
        mv.visitLabel(l1);
        mv.visitLocalVariable("this", "L" + name2 + ";", null, l0, l1, 0);
        mv.visitLocalVariable("x0", "Ljava/lang/Object;", null, l0, l1, 1);
        mv.visitMaxs(2, 2);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PUBLIC, "writeExternal", "(Ljava/io/ObjectOutput;)V", null,
                new String[] { "java/io/IOException" });
        mv.visitCode();
        final Label l0 = new Label();
        mv.visitLabel(l0);
        mv.visitLineNumber(185, l0);
        for (final FieldModel fm : tm.fields()) {
            mv.visitVarInsn(ALOAD, 1);
            if (fm.bcLSetType().equals(fm.bcLFieldType())) {
                mv.visitVarInsn(ALOAD, 0);
                mv.visitMethodInsn(INVOKEVIRTUAL, name2, "get" + fm.titleFieldName(), "()" + fm.bcLFieldType());
                mv.visitMethodInsn(INVOKESTATIC, fm.bcModelType(), "write",
                        "(Ljava/io/ObjectOutput;" + fm.bcLSetType() + ")V");
            } else {
                mv.visitLdcInsn(Type.getType(fm.bcLFieldType()));
                mv.visitVarInsn(ALOAD, 0);
                mv.visitMethodInsn(INVOKEVIRTUAL, name2, "get" + fm.titleFieldName(), "()" + fm.bcLFieldType());
                mv.visitMethodInsn(INVOKESTATIC, fm.bcModelType(), "write",
                        "(Ljava/io/ObjectOutput;Ljava/lang/Class;" + fm.bcLSetType() + ")V");
            }
        }
        final Label l13 = new Label();
        mv.visitLabel(l13);
        mv.visitLineNumber(288, l13);
        mv.visitInsn(RETURN);
        final Label l14 = new Label();
        mv.visitLabel(l14);
        mv.visitLocalVariable("this", "L" + name2 + ';', null, l0, l14, 0);
        mv.visitLocalVariable("out", "Ljava/io/ObjectOutput;", null, l0, l14, 1);
        mv.visitMaxs(4, 2);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PUBLIC, "readExternal", "(Ljava/io/ObjectInput;)V", null,
                new String[] { "java/io/IOException", "java/lang/ClassNotFoundException" });
        mv.visitCode();
        final Label l0 = new Label();
        mv.visitLabel(l0);
        mv.visitLineNumber(287, l0);
        for (final FieldModel fm : tm.fields()) {
            mv.visitVarInsn(ALOAD, 0);
            mv.visitVarInsn(ALOAD, 1);
            if (fm.bcLSetType().equals(fm.bcLFieldType())) {
                mv.visitMethodInsn(INVOKESTATIC, fm.bcModelType(), "read",
                        "(Ljava/io/ObjectInput;)" + fm.bcLSetType());
            } else {
                mv.visitLdcInsn(Type.getType(fm.bcLFieldType()));
                mv.visitMethodInsn(INVOKESTATIC, fm.bcModelType(), "read",
                        "(Ljava/io/ObjectInput;Ljava/lang/Class;)" + fm.bcLSetType());
                mv.visitTypeInsn(CHECKCAST, fm.bcFieldType());
            }
            mv.visitMethodInsn(INVOKEVIRTUAL, name2, "set" + fm.titleFieldName(),
                    "(" + fm.bcLFieldType() + ")V");
        }
        final Label l13 = new Label();
        mv.visitLabel(l13);
        mv.visitLineNumber(305, l13);
        mv.visitInsn(RETURN);
        final Label l14 = new Label();
        mv.visitLabel(l14);
        mv.visitLocalVariable("this", "L" + name2 + ";", null, l0, l14, 0);
        mv.visitLocalVariable("in", "Ljava/io/ObjectInput;", null, l0, l14, 1);
        mv.visitMaxs(3, 2);
        mv.visitEnd();
    }
}

From source file:com.weibo.lodil.mmap.impl.GenerateHugeArrays.java

License:Apache License

public static byte[] dumpImpl(final TypeModel tm) {

    final ClassWriter cw = new ClassWriter(0);
    FieldVisitor fv;/*w  w  w .  ja v a  2  s. c om*/
    MethodVisitor mv;

    final String name = tm.bcType();

    cw.visit(V1_5, ACC_PUBLIC + ACC_SUPER, name + "Impl",
            "Ljava/lang/Object;L" + name + ";L" + collections + "api/HugeElement<L" + name
                    + ";>;Ljava/io/Externalizable;",
            "java/lang/Object",
            new String[] { name, collections + "api/HugeElement", "java/io/Externalizable" });

    cw.visitSource(tm.getClass().getSimpleName() + "Impl.java", null);

    for (final FieldModel fm : tm.fields()) {
        fv = cw.visitField(ACC_PRIVATE, "m_" + fm.fieldName(), fm.bcLFieldType(), null, null);
        fv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
        mv.visitCode();
        final Label l0 = new Label();
        mv.visitLabel(l0);
        mv.visitLineNumber(30, l0);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V");
        mv.visitInsn(RETURN);
        final Label l1 = new Label();
        mv.visitLabel(l1);
        mv.visitLocalVariable("this", "L" + name + "Impl;", null, l0, l1, 0);
        mv.visitMaxs(1, 1);
        mv.visitEnd();
    }
    for (final FieldModel fm : tm.fields()) {
        mv = cw.visitMethod(ACC_PUBLIC, "set" + fm.titleFieldName(), "(" + fm.bcLFieldType() + ")V", null,
                null);
        mv.visitCode();
        final Label l0 = new Label();
        mv.visitLabel(l0);
        mv.visitLineNumber(47, l0);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitVarInsn(loadFor(fm.bcType()), 1);
        mv.visitFieldInsn(PUTFIELD, name + "Impl", "m_" + fm.fieldName(), fm.bcLFieldType());
        final Label l1 = new Label();
        mv.visitLabel(l1);
        mv.visitLineNumber(48, l1);
        mv.visitInsn(RETURN);
        final Label l2 = new Label();
        mv.visitLabel(l2);
        mv.visitLocalVariable("this", "L" + name + "Impl;", null, l0, l2, 0);
        mv.visitLocalVariable("b", fm.bcLFieldType(), null, l0, l2, 1);
        mv.visitMaxs(1 + fm.bcFieldSize(), 1 + fm.bcFieldSize());
        mv.visitEnd();

        mv = cw.visitMethod(ACC_PUBLIC, "get" + fm.titleFieldName(), "()" + fm.bcLFieldType(), null, null);
        mv.visitCode();
        final Label l3 = new Label();
        mv.visitLabel(l3);
        mv.visitLineNumber(45, l3);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, name + "Impl", "m_" + fm.fieldName(), fm.bcLFieldType());
        mv.visitInsn(returnFor(fm.bcType()));
        final Label l4 = new Label();
        mv.visitLabel(l4);
        mv.visitLocalVariable("this", "L" + name + "Impl;", null, l3, l4, 0);
        mv.visitMaxs(1 + fm.bcFieldSize(), 1);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PUBLIC, "index", "(J)V", null, null);
        mv.visitCode();
        final Label l0 = new Label();
        mv.visitLabel(l0);
        mv.visitLineNumber(175, l0);
        mv.visitTypeInsn(NEW, "java/lang/UnsupportedOperationException");
        mv.visitInsn(DUP);
        mv.visitMethodInsn(INVOKESPECIAL, "java/lang/UnsupportedOperationException", "<init>", "()V");
        mv.visitInsn(ATHROW);
        final Label l1 = new Label();
        mv.visitLabel(l1);
        mv.visitLocalVariable("this", "L" + name + "Impl;", null, l0, l1, 0);
        mv.visitLocalVariable("n", "J", null, l0, l1, 1);
        mv.visitMaxs(2, 3);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PUBLIC, "index", "()J", null, null);
        mv.visitCode();
        final Label l0 = new Label();
        mv.visitLabel(l0);
        mv.visitLineNumber(180, l0);
        mv.visitInsn(LCONST_0);
        mv.visitInsn(LRETURN);
        final Label l1 = new Label();
        mv.visitLabel(l1);
        mv.visitLocalVariable("this", "L" + name + "Impl;", null, l0, l1, 0);
        mv.visitMaxs(2, 1);
        mv.visitEnd();
    }
    appendToStringHashCodeEqualsCopyOf(tm, cw, name + "Impl", false);

    {
        mv = cw.visitMethod(ACC_PUBLIC, "hugeElementType", "()L" + collections + "api/HugeElementType;", null,
                null);
        mv.visitCode();
        final Label l0 = new Label();
        mv.visitLabel(l0);
        mv.visitLineNumber(236, l0);
        mv.visitFieldInsn(GETSTATIC, collections + "api/HugeElementType", "BeanImpl",
                "L" + collections + "api/HugeElementType;");
        mv.visitInsn(ARETURN);
        final Label l1 = new Label();
        mv.visitLabel(l1);
        mv.visitLocalVariable("this", "L" + name + "Impl;", null, l0, l1, 0);
        mv.visitMaxs(1, 1);
        mv.visitEnd();
    }
    cw.visitEnd();

    final byte[] bytes = cw.toByteArray();
    // ClassReader cr = new ClassReader(bytes);
    // cr.accept(new ASMifierClassVisitor(new PrintWriter(System.out)), 0);

    return bytes;
}

From source file:de.javanarior.vo.generator.ByteCodeGenerator.java

License:Apache License

/**
 * Generate a implementation of a value object.
 *
 * @param valueType/*from   w w  w  .  j  av  a2  s. c  om*/
 *            - value object type
 * @param technicalType
 *            - to which the value object is mapped
 * @param wrapperClass
 *            - abstract wrapper class, correspond to the technical type
 * @return class name and byte code in a container
 */
/* CHECKSTYLE:OFF */
public static ByteCodeContainer generate(Class<?> valueType, Class<? extends Comparable<?>> technicalType,
        @SuppressWarnings("rawtypes") Class<? extends AbstractValue> wrapperClass) {
    /* CHECKSTYLE:ON */

    if (!isInterface(valueType)) {
        throw new IllegalArgumentException("Could not generate implementation for class " + valueType.getName()
                + ". Please provide interface");
    }

    ClassWriter classWriter = new ClassWriter(0);
    MethodVisitor methodVisitor;

    classWriter.visit(Opcodes.V1_7, Opcodes.ACC_PUBLIC + Opcodes.ACC_SUPER, implementationClassName(valueType),
            "L" + parentClassName(wrapperClass) + "<" + addTypeDiscriptor(valueType) + ">;"
                    + addTypeDiscriptor(valueType),
            parentClassName(wrapperClass), implementedInterfaces(valueType));

    methodVisitor = classWriter.visitMethod(Opcodes.ACC_PUBLIC, CONSTRUCTOR, methodDescriptor(technicalType),
            null, null);
    methodVisitor.visitCode();
    Label label0 = new Label();
    methodVisitor.visitLabel(label0);
    /* CHECKSTYLE:OFF */
    methodVisitor.visitLineNumber(8, label0);
    /* CHECKSTYLE:ON */
    methodVisitor.visitVarInsn(Opcodes.ALOAD, 0);
    methodVisitor.visitVarInsn(getILOADOpCode(technicalType), 1);
    methodVisitor.visitMethodInsn(Opcodes.INVOKESPECIAL, parentClassName(wrapperClass), CONSTRUCTOR,
            methodDescriptor(technicalType), false);
    Label label1 = new Label();
    methodVisitor.visitLabel(label1);
    /* CHECKSTYLE:OFF */
    methodVisitor.visitLineNumber(9, label1);
    /* CHECKSTYLE:ON */
    methodVisitor.visitInsn(Opcodes.RETURN);
    Label label2 = new Label();
    methodVisitor.visitLabel(label2);
    methodVisitor.visitLocalVariable("this", addTypeSignature(implementationClassName(valueType)), null, label0,
            label2, 0);
    methodVisitor.visitLocalVariable("value", getType(technicalType), null, label0, label2, 1);
    int stackSize = getStackSize(Type.getDescriptor(technicalType));
    methodVisitor.visitMaxs(stackSize, stackSize);
    methodVisitor.visitEnd();
    classWriter.visitEnd();

    return new ByteCodeContainer(binaryClassName(valueType), classWriter.toByteArray());
}

From source file:fr.insalyon.citi.golo.compiler.JavaBytecodeUtils.java

License:Apache License

static void visitLine(GoloElement element, MethodVisitor visitor) {
    if (element.hasASTNode()) {
        Label label = new Label();
        visitor.visitLabel(label);/*w w  w . j  av a  2 s .  com*/
        visitor.visitLineNumber(element.getPositionInSourceCode().getLine(), label);
    }
}

From source file:net.sourceforge.cobertura.instrument.pass3.TestUnitCodeProvider.java

License:GNU General Public License

/**
 * Generates:// ww  w.  j  ava2s. c om
 *   public static void __cobertura_init()
 *   {
 *     if (__cobertura_counters == null)
 *       {
 *         __cobertura_counters = new TestUnitInformationHolder[class.length];
 *       }
 *       TouchCollector.registerClass("mypackage/HelloWorld");
 *   }
 */
public void generateCINITmethod(MethodVisitor mv, String className, int counters_cnt) {
    mv.visitFieldInsn(Opcodes.GETSTATIC, className, COBERTURA_COUNTERS_FIELD_NAME,
            COBERTURA_COUNTERS_FIELD_TYPE);
    Label l1 = new Label();
    mv.visitJumpInsn(Opcodes.IFNONNULL, l1);
    mv.visitLdcInsn(counters_cnt);
    mv.visitTypeInsn(Opcodes.ANEWARRAY, Type.getInternalName(TestUnitInformationHolder.class));
    mv.visitFieldInsn(Opcodes.PUTSTATIC, className, COBERTURA_COUNTERS_FIELD_NAME,
            COBERTURA_COUNTERS_FIELD_TYPE);

    mv.visitInsn(ICONST_0);
    mv.visitVarInsn(ISTORE, 1);
    Label l11 = new Label();
    mv.visitLabel(l11);
    Label l2 = new Label();
    mv.visitJumpInsn(GOTO, l2);
    Label l3 = new Label();
    mv.visitLabel(l3);
    mv.visitLineNumber(24, l3);
    mv.visitFrame(Opcodes.F_APPEND, 1, new Object[] { Opcodes.INTEGER }, 0, null);
    mv.visitFieldInsn(GETSTATIC, className, COBERTURA_COUNTERS_FIELD_NAME, COBERTURA_COUNTERS_FIELD_TYPE);
    mv.visitVarInsn(ILOAD, 1);
    mv.visitTypeInsn(NEW, "net/sourceforge/cobertura/coveragedata/TestUnitInformationHolder");
    mv.visitInsn(DUP);
    mv.visitMethodInsn(INVOKESPECIAL, "net/sourceforge/cobertura/coveragedata/TestUnitInformationHolder",
            "<init>", "()V");
    mv.visitInsn(AASTORE);
    Label l4 = new Label();
    mv.visitLabel(l4);
    mv.visitLineNumber(23, l4);
    mv.visitIincInsn(1, 1);
    mv.visitLabel(l2);
    mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
    mv.visitVarInsn(ILOAD, 1);
    mv.visitFieldInsn(GETSTATIC, className, COBERTURA_COUNTERS_FIELD_NAME, COBERTURA_COUNTERS_FIELD_TYPE);
    mv.visitInsn(ARRAYLENGTH);
    mv.visitJumpInsn(IF_ICMPLT, l3);

    mv.visitLabel(l1);

    generateRegisterClass(mv, className);
}

From source file:org.adjective.stout.writer.ByteCodeWriter.java

License:Apache License

private void writeMethod(ClassVisitor cv, ClassDescriptor cls, MethodDescriptor method) {
    String[] exceptions = new String[method.getExceptions().length];
    for (int i = 0; i < exceptions.length; i++) {
        exceptions[i] = getInternalName(method.getExceptions()[i]);
    }//from w w  w  .  j av  a  2  s  .c  o  m

    String signature = null; // @TODO
    final MethodVisitor mv = visitMethod(cv, method, exceptions, signature);

    for (AnnotationDescriptor annotation : method.getAnnotations()) {
        AnnotationVisitor av = mv.visitAnnotation(Type.getDescriptor(annotation.getType()),
                annotation.isRuntime());
        processAnnotation(annotation, av);
    }

    Parameter[] parameters = method.getParameters();
    for (int i = 0; i < parameters.length; i++) {
        Parameter parameter = parameters[i];
        AnnotationDescriptor[] annotations = parameter.getAnnotations();
        for (AnnotationDescriptor annotation : annotations) {
            AnnotationVisitor av = mv.visitParameterAnnotation(i, Type.getDescriptor(annotation.getType()),
                    annotation.isRuntime());
            processAnnotation(annotation, av);
        }
    }

    Code body = method.getBody();
    if (method.getModifiers().contains(ElementModifier.ABSTRACT)) {
        if (body != null) {
            throw new IllegalStateException("The abstract method " + method + " cannot have a body");
        }
    } else {
        if (body == null) {
            throw new IllegalStateException(
                    "The method " + method + " is not abstract, but does not have a body");
        }
        mv.visitCode();

        CodeStack stack = new CodeStack(cls, method, mv);
        Block block = stack.pushBlock();
        if (!isStatic(method)) {
            block.declareVariable(new Variable("#this", cls));
        }

        for (Parameter parameter : parameters) {
            block.declareVariable(new Variable(parameter.getName(), parameter.getType()));
        }

        InstructionCollector collector = new AbstractInstructionCollector() {
            private int _line;

            public void add(Instruction instruction, int line) {
                if (line != 0 && line != _line) {
                    _line = line;
                    Label label = new Label();
                    mv.visitLabel(label);
                    mv.visitLineNumber(line, label);
                }
                instruction.accept(mv);
            }
        };
        try {
            body.getInstructions(stack, collector);
        } catch (StoutException e) {
            throw new WriterException("In class " + cls.getPackage() + "." + cls.getName()
                    + ", cannot write method " + method.getName(), e);
        }
        stack.popBlock(block);
        stack.declareVariableInfo();
    }

    mv.visitMaxs(0, 0);
    mv.visitEnd();
}

From source file:org.apache.commons.jci.classes.ExtendedDump.java

License:Apache License

public static byte[] dump() throws Exception {

    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
    MethodVisitor mv;

    cw.visit(V1_4, ACC_PUBLIC + ACC_SUPER, "jci/Extended", null, "jci/Simple", null);

    cw.visitSource("Extended.java", null);

    {//from   www. j  av  a 2s  .c o  m
        mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
        mv.visitCode();
        Label l0 = new Label();
        mv.visitLabel(l0);
        mv.visitLineNumber(3, l0);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKESPECIAL, "jci/Simple", "<init>", "()V");
        mv.visitInsn(RETURN);
        Label l1 = new Label();
        mv.visitLabel(l1);
        mv.visitLocalVariable("this", "Ljci/Extended;", null, l0, l1, 0);
        mv.visitMaxs(1, 1);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PUBLIC, "toString", "()Ljava/lang/String;", null, null);
        mv.visitCode();
        Label l0 = new Label();
        mv.visitLabel(l0);
        mv.visitLineNumber(6, l0);
        mv.visitTypeInsn(NEW, "java/lang/StringBuffer");
        mv.visitInsn(DUP);
        mv.visitLdcInsn("Extended:");
        mv.visitMethodInsn(INVOKESPECIAL, "java/lang/StringBuffer", "<init>", "(Ljava/lang/String;)V");
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKESPECIAL, "jci/Simple", "toString", "()Ljava/lang/String;");
        mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuffer", "append",
                "(Ljava/lang/String;)Ljava/lang/StringBuffer;");
        mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuffer", "toString", "()Ljava/lang/String;");
        mv.visitInsn(ARETURN);
        Label l1 = new Label();
        mv.visitLabel(l1);
        mv.visitLocalVariable("this", "Ljci/Extended;", null, l0, l1, 0);
        mv.visitMaxs(3, 1);
        mv.visitEnd();
    }
    cw.visitEnd();

    return cw.toByteArray();
}

From source file:org.apache.commons.jci.classes.SimpleDump.java

License:Apache License

public static byte[] dump(final String to) throws Exception {

    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
    MethodVisitor mv;

    cw.visit(V1_4, ACC_PUBLIC + ACC_SUPER, "jci/Simple", null, "java/lang/Object", null);

    cw.visitSource("Simple.java", null);

    {/*from  w  w w . j av  a  2 s  .com*/
        mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
        mv.visitCode();
        Label l0 = new Label();
        mv.visitLabel(l0);
        mv.visitLineNumber(3, 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", "Ljci/Simple;", null, l0, l1, 0);
        mv.visitMaxs(1, 1);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PUBLIC, "toString", "()Ljava/lang/String;", null, null);
        mv.visitCode();
        Label l0 = new Label();
        mv.visitLabel(l0);
        mv.visitLineNumber(6, l0);
        mv.visitLdcInsn(to);
        mv.visitInsn(ARETURN);
        Label l1 = new Label();
        mv.visitLabel(l1);
        mv.visitLocalVariable("this", "Ljci/Simple;", null, l0, l1, 0);
        mv.visitMaxs(1, 1);
        mv.visitEnd();
    }
    cw.visitEnd();

    return cw.toByteArray();
}

From source file:org.apache.cxf.binding.corba.utils.CorbaAnyHelper.java

License:Apache License

private static void addReadOverride(ClassWriter cw) {
    MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "read_value",
            "(Lorg/omg/CORBA/portable/InputStream;Lorg/omg/CORBA/TypeCode;)V", null, null);
    mv.visitCode();//from   w  w  w . j  a va2  s .co  m
    Label l0 = new Label();
    mv.visitLabel(l0);
    mv.visitLineNumber(54, l0);
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitFieldInsn(Opcodes.GETFIELD, "org/apache/cxf/binding/corba/utils/FixedAnyImpl", "obj",
            "Lorg/omg/CORBA/portable/Streamable;");
    Label l1 = new Label();
    mv.visitJumpInsn(Opcodes.IFNULL, l1);
    Label l2 = new Label();
    mv.visitLabel(l2);
    mv.visitLineNumber(55, l2);
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitFieldInsn(Opcodes.GETFIELD, "org/apache/cxf/binding/corba/utils/FixedAnyImpl", "obj",
            "Lorg/omg/CORBA/portable/Streamable;");
    mv.visitVarInsn(Opcodes.ALOAD, 1);
    mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "org/omg/CORBA/portable/Streamable", "_read",
            "(Lorg/omg/CORBA/portable/InputStream;)V");
    Label l3 = new Label();
    mv.visitJumpInsn(Opcodes.GOTO, l3);
    mv.visitLabel(l1);
    mv.visitLineNumber(57, l1);
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitVarInsn(Opcodes.ALOAD, 1);
    mv.visitVarInsn(Opcodes.ALOAD, 2);
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "com/sun/corba/se/impl/corba/AnyImpl", "read_value",
            "(Lorg/omg/CORBA/portable/InputStream;Lorg/omg/CORBA/TypeCode;)V");
    mv.visitLabel(l3);
    mv.visitLineNumber(59, l3);
    mv.visitInsn(Opcodes.RETURN);
    Label l4 = new Label();
    mv.visitLabel(l4);
    mv.visitLocalVariable("this", "Lorg/apache/cxf/binding/corba/utils/FixedAnyImpl;", null, l0, l4, 0);
    mv.visitLocalVariable("is", "Lorg/omg/CORBA/portable/InputStream;", null, l0, l4, 1);
    mv.visitLocalVariable("t", "Lorg/omg/CORBA/TypeCode;", null, l0, l4, 2);
    mv.visitMaxs(3, 3);
    mv.visitEnd();
}

From source file:org.apache.cxf.binding.corba.utils.CorbaAnyHelper.java

License:Apache License

private static void addWriteOverride(ClassWriter cw) {
    MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "write_value",
            "(Lorg/omg/CORBA/portable/OutputStream;)V", null, null);
    mv.visitCode();//ww  w .j  a  va 2s .  co m
    Label l0 = new Label();
    mv.visitLabel(l0);
    mv.visitLineNumber(61, l0);
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitFieldInsn(Opcodes.GETFIELD, "org/apache/cxf/binding/corba/utils/FixedAnyImpl", "obj",
            "Lorg/omg/CORBA/portable/Streamable;");
    Label l1 = new Label();
    mv.visitJumpInsn(Opcodes.IFNULL, l1);
    Label l2 = new Label();
    mv.visitLabel(l2);
    mv.visitLineNumber(62, l2);
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitFieldInsn(Opcodes.GETFIELD, "org/apache/cxf/binding/corba/utils/FixedAnyImpl", "obj",
            "Lorg/omg/CORBA/portable/Streamable;");
    mv.visitVarInsn(Opcodes.ALOAD, 1);
    mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "org/omg/CORBA/portable/Streamable", "_write",
            "(Lorg/omg/CORBA/portable/OutputStream;)V");
    Label l3 = new Label();
    mv.visitJumpInsn(Opcodes.GOTO, l3);
    mv.visitLabel(l1);
    mv.visitLineNumber(64, l1);
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitVarInsn(Opcodes.ALOAD, 1);
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "com/sun/corba/se/impl/corba/AnyImpl", "write_value",
            "(Lorg/omg/CORBA/portable/OutputStream;)V");
    mv.visitLabel(l3);
    mv.visitLineNumber(66, l3);
    mv.visitInsn(Opcodes.RETURN);
    Label l4 = new Label();
    mv.visitLabel(l4);
    mv.visitLocalVariable("this", "Lorg/apache/cxf/binding/corba/utils/FixedAnyImpl;", null, l0, l4, 0);
    mv.visitLocalVariable("os", "Lorg/omg/CORBA/portable/OutputStream;", null, l0, l4, 1);
    mv.visitMaxs(2, 2);
    mv.visitEnd();

}