Example usage for org.objectweb.asm MethodVisitor visitInsn

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

Introduction

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

Prototype

public void visitInsn(final int opcode) 

Source Link

Document

Visits a zero operand instruction.

Usage

From source file:boilerplate.processor.adapters.EqualsAdapter.java

License:Open Source License

private void addEqualsMethod(List<FieldInfo> fields) {
    // CODE: public boolean equals(Object obj) {
    MethodVisitor mv = cv.visitMethod(ACC_PUBLIC, "equals", "(Ljava/lang/Object;)Z", null, null);
    mv.visitCode();//ww w  . ja v  a  2s .  co  m

    // CODE: if (this == obj) return true;
    mv.visitVarInsn(ALOAD, 0);
    mv.visitVarInsn(ALOAD, 1);
    Label l0 = new Label();
    mv.visitJumpInsn(IF_ACMPNE, l0);
    mv.visitInsn(ICONST_1);
    mv.visitInsn(IRETURN);
    mv.visitLabel(l0);
    mv.visitFrame(F_SAME, 0, null, 0, null);

    // CODE: if (!(obj instanceof Foo)) return false;
    mv.visitVarInsn(ALOAD, 1);
    mv.visitTypeInsn(INSTANCEOF, typeInfo.type);
    Label l1 = new Label();
    mv.visitJumpInsn(IFNE, l1);
    mv.visitInsn(ICONST_0);
    mv.visitInsn(IRETURN);
    mv.visitLabel(l1);
    mv.visitFrame(F_SAME, 0, null, 0, null);

    // CODE: final Foo other = (Foo) obj;
    mv.visitVarInsn(ALOAD, 1);
    mv.visitTypeInsn(CHECKCAST, typeInfo.type);
    mv.visitVarInsn(ASTORE, 2);

    for (FieldInfo info : fields) {
        addTest(mv, info);
    }

    // CODE: return true; }
    mv.visitInsn(ICONST_1);
    mv.visitInsn(IRETURN);
    mv.visitMaxs(1, 2);
    mv.visitEnd();
}

From source file:boilerplate.processor.adapters.EqualsAdapter.java

License:Open Source License

private void addHashCodeMethod(List<FieldInfo> fields) {
    // CODE: public int hashCode() {
    MethodVisitor mv = cv.visitMethod(ACC_PUBLIC, "hashCode", "()I", null, null);
    mv.visitCode();//from   w w  w.  ja v a  2 s  .c  om

    // CODE: final int prime = 31;
    mv.visitIntInsn(BIPUSH, 31);
    mv.visitVarInsn(ISTORE, 1);
    mv.visitInsn(ICONST_1);

    // CODE: int result = 1;
    mv.visitVarInsn(ISTORE, 2);

    for (FieldInfo fieldInfo : fields) {
        addCalculation(mv, fieldInfo);
    }

    // CODE: return result; }
    mv.visitVarInsn(ILOAD, 2);
    mv.visitInsn(IRETURN);
    mv.visitMaxs(1, 3);
    mv.visitEnd();
}

From source file:boilerplate.processor.adapters.ToStringAdapter.java

License:Open Source License

private void addMethod() {
    List<String> fieldNames = new ArrayList<String>(typeInfo.fields.size());
    Map<String, FieldInfo> fields = new HashMap<String, FieldInfo>(typeInfo.fields.size());
    boolean hasAnnotatedFields = typeInfo.hasAnnotatedFields();
    for (FieldInfo info : typeInfo.fields) {
        if (!(hasAnnotatedFields ^ info.mark == Mark.INCLUDE)) {
            fieldNames.add(info.name);// w ww .j av a2  s  .  c  o  m
            fields.put(info.name, info);
        }
    }
    Collections.sort(fieldNames);

    MethodVisitor mv = cv.visitMethod(ACC_PUBLIC, "toString", "()Ljava/lang/String;", null, null);
    mv.visitCode();
    mv.visitTypeInsn(NEW, "java/lang/StringBuilder");
    mv.visitInsn(DUP);
    mv.visitMethodInsn(INVOKESPECIAL, "java/lang/StringBuilder", "<init>", "()V");
    mv.visitVarInsn(ASTORE, 1);
    for (String name : fieldNames) {
        FieldInfo info = fields.get(name);
        addField(mv, info);
    }
    mv.visitVarInsn(ALOAD, 1);
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "toString", "()Ljava/lang/String;");
    mv.visitInsn(ARETURN);
    mv.visitMaxs(4, 2);
    mv.visitEnd();
}

