Example usage for org.objectweb.asm Opcodes GOTO

List of usage examples for org.objectweb.asm Opcodes GOTO

Introduction

In this page you can find the example usage for org.objectweb.asm Opcodes GOTO.

Prototype

int GOTO

To view the source code for org.objectweb.asm Opcodes GOTO.

Click Source Link

Usage

From source file:de.unisb.cs.st.javalanche.mutation.bytecodeMutations.BytecodeTasks.java

License:Open Source License

/**
 * Inserts a mutation. The inserted code is like this:
 * <code>if(System.getProperty(mutationID)){
 *          execute mutated code/* w  w w .java  2 s.com*/
 *       }
 *       else{
 *          execute unmutated code
 *       }
 * 
 * @param mv
 *            MethodVisitor where the code is inserted.
 * @param unMutated
 *            code that should be used when no mutation is applied.
 * @param mutations
 *            code that should be used when one of the mutations is applied.
 */
public static void insertIfElse(MethodVisitor mv, MutationCode unMutated, MutationCode[] mutations) {
    Label endLabel = new Label();
    Label mutationStartLabel = new Label();
    mutationStartLabel.info = new MutationMarker(true);
    mv.visitLabel(mutationStartLabel);
    for (MutationCode mutationCode : mutations) {
        Mutation mutation = mutationCode.getMutation();
        mv.visitLdcInsn(mutation.getMutationVariable());
        // mv.visitLdcInsn(mutation.getMutationType() + "");
        // mv.visitInsn(Opcodes.POP);
        mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/System", "getProperty",
                "(Ljava/lang/String;)Ljava/lang/String;");
        Label l1 = new Label();
        mv.visitJumpInsn(Opcodes.IFNULL, l1);

        Label l2 = new Label();
        mv.visitLabel(l2);
        // insertPrintStatements(mv, "Mutation touched: " +
        // mutation.getId());
        insertMutationTouchedCode(mv, mutation);
        if (!DebugProperties.INSERT_ORIGINAL_INSTEAD_OF_MUTATION) {
            mutationCode.insertCodeBlock(mv);
        } else {
            logger.warn("Debug mode: not inserting mutated statement");
            unMutated.insertCodeBlock(mv);
        }
        mv.visitJumpInsn(Opcodes.GOTO, endLabel);
        mv.visitLabel(l1);
    }
    Label mutationEndLabel = new Label();
    mutationEndLabel.info = new MutationMarker(false);
    mv.visitLabel(mutationEndLabel);
    unMutated.insertCodeBlock(mv);
    mv.visitLabel(endLabel);

}

From source file:de.unisb.cs.st.javalanche.mutation.bytecodeMutations.negateJumps.NegateJumpsMethodAdapter.java

License:Open Source License

@Override
protected void handleMutation(Mutation mutation, final Label label, final int opcode) {

    logger.debug("Applying mutation for line: " + getLineNumber());
    MutationCode unMutated = new MutationCode(null) {
        @Override/*w w  w .  j  a v a2s  .  c om*/
        public void insertCodeBlock(MethodVisitor mv) {
            mv.visitJumpInsn(opcode, label);
        }

    };
    List<Mutation> mutations = QueryManager.getMutations(mutation.getClassName(), mutation.getMethodName(),
            mutation.getLineNumber(), mutation.getMutationForLine(), MutationType.NEGATE_JUMP);
    List<MutationCode> mutationCode = new ArrayList<MutationCode>();
    for (final Mutation m : mutations) {
        if (mutationManager.shouldApplyMutation(m)) {
            MutationCode mutated = new MutationCode(m) {
                @Override
                public void insertCodeBlock(MethodVisitor mv) {
                    int insertOpcode = Integer.parseInt(m.getOperatorAddInfo());
                    if (insertOpcode == POP_ONCE_TRUE) {
                        mv.visitInsn(Opcodes.POP);
                        mv.visitJumpInsn(Opcodes.GOTO, label);
                    } else if (insertOpcode == POP_ONCE_FALSE) {
                        mv.visitInsn(Opcodes.POP);
                    } else if (insertOpcode == POP_TWICE_TRUE) {
                        mv.visitInsn(Opcodes.POP2);
                        mv.visitJumpInsn(Opcodes.GOTO, label);
                    } else if (insertOpcode == POP_TWICE_FALSE) {
                        mv.visitInsn(Opcodes.POP2);
                    } else {
                        mv.visitJumpInsn(insertOpcode, label);
                    }
                }
            };
            mutationCode.add(mutated);
        }
    }
    if (mutationCode.size() > 0) {
        BytecodeTasks.insertIfElse(mv, unMutated, mutationCode.toArray(new MutationCode[0]));
    } else {
        mv.visitJumpInsn(opcode, label);
    }
}

