proguard.classfile.instruction.Instruction Java Examples

The following examples show how to use proguard.classfile.instruction.Instruction. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: InstructionSequenceMatcher.java    From java-n-IDE-for-Android with Apache License 2.0 6 votes vote down vote up
public void visitSimpleInstruction(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, SimpleInstruction simpleInstruction)
{
    Instruction patternInstruction = patternInstructions[patternInstructionIndex];

    // Check if the instruction matches the next instruction in the sequence.
    boolean condition =
        matchingOpcodes(simpleInstruction, patternInstruction) &&
        matchingArguments(simpleInstruction.constant,
                          ((SimpleInstruction)patternInstruction).constant);

    // Check if the instruction sequence is matching now.
    checkMatch(condition,
               clazz,
               method,
               codeAttribute,
               offset,
               simpleInstruction);
}
 
Example #2
Source File: EvaluationSimplifier.java    From java-n-IDE-for-Android with Apache License 2.0 6 votes vote down vote up
/**
 * Replaces the given instruction by an infinite loop.
 */
private void replaceByInfiniteLoop(Clazz       clazz,
                                   int         offset,
                                   Instruction instruction)
{
    // Replace the instruction by an infinite loop.
    Instruction replacementInstruction =
        new BranchInstruction(InstructionConstants.OP_GOTO, 0);

    if (DEBUG) System.out.println("  Replacing unreachable instruction by infinite loop "+replacementInstruction.toString(offset));

    codeAttributeEditor.replaceInstruction(offset, replacementInstruction);

    // Visit the instruction, if required.
    if (extraInstructionVisitor != null)
    {
        // Note: we're not passing the right arguments for now, knowing that
        // they aren't used anyway.
        instruction.accept(clazz, null, null, offset, extraInstructionVisitor);
    }
}
 
Example #3
Source File: EvaluationSimplifier.java    From java-n-IDE-for-Android with Apache License 2.0 6 votes vote down vote up
/**
 * Replaces the reference pushing instruction at the given offset by a
 * simpler push instruction, if possible.
 */
private void replaceReferencePushInstruction(Clazz       clazz,
                                             int         offset,
                                             Instruction instruction)
{
    Value pushedValue = partialEvaluator.getStackAfter(offset).getTop(0);
    if (pushedValue.isParticular())
    {
        // A reference value can only be specific if it is null.
        replaceConstantPushInstruction(clazz,
                                       offset,
                                       instruction,
                                       InstructionConstants.OP_ACONST_NULL,
                                       0);
    }
}
 
Example #4
Source File: EvaluationSimplifier.java    From java-n-IDE-for-Android with Apache License 2.0 6 votes vote down vote up
/**
 * Replaces the instruction at a given offset by a given push instruction.
 */
private void replaceInstruction(Clazz       clazz,
                                int         offset,
                                Instruction instruction,
                                Instruction replacementInstruction)
{
    // Pop unneeded stack entries if necessary.
    int popCount =
        instruction.stackPopCount(clazz) -
        replacementInstruction.stackPopCount(clazz);

    insertPopInstructions(offset, popCount);

    if (DEBUG) System.out.println("  Replacing instruction "+instruction.toString(offset)+" -> "+replacementInstruction.toString()+(popCount == 0 ? "" : " ("+popCount+" pops)"));

    codeAttributeEditor.replaceInstruction(offset, replacementInstruction);

    // Visit the instruction, if required.
    if (extraInstructionVisitor != null)
    {
        // Note: we're not passing the right arguments for now, knowing that
        // they aren't used anyway.
        instruction.accept(clazz, null, null, offset, extraInstructionVisitor);
    }
}
 
Example #5
Source File: CodeAttributeEditor.java    From java-n-IDE-for-Android with Apache License 2.0 6 votes vote down vote up
public void accept(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, InstructionVisitor instructionVisitor)
{
    if (instructionVisitor != CodeAttributeEditor.this)
    {
        throw new UnsupportedOperationException("Unexpected visitor ["+instructionVisitor+"]");
    }

    for (int index = 0; index < instructions.length; index++)
    {
        Instruction instruction = instructions[index];

        instruction.accept(clazz, method, codeAttribute, offset, CodeAttributeEditor.this);

        offset += instruction.length(offset);
    }
}
 