From source file:br.ufpr.gres.core.operators.insn_components.InsnSubstitution.java

License:Apache License

@Override
public void apply(MethodVisitor mv) {
    mv.visitInsn(this.replacementOpcode);
}

From source file:br.usp.each.saeg.badua.core.internal.instr.ClassInstrumenter.java

License:Open Source License

@Override
public void visitEnd() {
    // not instrument interfaces or
    // classes with probe count equals to zero
    if (interfaceType || classProbeCount == 0) {
        super.visitEnd();
        return;/*w  w  w.  j a  v  a  2s. c om*/
    }

    final FieldVisitor fv = cv.visitField(InstrSupport.DATAFIELD_ACC, InstrSupport.DATAFIELD_NAME,
            InstrSupport.DATAFIELD_DESC, null, null);
    fv.visitEnd();

    final MethodVisitor mv = cv.visitMethod(InstrSupport.DATAMETHOD_ACC, InstrSupport.DATAMETHOD_NAME,
            InstrSupport.DATAMETHOD_DESC, null, null);
    mv.visitCode();

    // Load the value of the static data field:
    mv.visitFieldInsn(Opcodes.GETSTATIC, className, InstrSupport.DATAFIELD_NAME, InstrSupport.DATAFIELD_DESC);
    mv.visitInsn(Opcodes.DUP);

    // Stack[1]: [J
    // Stack[0]: [J

    // Skip initialization when we already have a data array:
    final Label alreadyInitialized = new Label();
    mv.visitJumpInsn(Opcodes.IFNONNULL, alreadyInitialized);

    // Stack[0]: [J

    mv.visitInsn(Opcodes.POP);
    mv.visitLdcInsn(classId);
    InstrSupport.push(mv, classProbeCount);
    mv.visitMethodInsn(Opcodes.INVOKESTATIC, InstrSupport.RUNTIME_OWNER, InstrSupport.RUNTIME_NAME,
            InstrSupport.RUNTIME_DESC, false);

    // Stack[0]: [J

    mv.visitInsn(Opcodes.DUP);

    // Stack[1]: [J
    // Stack[0]: [J

    mv.visitFieldInsn(Opcodes.PUTSTATIC, className, InstrSupport.DATAFIELD_NAME, InstrSupport.DATAFIELD_DESC);

    // Stack[0]: [J

    if (withFrames) {
        mv.visitFrame(Opcodes.F_NEW, 0, new Object[] {}, 1, new Object[] { InstrSupport.DATAFIELD_DESC });
    }
    mv.visitLabel(alreadyInitialized);
    mv.visitInsn(Opcodes.ARETURN);

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

    super.visitEnd();
}

From source file:br.usp.each.saeg.badua.core.internal.instr.InstrSupport.java

License:Open Source License