From source file:de.unisb.cs.st.javaslicer.common.classRepresentation.instructions.JumpInstruction.java

License:Open Source License

@Override
public String toString() {
    String condition;/*ww w.  ja  v  a2  s .  c  om*/
    switch (getOpcode()) {
    case Opcodes.IFEQ:
        condition = "IFEQ";
        break;
    case Opcodes.IFNE:
        condition = "IFNE";
        break;
    case Opcodes.IFLT:
        condition = "IFLT";
        break;
    case Opcodes.IFGE:
        condition = "IFGE";
        break;
    case Opcodes.IFGT:
        condition = "IFGT";
        break;
    case Opcodes.IFLE:
        condition = "IFLE";
        break;
    case Opcodes.IF_ICMPEQ:
        condition = "IF_ICMPEQ";
        break;
    case Opcodes.IF_ICMPNE:
        condition = "IF_ICMPNE";
        break;
    case Opcodes.IF_ICMPLT:
        condition = "IF_ICMPLT";
        break;
    case Opcodes.IF_ICMPGE:
        condition = "IF_ICMPGE";
        break;
    case Opcodes.IF_ICMPGT:
        condition = "IF_ICMPGT";
        break;
    case Opcodes.IF_ICMPLE:
        condition = "IF_ICMPLE";
        break;
    case Opcodes.IF_ACMPEQ:
        condition = "IF_ACMPEQ";
        break;
    case Opcodes.IF_ACMPNE:
        condition = "IF_ACMPNE";
        break;
    case Opcodes.GOTO:
        condition = "GOTO";
        break;
    case Opcodes.JSR:
        condition = "JSR";
        break;
    case Opcodes.IFNULL:
        condition = "IFNULL";
        break;
    case Opcodes.IFNONNULL:
        condition = "IFNONNULL";
        break;
    default:
        assert false;
        condition = "--ERROR--";
        break;
    }

    return condition + " " + this.label;
}

From source file:de.unisb.cs.st.javaslicer.common.classRepresentation.instructions.JumpInstruction2.java

License:Open Source License

@Override
public String toString() {
    String condition;//  w ww  .  jav  a  2  s. c om
    switch (getOpcode()) {
    case Opcodes.IFEQ:
        condition = "IFEQ";
        break;
    case Opcodes.IFNE:
        condition = "IFNE";
        break;
    case Opcodes.IFLT:
        condition = "IFLT";
        break;
    case Opcodes.IFGE:
        condition = "IFGE";
        break;
    case Opcodes.IFGT:
        condition = "IFGT";
        break;
    case Opcodes.IFLE:
        condition = "IFLE";
        break;
    case Opcodes.IF_ICMPEQ:
        condition = "IF_ICMPEQ";
        break;
    case Opcodes.IF_ICMPNE:
        condition = "IF_ICMPNE";
        break;
    case Opcodes.IF_ICMPLT:
        condition = "IF_ICMPLT";
        break;
    case Opcodes.IF_ICMPGE:
        condition = "IF_ICMPGE";
        break;
    case Opcodes.IF_ICMPGT:
        condition = "IF_ICMPGT";
        break;
    case Opcodes.IF_ICMPLE:
        condition = "IF_ICMPLE";
        break;
    case Opcodes.IF_ACMPEQ:
        condition = "IF_ACMPEQ";
        break;
    case Opcodes.IF_ACMPNE:
        condition = "IF_ACMPNE";
        break;
    case Opcodes.GOTO:
        condition = "GOTO";
        break;
    case Opcodes.JSR:
        condition = "JSR";
        break;
    case Opcodes.IFNULL:
        condition = "IFNULL";
        break;
    case Opcodes.IFNONNULL:
        condition = "IFNONNULL";
        break;
    default:
        assert false;
        condition = "--ERROR--";
        break;
    }

    return condition + " " + this.target;
}

