Java Code Examples for proguard.classfile.attribute.CodeAttribute#exceptionsAccept()

The following examples show how to use proguard.classfile.attribute.CodeAttribute#exceptionsAccept() . 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: MethodInliner.java    From java-n-IDE-for-Android with Apache License 2.0 6 votes vote down vote up
/**
 * Appends the code of the given code attribute.
 */
private void copyCode(Clazz clazz, Method method, CodeAttribute codeAttribute)
{
    // The code may expand, due to expanding constant and variable
    // instructions.
    codeAttributeComposer.beginCodeFragment(codeAttribute.u4codeLength);

    // Copy the instructions.
    codeAttribute.instructionsAccept(clazz, method, this);

    // Append a label just after the code.
    codeAttributeComposer.appendLabel(codeAttribute.u4codeLength);

    // Copy the exceptions.
    codeAttribute.exceptionsAccept(clazz, method, exceptionInfoAdder);

    codeAttributeComposer.endCodeFragment();
}
 
Example 2
Source File: ClassPrinter.java    From java-n-IDE-for-Android with Apache License 2.0 6 votes vote down vote up
public void visitCodeAttribute(Clazz clazz, Method method, CodeAttribute codeAttribute)
{
    println(visitorInfo(codeAttribute) +
            " Code attribute instructions (code length = "+ codeAttribute.u4codeLength +
            ", locals = "+ codeAttribute.u2maxLocals +
            ", stack = "+ codeAttribute.u2maxStack + "):");

    indent();

    codeAttribute.instructionsAccept(clazz, method, this);

    println("Code attribute exceptions (count = " +
            codeAttribute.u2exceptionTableLength + "):");

    codeAttribute.exceptionsAccept(clazz, method, this);

    println("Code attribute attributes (attribute count = " +
            codeAttribute.u2attributesCount + "):");

    codeAttribute.attributesAccept(clazz, method, this);

    outdent();
}
 
Example 3
Source File: ConstantPoolRemapper.java    From java-n-IDE-for-Android with Apache License 2.0 6 votes vote down vote up
public void visitCodeAttribute(Clazz clazz, Method method, CodeAttribute codeAttribute)
{
    codeAttribute.u2attributeNameIndex =
        remapConstantIndex(codeAttribute.u2attributeNameIndex);

    // Initially, the code attribute editor doesn't contain any changes.
    codeAttributeEditor.reset(codeAttribute.u4codeLength);

    // Remap the constant pool references of the instructions.
    codeAttribute.instructionsAccept(clazz, method, this);

    // Apply the code atribute editor. It will only contain any changes if
    // the code length is changing at any point.
    codeAttributeEditor.visitCodeAttribute(clazz, method, codeAttribute);

    // Remap the constant pool references of the exceptions and attributes.
    codeAttribute.exceptionsAccept(clazz, method, this);
    codeAttribute.attributesAccept(clazz, method, this);
}
 
Example 4
Source File: ProgramClassWriter.java    From java-n-IDE-for-Android with Apache License 2.0 6 votes vote down vote up
public void visitCodeAttribute(Clazz clazz, Method method, CodeAttribute codeAttribute)
{
    // Write the stack size and local variable frame size.
    dataOutput.writeShort(codeAttribute.u2maxStack);
    dataOutput.writeShort(codeAttribute.u2maxLocals);

    // Write the byte code.
    dataOutput.writeInt(codeAttribute.u4codeLength);

    dataOutput.write(codeAttribute.code, 0, codeAttribute.u4codeLength);

    // Write the exceptions.
    dataOutput.writeShort(codeAttribute.u2exceptionTableLength);

    codeAttribute.exceptionsAccept(clazz, method, this);

    // Write the code attributes.
    dataOutput.writeShort(codeAttribute.u2attributesCount);

    codeAttribute.attributesAccept(clazz, method, ProgramClassWriter.this);
}
 
Example 5
Source File: UnreachableExceptionRemover.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
public void visitCodeAttribute(Clazz clazz, Method method, CodeAttribute codeAttribute)
{
    // Go over the exception table.
    codeAttribute.exceptionsAccept(clazz, method, this);

    // Remove exceptions with empty code blocks.
    codeAttribute.u2exceptionTableLength =
        removeEmptyExceptions(codeAttribute.exceptionTable,
                              codeAttribute.u2exceptionTableLength);
}
 
