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

The following examples show how to use proguard.classfile.attribute.CodeAttribute#instructionsAccept() . 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: WrapperClassUseSimplifier.java    From proguard with GNU General Public License v2.0 6 votes vote down vote up
public void visitCodeAttribute(Clazz clazz, Method method, CodeAttribute codeAttribute)
{
    if (DEBUG)
    {
        System.out.println("WrapperClassUseSimplifier: "+clazz.getName()+"."+method.getName(clazz)+method.getDescriptor(clazz));
    }

    int codeLength = codeAttribute.u4codeLength;

    // Reset the code changes.
    codeAttributeEditor.reset(codeLength);

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

    // Apply all accumulated changes to the code.
    codeAttributeEditor.visitCodeAttribute(clazz, method, codeAttribute);
}
 
Example 2
Source File: PeepholeOptimizer.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)
{
    if (branchTargetFinder != null)
    {
        // Set up the branch target finder.
        branchTargetFinder.visitCodeAttribute(clazz, method, codeAttribute);
    }

    // Set up the code attribute editor.
    codeAttributeEditor.reset(codeAttribute.u4codeLength);

    // Find the peephole optimizations.
    codeAttribute.instructionsAccept(clazz, method, instructionVisitor);

    // Apply the peephole optimizations.
    codeAttributeEditor.visitCodeAttribute(clazz, method, codeAttribute);
}
 
Example 3
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 4
Source File: GotoCommonCodeReplacer.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)
    {
//        DEBUG =
//            clazz.getName().equals("abc/Def") &&
//            method.getName(clazz).equals("abc");

        // Mark all branch targets.
        branchTargetFinder.visitCodeAttribute(clazz, method, codeAttribute);

        // Reset the code attribute editor.
        codeAttributeEditor.reset(codeAttribute.u4codeLength);

        // Remap the variables of the instructions.
        codeAttribute.instructionsAccept(clazz, method, this);

        // Apply the code atribute editor.
        codeAttributeEditor.visitCodeAttribute(clazz, method, codeAttribute);
    }
 
Example 5
Source File: DynamicClassReferenceInitializer.java    From java-n-IDE-for-Android with Apache License 2.0 6 votes vote down vote up
/**
 * Returns whether the first whether the first instructions of the
 * given code attribute match with the given instruction matcher.
 */
private boolean isDotClassMethodCode(Clazz                      clazz,
                                     Method                     method,
                                     CodeAttribute codeAttribute,
                                     InstructionSequenceMatcher codeMatcher,
                                     int                        codeLength)
{
    // Check the minimum code length.
    if (codeAttribute.u4codeLength < codeLength)
    {
        return false;
    }

    // Check the actual instructions.
    codeMatcher.reset();
    codeAttribute.instructionsAccept(clazz, method, 0, codeLength, codeMatcher);
    return codeMatcher.isMatching();
}
 
Example 6
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 7
Source File: VariableUsageMarker.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)
{
    int maxLocals = codeAttribute.u2maxLocals;

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

    codeAttribute.instructionsAccept(clazz, method, this);
}
 
Example 8
Source File: VariableRemapper.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)
{
    if (DEBUG)
    {
        System.out.println("VariableRemapper: "+clazz.getName()+"."+method.getName(clazz)+method.getDescriptor(clazz));
        for (int index= 0; index < codeAttribute.u2maxLocals; index++)
        {
            System.out.println("  v"+index+" -> "+variableMap[index]);
        }
    }

    // Remap the variables of the attributes, before editing the code and
    // cleaning up its local variable frame.
    codeAttribute.attributesAccept(clazz, method, this);

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

    // Remap the variables of the instructions.
    codeAttribute.instructionsAccept(clazz, method, this);

    // Apply the code atribute editor.
    codeAttributeEditor.visitCodeAttribute(clazz, method, codeAttribute);
}
 
Example 9
Source File: VariableSizeUpdater.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)
    {
//        DEBUG =
//            clazz.getName().equals("abc/Def") &&
//            method.getName(clazz).equals("abc");

        // The minimum variable size is determined by the arguments.
        codeAttribute.u2maxLocals =
            ClassUtil.internalMethodParameterSize(method.getDescriptor(clazz),
                                                  method.getAccessFlags());

        if (DEBUG)
        {
            System.out.println("VariableSizeUpdater: "+clazz.getName()+"."+method.getName(clazz)+method.getDescriptor(clazz));
            System.out.println("  Max locals: "+codeAttribute.u2maxLocals+" <- parameters");
        }

        // Go over all instructions.
        codeAttribute.instructionsAccept(clazz, method, this);

        // Remove the unused variables of the attributes.
        variableCleaner.visitCodeAttribute(clazz, method, codeAttribute);
    }
 