From source file:de.unisb.cs.st.javaslicer.controlflowanalysis.ControlFlowGraph.java

License:Open Source License

private Instruction followLabelsAndGotos(Instruction instr) {
    Instruction nonLabel = instr;//w  w w.  jav  a2s  . c o m
    while (nonLabel != null) {
        if (nonLabel.getType() == InstructionType.LABEL) {
            nonLabel = nonLabel.getNext();
        } else if (nonLabel.getOpcode() == Opcodes.GOTO) {
            nonLabel = ((JumpInstruction) nonLabel).getLabel().getNext();
        } else
            break;
    }
    return nonLabel;
}

From source file:de.unisb.cs.st.javaslicer.controlflowanalysis.ControlFlowGraph.java

License:Open Source License

protected InstrNode getInstrNode(Instruction instruction, NodeFactory nodeFactory, boolean excludeLabels) {
    int idx = instruction.getIndex() - this.method.getInstructionNumberStart();
    InstrNode node = this.instructionNodes[idx];
    if (node != null || (excludeLabels
            && (instruction.getType() == InstructionType.LABEL || instruction.getOpcode() == Opcodes.GOTO)))
        return node;

    InstrNode newNode = nodeFactory.createNode(this, instruction);
    this.instructionNodes[idx] = newNode;
    for (Instruction succ : getSuccessors(instruction)) {
        Instruction nonLabel = excludeLabels ? followLabelsAndGotos(succ) : succ;
        if (nonLabel == null)
            continue;
        InstrNode succNode = getInstrNode(nonLabel, nodeFactory, excludeLabels);
        newNode.addSuccessor(succNode);//w w w .  ja  va 2  s.  co  m
        succNode.addPredecessor(newNode);
    }
    return newNode;
}

From source file:de.unisb.cs.st.javaslicer.controlflowanalysis.ControlFlowGraph.java

License:Open Source License

private static Collection<Instruction> getSuccessors(Instruction instruction) {
    int opcode = instruction.getOpcode();
    Instruction nextInstruction = instruction.getNext();
    switch (instruction.getType()) {
    case JUMP:/* w ww  .  j a v  a 2  s.  c o m*/
        // GOTO and JSR are not conditional
        if (opcode == Opcodes.GOTO || opcode == Opcodes.JSR) {
            return Collections.singleton((Instruction) ((JumpInstruction) instruction).getLabel());
        }
        assert nextInstruction != null;
        return Arrays.asList(((JumpInstruction) instruction).getLabel(), nextInstruction);
    case LOOKUPSWITCH: {
        LookupSwitchInstruction lsi = (LookupSwitchInstruction) instruction;
        Instruction[] successors = new AbstractInstruction[lsi.getHandlers().size() + 1];
        successors[0] = lsi.getDefaultHandler();
        int i = 1;
        for (LabelMarker lm : lsi.getHandlers().values())
            successors[i++] = lm;
        return Arrays.asList(successors);
    }
    case TABLESWITCH: {
        TableSwitchInstruction tsi = (TableSwitchInstruction) instruction;
        Instruction[] successors = new AbstractInstruction[tsi.getHandlers().length + 1];
        successors[0] = tsi.getDefaultHandler();
        System.arraycopy(tsi.getHandlers(), 0, successors, 1, tsi.getHandlers().length);
        return Arrays.asList(successors);
    }
    case SIMPLE:
        switch (opcode) {
        case Opcodes.IRETURN:
        case Opcodes.LRETURN:
        case Opcodes.FRETURN:
        case Opcodes.DRETURN:
        case Opcodes.ARETURN:
        case Opcodes.RETURN:
            return Collections.emptySet();

        default:
            break;
        }
        break;
    case VAR:
        if (opcode == Opcodes.RET) {
            List<JumpInstruction> callingInstructions = getJsrInstructions((VarInstruction) instruction);
            Instruction[] successors = new AbstractInstruction[callingInstructions.size()];
            int i = 0;
            for (JumpInstruction instr : callingInstructions)
                successors[i++] = instr.getNext();
            return Arrays.asList(successors);
        }
        break;
    case LABEL:
        if (instruction == instruction.getMethod().getAbnormalTerminationLabel())
            return Collections.emptySet();
        break;
    default:
        break;
    }
    assert nextInstruction != null;
    return Collections.singleton(nextInstruction);
}