/**
 * Generates the instruction to push the given int value on the stack.
 * Implementation taken from/*from   w  ww.  jav a  2 s . c  om*/
 * {@link org.objectweb.asm.commons.GeneratorAdapter#push(int)}.
 *
 * @param mv
 *            visitor to emit the instruction
 * @param value
 *            the value to be pushed on the stack.
 */
public static void push(final MethodVisitor mv, final int value) {
    if (value >= -1 && value <= 5) {
        mv.visitInsn(Opcodes.ICONST_0 + value);
    } else if (value >= Byte.MIN_VALUE && value <= Byte.MAX_VALUE) {
        mv.visitIntInsn(Opcodes.BIPUSH, value);
    } else if (value >= Short.MIN_VALUE && value <= Short.MAX_VALUE) {
        mv.visitIntInsn(Opcodes.SIPUSH, value);
    } else {
        mv.visitLdcInsn(Integer.valueOf(value));
    }
}

From source file:br.usp.each.saeg.badua.core.internal.instr.InstrSupport.java

License:Open Source License

public static void swap(final MethodVisitor mv, final Type stackTop, final Type belowTop) {
    if (stackTop.getSize() == 1) {
        if (belowTop.getSize() == 1) {
            // Top = 1, below = 1
            mv.visitInsn(Opcodes.SWAP);
        } else {// w w  w  .  j a va  2s. c o  m
            // Top = 1, below = 2
            mv.visitInsn(Opcodes.DUP_X2);
            mv.visitInsn(Opcodes.POP);
        }
    } else {
        if (belowTop.getSize() == 1) {
            // Top = 2, below = 1
            mv.visitInsn(Opcodes.DUP2_X1);
        } else {
            // Top = 2, below = 2
            mv.visitInsn(Opcodes.DUP2_X2);
        }
        mv.visitInsn(Opcodes.POP2);
    }
}

From source file:br.usp.each.saeg.badua.core.internal.instr.IntegerInitProbe.java

License:Open Source License

@Override
public void accept(final MethodVisitor mv) {
    mv.visitInsn(Opcodes.ICONST_0);
    mv.visitVarInsn(Opcodes.ISTORE, vCovered);
    mv.visitInsn(Opcodes.ICONST_0);//from  w  w  w  .  ja v  a 2  s  . com
    mv.visitVarInsn(Opcodes.ISTORE, vAlive);
    mv.visitInsn(Opcodes.ICONST_M1);
    mv.visitVarInsn(Opcodes.ISTORE, vSleepy);
}

From source file:br.usp.each.saeg.badua.core.internal.instr.IntegerProbe.java

License:Open Source License

@Override
public void accept(final MethodVisitor mv) {

    // update covered
    if (potcov != 0) {
        mv.visitVarInsn(Opcodes.ILOAD, vAlive);

        if (!singlePredecessor && potcovpuse != 0) {
            mv.visitVarInsn(Opcodes.ILOAD, vSleepy);
            mv.visitInsn(Opcodes.IAND);
        }//from w  ww . j a v  a  2  s  .co  m

        InstrSupport.push(mv, (int) potcov);
        mv.visitInsn(Opcodes.IAND);
        mv.visitVarInsn(Opcodes.ILOAD, vCovered);
        mv.visitInsn(Opcodes.IOR);
        mv.visitVarInsn(Opcodes.ISTORE, vCovered);
    }

    // update alive
    if (disabled != 0) {
        InstrSupport.push(mv, ~(int) disabled);
        mv.visitVarInsn(Opcodes.ILOAD, vAlive);
        mv.visitInsn(Opcodes.IAND);
    }
    if (born != 0) {
        if (disabled == 0) {
            mv.visitVarInsn(Opcodes.ILOAD, vAlive);
        }
        InstrSupport.push(mv, (int) born);
        mv.visitInsn(Opcodes.IOR);
    }
    if (disabled != 0 || born != 0) {
        mv.visitVarInsn(Opcodes.ISTORE, vAlive);
    }

    // update sleepy
    InstrSupport.push(mv, ~(int) sleepy);
    mv.visitVarInsn(Opcodes.ISTORE, vSleepy);

}

From source file:br.usp.each.saeg.badua.core.internal.instr.IntegerUpdateProbe.java

License:Open Source License

@Override
public void accept(final MethodVisitor mv) {
    mv.visitVarInsn(Opcodes.ILOAD, vCovered);
    mv.visitInsn(Opcodes.I2L);
    mv.visitMethodInsn(Opcodes.INVOKESTATIC, owner, InstrSupport.DATAMETHOD_NAME, InstrSupport.DATAMETHOD_DESC,
            false);/*from   w  ww .  j a  va  2 s  .c o m*/
    InstrSupport.push(mv, index);
    mv.visitInsn(Opcodes.DUP2_X2);
    mv.visitInsn(Opcodes.LALOAD);
    mv.visitInsn(Opcodes.LOR);
    mv.visitInsn(Opcodes.LASTORE);
}