Example #6
Source File: ConfigurationLoggingInstructionSequencesReplacer.java    From proguard with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Creates an array of InstructionSequenceReplacer instances.
 *
 * @param constants               any constants referenced by the pattern
 *                                instructions and replacement instructions.
 * @param instructionSequences    the instruction sequences to be replaced,
 *                                with subsequently the sequence pair index,
 *                                the from/to index (0 or 1), and the
 *                                instruction index in the sequence.
 * @param branchTargetFinder      a branch target finder that has been
 *                                initialized to indicate branch targets
 *                                in the visited code.
 * @param codeAttributeEditor     a code editor that can be used for
 *                                accumulating changes to the code.
 * @param extraInstructionVisitor an optional extra visitor for all deleted
 *                                load instructions.
 */
private static InstructionVisitor[] createInstructionSequenceReplacers(Constant[]          constants,
                                                                       Instruction[][][]   instructionSequences,
                                                                       BranchTargetFinder  branchTargetFinder,
                                                                       CodeAttributeEditor codeAttributeEditor,
                                                                       InstructionVisitor  extraInstructionVisitor)
{
    InstructionVisitor[] instructionSequenceReplacers =
        new InstructionSequenceReplacer[instructionSequences.length];

    for (int index = 0; index < instructionSequenceReplacers.length; index++)
    {
        Instruction[][] instructionSequencePair = instructionSequences[index];
        instructionSequenceReplacers[index] =
            new ConfigurationLoggingInstructionSequenceReplacer(constants,
                                                                instructionSequencePair[PATTERN_INDEX],
                                                                constants,
                                                                instructionSequencePair[REPLACEMENT_INDEX],
                                                                branchTargetFinder,
                                                                codeAttributeEditor,
                                                                extraInstructionVisitor);
    }

    return instructionSequenceReplacers;
}
 
Example #7
Source File: InstructionSequencesReplacer.java    From java-n-IDE-for-Android with Apache License 2.0 6 votes vote down vote up
/**
 * Creates an array of InstructionSequenceReplacer instances.
 * @param patternConstants        any constants referenced by the pattern
 *                                instruction.
 * @param instructionSequences    the instruction sequences to be replaced,
 *                                with subsequently the sequence pair index,
 *                                the from/to index (0 or 1), and the
 *                                instruction index in the sequence.
 * @param branchTargetFinder      a branch target finder that has been
 *                                initialized to indicate branch targets
 *                                in the visited code.
 * @param codeAttributeEditor     a code editor that can be used for
 *                                accumulating changes to the code.
 * @param extraInstructionVisitor an optional extra visitor for all deleted
 *                                load instructions.
 */
private static InstructionVisitor[] createInstructionSequenceReplacers(Constant[]          patternConstants,
                                                                       Instruction[][][]   instructionSequences,
                                                                       BranchTargetFinder  branchTargetFinder,
                                                                       CodeAttributeEditor codeAttributeEditor,
                                                                       InstructionVisitor extraInstructionVisitor)
{
    InstructionVisitor[] instructionSequenceReplacers =
        new InstructionSequenceReplacer[instructionSequences.length];

    for (int index = 0; index < instructionSequenceReplacers.length; index++)
    {
        Instruction[][] instructionSequencePair = instructionSequences[index];
        instructionSequenceReplacers[index] =
            new InstructionSequenceReplacer(patternConstants,
                                            instructionSequencePair[PATTERN_INDEX],
                                            instructionSequencePair[REPLACEMENT_INDEX],
                                            branchTargetFinder,
                                            codeAttributeEditor,
                                            extraInstructionVisitor);
    }

    return instructionSequenceReplacers;
}
 
Example #8
Source File: InstructionWriter.java    From java-n-IDE-for-Android with Apache License 2.0 6 votes vote down vote up
public void visitVariableInstruction(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, VariableInstruction variableInstruction)
{
    try
    {
        // Try to write out the instruction.
        variableInstruction.write(codeAttribute, offset);
    }
    catch (IllegalArgumentException exception)
    {
        // Create a new variable instruction that will fit.
        Instruction replacementInstruction =
            new VariableInstruction(variableInstruction.opcode,
                                    variableInstruction.variableIndex,
                                    variableInstruction.constant).shrink();

        replaceInstruction(offset, replacementInstruction);

        // Write out a dummy variable instruction for now.
        variableInstruction.variableIndex = 0;
        variableInstruction.constant      = 0;
        variableInstruction.write(codeAttribute, offset);
    }
}
 