From source file:de.unisb.cs.st.javaslicer.controlflowanalysis.ControlFlowGraph2.java

License:Open Source License

private Instruction followLabelsAndGotos(Instruction instr) {
    Instruction nonLabel = instr;// ww  w .  j av a 2  s  .  c om
    while (nonLabel != null) {
        if (nonLabel.getType() == InstructionType.LABEL) {
            nonLabel = nonLabel.getNext();
        } else if (nonLabel.getOpcode() == Opcodes.GOTO) {
            nonLabel = ((JumpInstruction2) nonLabel).getLabel().getNext();
        } else
            break;
    }
    return nonLabel;
}

From source file:de.unisb.cs.st.javaslicer.controlflowanalysis.ControlFlowGraph2.java

License:Open Source License

protected InstrNode getInstrNode(Instruction instruction, NodeFactory nodeFactory, boolean excludeLabels) {
    int idx = instruction.getIndex() - this.method.getInstructionNumberStart();
    // instructionNodes CFGidx???
    InstrNode node = this.instructionNodes[idx];
    // LabelGoto CFG??
    if (node != null || (excludeLabels
            && (instruction.getType() == InstructionType.LABEL || instruction.getOpcode() == Opcodes.GOTO)))
        return node;

    InstrNode newNode = nodeFactory.createNode(this, instruction);
    this.instructionNodes[idx] = newNode;
    // getSuccessor(instruction) ??
    for (Instruction succ : getSuccessors(instruction)) {
        Instruction nonLabel = excludeLabels ? followLabelsAndGotos(succ) : succ;
        if (nonLabel == null)
            continue;
        InstrNode succNode = getInstrNode(nonLabel, nodeFactory, excludeLabels);
        // ???//  w  ww  .  j a  v  a 2 s.  co m
        newNode.addSuccessor(succNode);
        succNode.addPredecessor(newNode);
    }
    return newNode;
}

From source file:de.unisb.cs.st.javaslicer.controlflowanalysis.ControlFlowGraph2.java

License:Open Source License