Example 6
Source File: UsageMarker.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
public void visitCodeAttribute(Clazz clazz, Method method, CodeAttribute codeAttribute)
{
    markAsUsed(codeAttribute);

    markConstant(clazz, codeAttribute.u2attributeNameIndex);

    // Mark the constant pool entries referenced by the instructions,
    // by the exceptions, and by the attributes.
    codeAttribute.instructionsAccept(clazz, method, this);
    codeAttribute.exceptionsAccept(clazz, method, this);
    codeAttribute.attributesAccept(clazz, method, this);
}
 
Example 7
Source File: StackSizeComputer.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
public void visitCodeAttribute0(Clazz clazz, Method method, CodeAttribute codeAttribute)
{
    if (DEBUG)
    {
        System.out.println("StackSizeComputer: "+clazz.getName()+"."+method.getName(clazz)+method.getDescriptor(clazz));
    }

    // Try to reuse the previous array.
    int codeLength = codeAttribute.u4codeLength;
    if (evaluated.length < codeLength)
    {
        evaluated  = new boolean[codeLength];
        stackSizes = new int[codeLength];
    }
    else
    {
        for (int index = 0; index < codeLength; index++)
        {
            evaluated[index] = false;
        }
    }

    // The initial stack is always empty.
    stackSize    = 0;
    maxStackSize = 0;

    // Evaluate the instruction block starting at the entry point of the method.
    evaluateInstructionBlock(clazz, method, codeAttribute, 0);

    // Evaluate the exception handlers.
    codeAttribute.exceptionsAccept(clazz, method, this);
}
 
Example 8
Source File: ClassCleaner.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
public void visitCodeAttribute(Clazz clazz, Method method, CodeAttribute codeAttribute)
{
    clean(codeAttribute);

    codeAttribute.exceptionsAccept(clazz, method, this);
    codeAttribute.attributesAccept(clazz, method, this);
}
 
Example 9
Source File: ReachableCodeMarker.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
public void visitCodeAttribute(Clazz clazz, Method method, CodeAttribute codeAttribute)
{
    // Make sure there is a sufficiently large array.
    int codeLength = codeAttribute.u4codeLength;
    if (isReachable.length < codeLength)
    {
        // Create a new array.
        isReachable = new boolean[codeLength];
    }
    else
    {
        // Reset the array.
        for (int index = 0; index < codeLength; index++)
        {
            isReachable[index] = false;
        }
    }

    // Mark the code, starting at the entry point.
    markCode(clazz, method, codeAttribute, 0);

    // Mark the exception handlers, iterating as long as necessary.
    do
    {
        evaluateExceptions = false;

        codeAttribute.exceptionsAccept(clazz, method, this);
    }
    while (evaluateExceptions);
}
 
Example 10
Source File: PartialEvaluator.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Evaluates the exception handlers covering and targeting the given
 * instruction range in the given code.
 */
private void evaluateExceptionHandlers(Clazz         clazz,
                                       Method        method,
                                       CodeAttribute codeAttribute,
                                       int           startOffset,
                                       int           endOffset)
{
    if (DEBUG) System.out.println("Evaluating exceptions covering ["+startOffset+" -> "+endOffset+"]:");

    ExceptionHandlerFilter exceptionEvaluator =
        new ExceptionHandlerFilter(startOffset,
                                   endOffset,
                                   this);

    // Evaluate the exception catch blocks, until their entry variables
    // have stabilized.
    do
    {
        // Reset the flag to stop evaluating.
        evaluateExceptions = false;

        // Evaluate all relevant exception catch blocks once.
        codeAttribute.exceptionsAccept(clazz,
                                       method,
                                       startOffset,
                                       endOffset,
                                       exceptionEvaluator);
    }
    while (evaluateExceptions);
}
 