Example #9
Source File: ConfigurationLoggingInstructionSequenceReplacer.java    From proguard with GNU General Public License v2.0 6 votes vote down vote up
public ConfigurationLoggingInstructionSequenceReplacer(Constant[]          patternConstants,
                                                       Instruction[]       patternInstructions,
                                                       Constant[]          replacementConstants,
                                                       Instruction[]       replacementInstructions,
                                                       BranchTargetFinder  branchTargetFinder,
                                                       CodeAttributeEditor codeAttributeEditor,
                                                       InstructionVisitor  extraInstructionVisitor )
{
    super(patternConstants,
          patternInstructions,
          replacementConstants,
          replacementInstructions,
          branchTargetFinder,
          codeAttributeEditor,
          extraInstructionVisitor );
}
 
Example #10
Source File: InstructionWriter.java    From java-n-IDE-for-Android with Apache License 2.0 6 votes vote down vote up
public void visitConstantInstruction(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, ConstantInstruction constantInstruction)
{
    try
    {
        // Try to write out the instruction.
        constantInstruction.write(codeAttribute, offset);
    }
    catch (IllegalArgumentException exception)
    {
        // Create a new constant instruction that will fit.
        Instruction replacementInstruction =
            new ConstantInstruction(constantInstruction.opcode,
                                    constantInstruction.constantIndex,
                                    constantInstruction.constant).shrink();

        replaceInstruction(offset, replacementInstruction);

        // Write out a dummy constant instruction for now.
        constantInstruction.constantIndex = 0;
        constantInstruction.constant      = 0;
        constantInstruction.write(codeAttribute, offset);
    }
}
 
Example #11
Source File: ConfigurationLoggingInstructionSequenceReplacer.java    From proguard with GNU General Public License v2.0 6 votes vote down vote up
public ConfigurationLoggingInstructionSequenceReplacer(InstructionSequenceMatcher instructionSequenceMatcher,
                                                       Constant[]                 patternConstants,
                                                       Instruction[]              patternInstructions,
                                                       Constant[]                 replacementConstants,
                                                       Instruction[]              replacementInstructions,
                                                       BranchTargetFinder         branchTargetFinder,
                                                       CodeAttributeEditor        codeAttributeEditor,
                                                       InstructionVisitor         extraInstructionVisitor    )
{
    super(instructionSequenceMatcher,
          patternConstants,
          patternInstructions,
          replacementConstants,
          replacementInstructions,
          branchTargetFinder,
          codeAttributeEditor,
          extraInstructionVisitor    );
}
 
Example #12
Source File: UnreachableCodeRemover.java    From proguard with GNU General Public License v2.0 6 votes vote down vote up
public void visitAnyInstruction(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, Instruction instruction)
{
    if (DEBUG)
    {
        System.out.println("  "+(reachableCodeMarker.isReachable(offset) ? "+" : "-")+" "+instruction.toString(offset));
    }

    // Is this instruction unreachable?
    if (!reachableCodeMarker.isReachable(offset))
    {
        // Then delete it.
        codeAttributeEditor.deleteInstruction(offset);

        // Visit the instruction, if required.
        if (extraInstructionVisitor != null)
        {
            instruction.accept(clazz, method, codeAttribute, offset, extraInstructionVisitor);
        }
    }
}
 
Example #13
Source File: InstructionSequenceMatcher.java    From java-n-IDE-for-Android with Apache License 2.0 6 votes vote down vote up
public void visitLookUpSwitchInstruction(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, LookUpSwitchInstruction lookUpSwitchInstruction)
{
    Instruction patternInstruction = patternInstructions[patternInstructionIndex];

    // Check if the instruction matches the next instruction in the sequence.
    boolean condition =
        matchingOpcodes(lookUpSwitchInstruction, patternInstruction) &&
        matchingBranchOffsets(offset,
                              lookUpSwitchInstruction.defaultOffset,
                              ((LookUpSwitchInstruction)patternInstruction).defaultOffset) &&
        matchingArguments(lookUpSwitchInstruction.cases,
                          ((LookUpSwitchInstruction)patternInstruction).cases) &&
        matchingJumpOffsets(offset,
                            lookUpSwitchInstruction.jumpOffsets,
                            ((LookUpSwitchInstruction)patternInstruction).jumpOffsets);

    // Check if the instruction sequence is matching now.
    checkMatch(condition,
               clazz,
               method,
               codeAttribute,
               offset,
               lookUpSwitchInstruction);
}
 