Example 10
Source File: UnreachableCodeRemover.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("UnreachableCodeRemover: "+clazz.getName()+"."+method.getName(clazz)+method.getDescriptor(clazz));
    }

    reachableCodeMarker.visitCodeAttribute(clazz, method, codeAttribute);

    codeAttributeEditor.reset(codeAttribute.u4codeLength);

    codeAttribute.instructionsAccept(clazz, method, this);

    codeAttributeEditor.visitCodeAttribute(clazz, method, codeAttribute);
}
 
Example 11
Source File: DuplicateInitializerInvocationFixer.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)
{

    // Reset the code changes.
    codeAttributeEditor.reset(codeAttribute.u4codeLength);

    // Fix any duplicate constructor invocations.
    codeAttribute.instructionsAccept(clazz,
                                     method,
                                     this);

    // Apply all accumulated changes to the code.
    codeAttributeEditor.visitCodeAttribute(clazz, method, codeAttribute);
}
 
Example 12
Source File: ParameterUsageMarker.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)
{
    // Evaluate the code.
    partialEvaluator.visitCodeAttribute(clazz, method, codeAttribute);

    // Mark the parameters that are used by the code.
    codeAttribute.instructionsAccept(clazz, method, this);
}
 
Example 13
Source File: MethodInvocationFixer.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)
{
    // Reset the code attribute editor.
    codeAttributeEditor.reset(codeAttribute.u4codeLength);

    // Remap the variables of the instructions.
    codeAttribute.instructionsAccept(clazz, method, this);

    // Apply the code atribute editor.
    codeAttributeEditor.visitCodeAttribute(clazz, method, codeAttribute);
}
 
Example 14
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 15
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 16
Source File: BridgeMethodFixer.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)
{
    // Go over the instructions of the bridge method.
    codeAttribute.instructionsAccept(clazz, method, this);
}
 
Example 17
Source File: MethodInliner.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 (!inlining)
        {
//            codeAttributeComposer.DEBUG = DEBUG =
//                clazz.getName().equals("abc/Def") &&
//                method.getName(clazz).equals("abc");

            targetClass                  = (ProgramClass)clazz;
            targetMethod                 = (ProgramMethod)method;
            constantAdder                = new ConstantAdder(targetClass);
            exceptionInfoAdder           = new ExceptionInfoAdder(targetClass, codeAttributeComposer);
            estimatedResultingCodeLength = codeAttribute.u4codeLength;
            inliningMethods.clear();
            uninitializedObjectCount     = method.getName(clazz).equals(ClassConstants.INTERNAL_METHOD_NAME_INIT) ? 1 : 0;
            inlinedAny                   = false;
            codeAttributeComposer.reset();
            stackSizeComputer.visitCodeAttribute(clazz, method, codeAttribute);

            // Append the body of the code.
            copyCode(clazz, method, codeAttribute);

            targetClass   = null;
            targetMethod  = null;
            constantAdder = null;

            // Update the code attribute if any code has been inlined.
            if (inlinedAny)
            {
                codeAttributeComposer.visitCodeAttribute(clazz, method, codeAttribute);

                // Update the accessing flags.
                codeAttribute.instructionsAccept(clazz, method, accessMethodMarker);

                // Update the exception catching flags.
                catchExceptionMarker.visitCodeAttribute(clazz, method, codeAttribute);
            }
        }

        // Only inline the method if it is invoked once or if it is short.
        else if ((inlineSingleInvocations ?
                      MethodInvocationMarker.getInvocationCount(method) == 1 :
                      codeAttribute.u4codeLength <= MAXIMUM_INLINED_CODE_LENGTH) &&
                 estimatedResultingCodeLength + codeAttribute.u4codeLength <
                 (microEdition ?
                     MAXIMUM_RESULTING_CODE_LENGTH_JME :
                     MAXIMUM_RESULTING_CODE_LENGTH_JSE))
        {
            if (DEBUG)
            {
                System.out.println("MethodInliner: inlining ["+
                                   clazz.getName()+"."+method.getName(clazz)+method.getDescriptor(clazz)+"] in ["+
                                   targetClass.getName()+"."+targetMethod.getName(targetClass)+targetMethod.getDescriptor(targetClass)+"]");
            }

            // Ignore the removal of the original method invocation,
            // the addition of the parameter setup, and
            // the modification of a few inlined instructions.
            estimatedResultingCodeLength += codeAttribute.u4codeLength;

            // Append instructions to store the parameters.
            storeParameters(clazz, method);

            // Inline the body of the code.
            copyCode(clazz, method, codeAttribute);

            inlined    = true;
            inlinedAny = true;
        }
    }
 
Example 18
Source File: AllInstructionVisitor.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.instructionsAccept(clazz, method, instructionVisitor);
}
 
Example 19
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();
}
 
Example 20
Source File: ClassDetailVisitor.java    From atlas with Apache License 2.0 3 votes vote down vote up
@Override
public void visitCodeAttribute(Clazz clazz, Method method, CodeAttribute codeAttribute) {

    //println("visitCodeAttribute >>>>>");

    codeAttribute.instructionsAccept(clazz, method, this);
}