Example 11
Source File: AttributeAdder.java    From java-n-IDE-for-Android with Apache License 2.0 4 votes vote down vote up
public void visitCodeAttribute(Clazz clazz, Method method, CodeAttribute codeAttribute)
{
    // Create a new code attribute.
    CodeAttribute newCodeAttribute =
        new CodeAttribute(constantAdder.addConstant(clazz, codeAttribute.u2attributeNameIndex),
                          codeAttribute.u2maxStack,
                          codeAttribute.u2maxLocals,
                          0,
                          EMPTY_BYTES,
                          0,
                          codeAttribute.u2exceptionTableLength > 0 ?
                              new ExceptionInfo[codeAttribute.u2exceptionTableLength] :
                              EMPTY_EXCEPTIONS,
                          0,
                          codeAttribute.u2attributesCount > 0 ?
                              new Attribute[codeAttribute.u2attributesCount] :
                              EMPTY_ATTRIBUTES);

    CodeAttributeComposer codeAttributeComposer = new CodeAttributeComposer();

    codeAttributeComposer.beginCodeFragment(codeAttribute.u4codeLength + 32);

    // Add the instructions.
    codeAttribute.instructionsAccept(clazz,
                                     method,
                                     new InstructionAdder(targetClass,
                                                          codeAttributeComposer));

    // Append a label just after the code.
    codeAttributeComposer.appendLabel(codeAttribute.u4codeLength);

    // Add the exceptions.
    codeAttribute.exceptionsAccept(clazz,
                                   method,
                                   new ExceptionInfoAdder(targetClass,
                                                          codeAttributeComposer));

    codeAttributeComposer.endCodeFragment();

    // Add the attributes.
    codeAttribute.attributesAccept(clazz,
                                   method,
                                   new AttributeAdder(targetClass,
                                                      targetMember,
                                                      newCodeAttribute,
                                                      replaceAttributes));

    // Apply these changes to the new code attribute.
    codeAttributeComposer.visitCodeAttribute(targetClass,
                                             (Method)targetMember,
                                             newCodeAttribute);

    // Add the completed code attribute to the target method.
    attributesEditor.addAttribute(newCodeAttribute);
}
 
Example 12
Source File: TailRecursionSimplifier.java    From java-n-IDE-for-Android with Apache License 2.0 4 votes vote down vote up
public void visitConstantInstruction(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, ConstantInstruction constantInstruction)
{
    // Is it a method invocation?
    switch (constantInstruction.opcode)
    {
        case InstructionConstants.OP_INVOKEVIRTUAL:
        case InstructionConstants.OP_INVOKESPECIAL:
        case InstructionConstants.OP_INVOKESTATIC:
        {
            // Is it a recursive call?
            clazz.constantPoolEntryAccept(constantInstruction.constantIndex, recursionChecker);

            if (recursionChecker.isRecursive())
            {
                // Is the next instruction a return?
                int nextOffset =
                    offset + constantInstruction.length(offset);

                Instruction nextInstruction =
                    InstructionFactory.create(codeAttribute.code, nextOffset);

                switch (nextInstruction.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:
                    {
                        // Isn't the recursive call inside a try/catch block?
                        codeAttribute.exceptionsAccept(clazz, method, offset, recursionChecker);

                        if (recursionChecker.isRecursive())
                        {
                            if (DEBUG)
                            {
                                System.out.println("TailRecursionSimplifier: ["+
                                                   clazz.getName()+"."+method.getName(clazz)+method.getDescriptor(clazz)+"], inlining "+constantInstruction.toString(offset));
                            }

                            // Append a label.
                            codeAttributeComposer.appendLabel(offset);

                            storeParameters(clazz, method);

                            // Branch back to the start of the method.
                            int gotoOffset = offset + 1;
                            codeAttributeComposer.appendInstruction(gotoOffset,
                                                                    new BranchInstruction(InstructionConstants.OP_GOTO, -gotoOffset));

                            // The original return instruction will be
                            // removed elsewhere, if possible.

                            // Remember that the code has changed.
                            inlinedAny = true;

                            if (extraTailRecursionVisitor != null)
                            {
                                extraTailRecursionVisitor.visitConstantInstruction(clazz, method, codeAttribute, offset, constantInstruction);
                            }

                            // The invocation itself is no longer necessary.
                            return;
                        }
                    }
                }
            }

            break;
        }
    }

    // Copy the instruction.
    codeAttributeComposer.appendInstruction(offset, constantInstruction.shrink());
}
 