Example #14
Source File: InstructionSequenceMatcher.java    From java-n-IDE-for-Android with Apache License 2.0 6 votes vote down vote up
public void visitTableSwitchInstruction(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, TableSwitchInstruction tableSwitchInstruction)
{
    Instruction patternInstruction = patternInstructions[patternInstructionIndex];

    // Check if the instruction matches the next instruction in the sequence.
    boolean condition =
        matchingOpcodes(tableSwitchInstruction, patternInstruction) &&
        matchingBranchOffsets(offset,
                              tableSwitchInstruction.defaultOffset,
                              ((TableSwitchInstruction)patternInstruction).defaultOffset) &&
        matchingArguments(tableSwitchInstruction.lowCase,
                          ((TableSwitchInstruction)patternInstruction).lowCase)  &&
        matchingArguments(tableSwitchInstruction.highCase,
                          ((TableSwitchInstruction)patternInstruction).highCase) &&
        matchingJumpOffsets(offset,
                            tableSwitchInstruction.jumpOffsets,
                            ((TableSwitchInstruction)patternInstruction).jumpOffsets);

    // Check if the instruction sequence is matching now.
    checkMatch(condition,
               clazz,
               method,
               codeAttribute,
               offset,
               tableSwitchInstruction);
}
 
Example #15
Source File: InstructionSequenceMatcher.java    From java-n-IDE-for-Android with Apache License 2.0 6 votes vote down vote up
public void visitBranchInstruction(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, BranchInstruction branchInstruction)
{
    Instruction patternInstruction = patternInstructions[patternInstructionIndex];

    // Check if the instruction matches the next instruction in the from
    // sequence.
    boolean condition =
        matchingOpcodes(branchInstruction, patternInstruction) &&
        matchingBranchOffsets(offset,
                              branchInstruction.branchOffset,
                              ((BranchInstruction)patternInstruction).branchOffset);

    // Check if the instruction sequence is matching now.
    checkMatch(condition,
               clazz,
               method,
               codeAttribute,
               offset,
               branchInstruction);
}
 
Example #16
Source File: InstructionSequenceMatcher.java    From java-n-IDE-for-Android with Apache License 2.0 6 votes vote down vote up
public void visitConstantInstruction(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, ConstantInstruction constantInstruction)
{
    Instruction patternInstruction = patternInstructions[patternInstructionIndex];

    // Check if the instruction matches the next instruction in the sequence.
    boolean condition =
        matchingOpcodes(constantInstruction, patternInstruction) &&
        matchingConstantIndices(clazz,
                                constantInstruction.constantIndex,
                                ((ConstantInstruction)patternInstruction).constantIndex) &&
        matchingArguments(constantInstruction.constant,
                          ((ConstantInstruction)patternInstruction).constant);

    // Check if the instruction sequence is matching now.
    checkMatch(condition,
               clazz,
               method,
               codeAttribute,
               offset,
               constantInstruction);
}
 
Example #17
Source File: UnreachableCodeRemover.java    From proguard with GNU General Public License v2.0 6 votes vote down vote up
public void visitAnyInstruction(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, Instruction instruction)
{
    if (DEBUG)
    {
        System.out.println("  "+(reachableCodeMarker.isReachable(offset) ? "+" : "-")+" "+instruction.toString(offset));
    }

    // Is this instruction unreachable?
    if (!reachableCodeMarker.isReachable(offset))
    {
        // Then delete it.
        codeAttributeEditor.deleteInstruction(offset);

        // Visit the instruction, if required.
        if (extraInstructionVisitor != null)
        {
            instruction.accept(clazz, method, codeAttribute, offset, extraInstructionVisitor);
        }
    }
}
 
Example #18
Source File: UnreachableExceptionRemover.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Returns whether the specified block of code may throw exceptions.
 */
private boolean mayThrowExceptions(Clazz         clazz,
                                   Method        method,
                                   CodeAttribute codeAttribute,
                                   int           startOffset,
                                   int           endOffset)
{
    byte[] code = codeAttribute.code;

    // Go over all instructions.
    int offset = startOffset;
    while (offset < endOffset)
    {
        // Get the current instruction.
        Instruction instruction = InstructionFactory.create(code, offset);

        // Check if it may be throwing exceptions.
        if (exceptionInstructionChecker.mayThrowExceptions(clazz,
                                                           method,
                                                           codeAttribute,
                                                           offset,
                                                           instruction))
        {
            return true;
        }

        // Go to the next instruction.
        offset += instruction.length(offset);
    }

    return false;
}
 
Example #19
Source File: CodeAttributeEditor.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Remembers to place the given instruction right before the instruction
 * at the given offset.
 * @param instructionOffset the offset of the instruction.
 * @param instruction       the new instruction.
 */