public Collection<Instruction> getSuccessors(Instruction instruction) {
    int opcode = instruction.getOpcode();
    // index??? getnextindex+1?
    Instruction nextInstruction = instruction.getNext();
    switch (instruction.getType()) {
    case JUMP:/*from w  ww.ja va2 s  .  co m*/
        // GOTO and JSR are not conditional
        // ????
        if (opcode == Opcodes.GOTO || opcode == Opcodes.JSR) {
            List<AbstractInstruction> instrs = this.method.instructions;
            Iterator<AbstractInstruction> iter = instrs.iterator();
            Instruction temp = null;
            while (iter.hasNext()) {
                temp = iter.next();
                if (temp.getIndex() == ((JumpInstruction2) instruction).getTarget())
                    break;
            }
            return Collections.singleton(temp);
        }
        assert nextInstruction != null;
        // ?nextInstruction, ?label!
        List<AbstractInstruction> instrs = this.method.instructions;
        Iterator<AbstractInstruction> iter = instrs.iterator();
        Instruction temp = null;
        while (iter.hasNext()) {
            temp = iter.next();
            if (temp.getIndex() == ((JumpInstruction2) instruction).getTarget())
                break;
        }
        return Arrays.asList(temp, nextInstruction);
    // switch 
    case LOOKUPSWITCH: {
        LookupSwitchInstruction2 lsi = (LookupSwitchInstruction2) instruction;
        List<AbstractInstruction> instrs4 = this.method.instructions;
        List<AbstractInstruction> instrs5 = this.method.instructions;
        Iterator<AbstractInstruction> iter4 = instrs4.iterator();
        Instruction temp2 = null;
        Instruction[] successors = new AbstractInstruction[lsi.getHandlers().size() + 1];
        // find the default
        while (iter4.hasNext()) {
            temp2 = iter4.next();
            if (temp2.getIndex() == lsi.getDefaultHandler()) {
                successors[0] = temp2;
                break;
            }
        }
        // find the handlers
        int index = 0;
        for (Integer lm : lsi.getHandlers().values()) {
            Iterator<AbstractInstruction> iter5 = instrs5.iterator();
            while (iter5.hasNext()) {
                temp2 = iter5.next();
                if (temp2.getIndex() == lm) {
                    successors[index + 1] = temp2;
                    index++;
                    break;
                }
            }
        }
        return Arrays.asList(successors);
    }
    // switch ?
    case TABLESWITCH: {
        TableSwitchInstruction2 tsi = (TableSwitchInstruction2) instruction;
        List<AbstractInstruction> instrs2 = this.method.instructions;
        List<AbstractInstruction> instrs3 = this.method.instructions;
        Iterator<AbstractInstruction> iter2 = instrs2.iterator();
        //Iterator<AbstractInstruction> iter3=instrs3.iterator();
        Instruction temp2 = null;
        Instruction[] successors = new AbstractInstruction[tsi.getHandlers().length + 1];
        // get the default target
        while (iter2.hasNext()) {
            temp2 = iter2.next();
            if (tsi.getDefaultHandler() == temp2.getIndex()) {
                successors[0] = temp2;
                break;
            }
        }
        // get the target from min to max! 
        for (int i = 0; i < tsi.getHandlers().length; i++) {
            Iterator<AbstractInstruction> iter3 = instrs3.iterator();
            while (iter3.hasNext()) {
                temp2 = iter3.next();
                if (temp2.getIndex() == (tsi.getHandlers()[i])) {
                    successors[i + 1] = temp2;
                    break;
                }
            }
        }
        return Arrays.asList(successors);
        /*
         TableSwitchInstruction tsi = (TableSwitchInstruction) instruction;
         Instruction[] successors = new AbstractInstruction[tsi.getHandlers().length+1];
         successors[0] = tsi.getDefaultHandler();
         System.arraycopy(tsi.getHandlers(), 0, successors, 1, tsi.getHandlers().length);
         return Arrays.asList(successors);*/
    }
    // ???return??nextInstruction!
    case SIMPLE:
        switch (opcode) {
        case Opcodes.IRETURN:
        case Opcodes.LRETURN:
        case Opcodes.FRETURN:
        case Opcodes.DRETURN:
        case Opcodes.ARETURN:
        case Opcodes.RETURN:
            return Collections.emptySet();

        default:
            break;
        }
        break;
    // ??ret 
    case VAR:
        if (opcode == Opcodes.RET) {
            List<JumpInstruction2> callingInstructions = getJsrInstructions((VarInstruction) instruction);
            Instruction[] successors = new AbstractInstruction[callingInstructions.size()];
            int i = 0;
            for (JumpInstruction2 instr : callingInstructions)
                successors[i++] = instr.getNext();
            return Arrays.asList(successors);
        }
        break;
    // label abnormalTermination?nextInstruction!
    case LABEL:
        if (instruction == instruction.getMethod().getAbnormalTerminationLabel())
            return Collections.emptySet();
        break;
    default:
        break;
    }
    // ?index+1??
    assert nextInstruction != null;
    return Collections.singleton(nextInstruction);
}