Example 13
Source File: TailRecursionSimplifier.java    From java-n-IDE-for-Android with Apache License 2.0 4 votes vote down vote up
public void visitCodeAttribute(Clazz clazz, Method method, CodeAttribute codeAttribute)
    {
        int accessFlags = method.getAccessFlags();

        if (// Only check the method if it is private, static, or final.
            (accessFlags & (ClassConstants.INTERNAL_ACC_PRIVATE |
                            ClassConstants.INTERNAL_ACC_STATIC  |
                            ClassConstants.INTERNAL_ACC_FINAL)) != 0 &&

            // Only check the method if it is not synchronized, etc.
            (accessFlags & (ClassConstants.INTERNAL_ACC_SYNCHRONIZED |
                            ClassConstants.INTERNAL_ACC_NATIVE       |
                            ClassConstants.INTERNAL_ACC_INTERFACE    |
                            ClassConstants.INTERNAL_ACC_ABSTRACT)) == 0)
        {
//            codeAttributeComposer.DEBUG = DEBUG =
//                clazz.getName().equals("abc/Def") &&
//                method.getName(clazz).equals("abc");

            targetMethod = method;
            inlinedAny   = false;
            codeAttributeComposer.reset();

            // The code may expand, due to expanding constant and variable
            // instructions.
            codeAttributeComposer.beginCodeFragment(codeAttribute.u4codeLength);

            // Copy the instructions.
            codeAttribute.instructionsAccept(clazz, method, this);

            // Update the code attribute if any code has been inlined.
            if (inlinedAny)
            {
                // Copy the exceptions.
                codeAttribute.exceptionsAccept(clazz, method, this);

                // Append a label just after the code.
                codeAttributeComposer.appendLabel(codeAttribute.u4codeLength);

                codeAttributeComposer.endCodeFragment();

                codeAttributeComposer.visitCodeAttribute(clazz, method, codeAttribute);
            }
        }
    }
 
Example 14
Source File: CodeAttributeEditor.java    From java-n-IDE-for-Android with Apache License 2.0 4 votes vote down vote up
public void visitCodeAttribute0(Clazz clazz, Method method, CodeAttribute codeAttribute)
{
    if (DEBUG)
    {
        System.out.println("CodeAttributeEditor: "+clazz.getName()+"."+method.getName(clazz)+method.getDescriptor(clazz));
    }

    // Do we have to update the code?
    if (modified)
    {
        // Can we perform a faster simple replacement of instructions?
        if (canPerformSimpleReplacements(codeAttribute))
        {
            if (DEBUG)
            {
                System.out.println("  Simple editing");
            }

            // Simply overwrite the instructions.
            performSimpleReplacements(codeAttribute);
        }
        else
        {
            if (DEBUG)
            {
                System.out.println("  Full editing");
            }

            // Move and remap the instructions.
            codeAttribute.u4codeLength =
                updateInstructions(clazz, method, codeAttribute);

            // Remap the exception table.
            codeAttribute.exceptionsAccept(clazz, method, this);

            // Remove exceptions with empty code blocks.
            codeAttribute.u2exceptionTableLength =
                removeEmptyExceptions(codeAttribute.exceptionTable,
                                      codeAttribute.u2exceptionTableLength);

            // Remap the line number table and the local variable tables.
            codeAttribute.attributesAccept(clazz, method, this);
        }

        // Make sure instructions are widened if necessary.
        instructionWriter.visitCodeAttribute(clazz, method, codeAttribute);
    }

    // Update the maximum stack size and local variable frame size.
    if (updateFrameSizes)
    {
        stackSizeUpdater.visitCodeAttribute(clazz, method, codeAttribute);
        variableSizeUpdater.visitCodeAttribute(clazz, method, codeAttribute);
    }
}
 