public void insertBeforeInstruction(int instructionOffset, Instruction instruction)
{
    if (instructionOffset < 0 ||
        instructionOffset >= codeLength)
    {
        throw new IllegalArgumentException("Invalid instruction offset ["+instructionOffset+"] in code with length ["+codeLength+"]");
    }

    preInsertions[instructionOffset] = instruction;

    modified = true;
    simple   = false;

}
 
Example #20
Source File: CodeAttributeEditor.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Checks if it is possible to modifies the given code without having to
 * update any offsets.
 * @param codeAttribute the code to be changed.
 * @return the new code length.
 */
private boolean canPerformSimpleReplacements(CodeAttribute codeAttribute)
{
    if (!simple)
    {
        return false;
    }

    byte[] code       = codeAttribute.code;
    int    codeLength = codeAttribute.u4codeLength;

    // Go over all replacement instructions.
    for (int offset = 0; offset < codeLength; offset++)
    {
        // Check if the replacement instruction, if any, has a different
        // length than the original instruction.
        Instruction replacementInstruction = replacements[offset];
        if (replacementInstruction != null &&
            replacementInstruction.length(offset) !=
                InstructionFactory.create(code, offset).length(offset))
        {
            return false;
        }
    }

    return true;
}
 
Example #21
Source File: ReachableCodeMarker.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Marks the code starting at the given offset.
 */
private void markCode(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset)
{
    boolean oldNext = next;

    byte[] code = codeAttribute.code;

    // Continue with the current instruction as long as we haven't marked it
    // yet.
    while (!isReachable[offset])
    {
        // Get the current instruction.
        Instruction instruction = InstructionFactory.create(code, offset);

        // Mark it as reachable.
        isReachable[offset] = true;

        // By default, we'll assume we can continue with the next
        // instruction in a moment.
        next = true;

        // Mark the branch targets, if any.
        instruction.accept(clazz, method, codeAttribute, offset, this);

        // Can we really continue with the next instruction?
        if (!next)
        {
            break;
        }

        // Go to the next instruction.
        offset += instruction.length(offset);
    }

    next = oldNext;
}
 
Example #22
Source File: CodeAttributeEditor.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Fills out the instruction offset map for the given code block.
 * @param oldCode   the instructions to be moved.
 * @param oldLength the code length.
 * @return the new code length.
 */
private int mapInstructions(byte[] oldCode, int oldLength)
{
    // Start mapping instructions at the beginning.
    newOffset       = 0;
    lengthIncreased = false;

    int oldOffset = 0;
    do
    {
        // Get the next instruction.
        Instruction instruction = InstructionFactory.create(oldCode, oldOffset);

        // Compute the mapping of the instruction.
        mapInstruction(oldOffset, instruction);

        oldOffset += instruction.length(oldOffset);

        if (newOffset > oldOffset)
        {
            lengthIncreased = true;
        }
    }
    while (oldOffset < oldLength);

    // Also add an entry for the first offset after the code.
    instructionOffsetMap[oldOffset] = newOffset;

    return newOffset;
}
 
Example #23
Source File: CodeAttributeEditor.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Fills out the instruction offset map for the given instruction.
 * @param oldOffset   the instruction's old offset.
 * @param instruction the instruction to be moved.
 */
private void mapInstruction(int         oldOffset,
                            Instruction instruction)
{
    instructionOffsetMap[oldOffset] = newOffset;

    // Account for the pre-inserted instruction, if any.
    Instruction preInstruction = preInsertions[oldOffset];
    if (preInstruction != null)
    {
        newOffset += preInstruction.length(newOffset);
    }

    // Account for the replacement instruction, or for the current
    // instruction, if it shouldn't be  deleted.
    Instruction replacementInstruction = replacements[oldOffset];
    if (replacementInstruction != null)
    {
        newOffset += replacementInstruction.length(newOffset);
    }
    else if (!deleted[oldOffset])
    {
        // Note that the instruction's length may change at its new offset,
        // e.g. if it is a switch instruction.
        newOffset += instruction.length(newOffset);
    }

    // Account for the post-inserted instruction, if any.
    Instruction postInstruction = postInsertions[oldOffset];
    if (postInstruction != null)
    {
        newOffset += postInstruction.length(newOffset);
    }
}
 
Example #24
Source File: DuplicateInitializerInvocationFixer.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
public void visitConstantInstruction(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, ConstantInstruction constantInstruction)
{
    if (constantInstruction.opcode == InstructionConstants.OP_INVOKESPECIAL)
    {
        hasBeenFixed = false;
        clazz.constantPoolEntryAccept(constantInstruction.constantIndex, this);

        if (hasBeenFixed)
        {
            Instruction extraInstruction =
                new SimpleInstruction(InstructionConstants.OP_ICONST_0);

            codeAttributeEditor.insertBeforeInstruction(offset,
                                                        extraInstruction);

            if (DEBUG)
            {
                System.out.println("  ["+clazz.getName()+"."+method.getName(clazz)+method.getDescriptor(clazz)+"] Inserting "+extraInstruction.toString()+" before "+constantInstruction.toString(offset));
            }

            if (extraAddedInstructionVisitor != null)
            {
                extraInstruction.accept(null, null, null, offset, extraAddedInstructionVisitor);
            }
        }
    }
}
 
Example #25
Source File: CodeAttributeEditor.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Remembers to place the given instruction right after the instruction
 * at the given offset.
 * @param instructionOffset the offset of the instruction.
 * @param instruction       the new instruction.
 */
public void insertAfterInstruction(int instructionOffset, Instruction instruction)
{
    if (instructionOffset < 0 ||
        instructionOffset >= codeLength)
    {
        throw new IllegalArgumentException("Invalid instruction offset ["+instructionOffset+"] in code with length ["+codeLength+"]");
    }

    postInsertions[instructionOffset] = instruction;

    modified = true;
    simple   = false;
}
 
Example #26
Source File: ExceptionInstructionChecker.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Returns whether the given instruction may throw exceptions.
 */
public boolean mayThrowExceptions(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, Instruction instruction)
{
    mayThrowExceptions = false;

    instruction.accept(clazz, method,  codeAttribute, offset, this);

    return mayThrowExceptions;
}
 
Example #27
Source File: InstructionAdder.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
public void visitConstantInstruction(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, ConstantInstruction constantInstruction)
{
    // Create a copy of the instruction.
    Instruction newConstantInstruction =
        new ConstantInstruction(constantInstruction.opcode,
                                constantAdder.addConstant(clazz, constantInstruction.constantIndex),
                                constantInstruction.constant).shrink();

    // Add the instruction.
    codeAttributeComposer.appendInstruction(offset, newConstantInstruction);
}
 
Example #28
Source File: InstructionWriter.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Remembers to place the given instruction right before the instruction
 * at the given offset.
 */
private void insertBeforeInstruction(int instructionOffset, Instruction instruction)
{
    ensureCodeAttributeEditor();

    // Replace the instruction.
    codeAttributeEditor.insertBeforeInstruction(instructionOffset, instruction);
}
 
Example #29
Source File: CodeAttributeEditor.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
public Instruction shrink()
{
    for (int index = 0; index < instructions.length; index++)
    {
        instructions[index] = instructions[index].shrink();
    }

    return this;
}
 
Example #30
Source File: GotoReturnReplacer.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
public void visitBranchInstruction(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, BranchInstruction branchInstruction)
{
    // Check if the instruction is an unconditional goto instruction.
    byte opcode = branchInstruction.opcode;
    if (opcode == InstructionConstants.OP_GOTO ||
        opcode == InstructionConstants.OP_GOTO_W)
    {
        // Check if the goto instruction points to a return instruction.
        int targetOffset = offset + branchInstruction.branchOffset;

        if (!codeAttributeEditor.isModified(offset) &&
            !codeAttributeEditor.isModified(targetOffset))
        {
            Instruction targetInstruction = InstructionFactory.create(codeAttribute.code,
                                                                      targetOffset);
            switch (targetInstruction.opcode)
            {
                case InstructionConstants.OP_IRETURN:
                case InstructionConstants.OP_LRETURN:
                case InstructionConstants.OP_FRETURN:
                case InstructionConstants.OP_DRETURN:
                case InstructionConstants.OP_ARETURN:
                case InstructionConstants.OP_RETURN:
                    // Replace the goto instruction by the return instruction.
                    Instruction returnInstruction =
                         new SimpleInstruction(targetInstruction.opcode);
                    codeAttributeEditor.replaceInstruction(offset,
                                                           returnInstruction);

                    // Visit the instruction, if required.
                    if (extraInstructionVisitor != null)
                    {
                        extraInstructionVisitor.visitBranchInstruction(clazz, method, codeAttribute, offset, branchInstruction);
                    }

                    break;
            }
        }
    }
}