Example 15
Source File: CodeSubroutineInliner.java    From java-n-IDE-for-Android with Apache License 2.0 4 votes vote down vote up
public void visitCodeAttribute0(Clazz clazz, Method method, CodeAttribute codeAttribute)
{
    branchTargetFinder.visitCodeAttribute(clazz, method, codeAttribute);

    // Don't bother if there aren't any subroutines anyway.
    if (!containsSubroutines(codeAttribute))
    {
        return;
    }

    if (DEBUG)
    {
        System.out.println("SubroutineInliner: processing ["+clazz.getName()+"."+method.getName(clazz)+method.getDescriptor(clazz)+"]");
    }

    // Append the body of the code.
    codeAttributeComposer.reset();
    codeAttributeComposer.beginCodeFragment(codeAttribute.u4codeLength);

    // Copy the non-subroutine instructions.
    int offset  = 0;
    while (offset < codeAttribute.u4codeLength)
    {
        Instruction instruction = InstructionFactory.create(codeAttribute.code, offset);
        int instructionLength = instruction.length(offset);

        // Is this returning subroutine?
        if (branchTargetFinder.isSubroutine(offset) &&
            branchTargetFinder.isSubroutineReturning(offset))
        {
            // Skip the subroutine.
            if (DEBUG)
            {
                System.out.println("  Skipping original subroutine instruction "+instruction.toString(offset));
            }

            // Append a label at this offset instead.
            codeAttributeComposer.appendLabel(offset);
        }
        else
        {
            // Copy the instruction, inlining any subroutine call recursively.
            instruction.accept(clazz, method, codeAttribute, offset, this);
        }

        offset += instructionLength;
    }

    // Copy the exceptions. Note that exceptions with empty try blocks
    // are automatically removed.
    codeAttribute.exceptionsAccept(clazz,
                                   method,
                                   subroutineExceptionInliner);

    if (DEBUG)
    {
        System.out.println("  Appending label after code at ["+offset+"]");
    }

    // Append a label just after the code.
    codeAttributeComposer.appendLabel(codeAttribute.u4codeLength);

    // End and update the code attribute.
    codeAttributeComposer.endCodeFragment();
    codeAttributeComposer.visitCodeAttribute(clazz, method, codeAttribute);
}
 
Example 16
Source File: AllExceptionInfoVisitor.java    From java-n-IDE-for-Android with Apache License 2.0 4 votes vote down vote up
public void visitCodeAttribute(Clazz clazz, Method method, CodeAttribute codeAttribute)
{
    codeAttribute.exceptionsAccept(clazz, method, exceptionInfoVisitor);
}
 
Example 17
Source File: CodeSubroutineInliner.java    From java-n-IDE-for-Android with Apache License 2.0 4 votes vote down vote up
/**
 * Appends the specified subroutine.
 */
private void inlineSubroutine(Clazz         clazz,
                              Method        method,
                              CodeAttribute codeAttribute,
                              int           subroutineInvocationOffset,
                              int           subroutineStart)
{
    int subroutineEnd = branchTargetFinder.subroutineEnd(subroutineStart);

    if (DEBUG)
    {
        System.out.println("  Inlining subroutine ["+subroutineStart+" -> "+subroutineEnd+"] at ["+subroutineInvocationOffset+"]");
    }

    // Don't go inlining exceptions that are already applicable to this
    // subroutine invocation.
    ExceptionInfoVisitor oldSubroutineExceptionInliner = subroutineExceptionInliner;
    int                  oldClipStart                  = clipStart;
    int                  oldClipEnd                    = clipEnd;

    subroutineExceptionInliner =
        new ExceptionExcludedOffsetFilter(subroutineInvocationOffset,
                                          subroutineExceptionInliner);
    clipStart = subroutineStart;
    clipEnd   = subroutineEnd;

    codeAttributeComposer.beginCodeFragment(codeAttribute.u4codeLength);

    // Copy the subroutine instructions, inlining any subroutine calls
    // recursively.
    codeAttribute.instructionsAccept(clazz,
                                     method,
                                     subroutineStart,
                                     subroutineEnd,
                                     this);

    if (DEBUG)
    {
        System.out.println("    Appending label after inlined subroutine at ["+subroutineEnd+"]");
    }

    // Append a label just after the code.
    codeAttributeComposer.appendLabel(subroutineEnd);

    // Inline the subroutine exceptions.
    codeAttribute.exceptionsAccept(clazz,
                                   method,
                                   subroutineStart,
                                   subroutineEnd,
                                   subroutineExceptionInliner);

    // We can again inline exceptions that are applicable to this
    // subroutine invocation.
    subroutineExceptionInliner = oldSubroutineExceptionInliner;
    clipStart                  = oldClipStart;
    clipEnd                    = oldClipEnd;

    codeAttributeComposer.endCodeFragment();
}