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

The following examples show how to use proguard.classfile.attribute.CodeAttribute#attributesAccept() . 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: 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 2
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 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: 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 5
Source File: Utf8UsageMarker.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)
{
    markCpUtf8Entry(clazz, codeAttribute.u2attributeNameIndex);

    // Mark the UTF-8 entries referenced by the attributes.
    codeAttribute.attributesAccept(clazz, method, this);
}
 
Example 6
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 7
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 8
Source File: MemberReferenceFixer.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)
{
    // Recompute the maximum stack size if necessary.
    if (stackSizesMayHaveChanged)
    {
            stackSizeUpdater.visitCodeAttribute(clazz, method, codeAttribute);
    }

    // Fix the nested attributes.
    codeAttribute.attributesAccept(clazz, method, this);
}
 
Example 9
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 10
Source File: TargetClassChanger.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)
{
    // Change the references of the attributes.
    codeAttribute.attributesAccept(clazz, method, this);
}
 
Example 11
Source File: VariableOptimizer.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)
    {
//        DEBUG =
//            clazz.getName().equals("abc/Def") &&
//            method.getName(clazz).equals("abc");

        // Initialize the global arrays.
        initializeArrays(codeAttribute);

        // Analyze the liveness of the variables in the code.
        livenessAnalyzer.visitCodeAttribute(clazz, method, codeAttribute);

        // Trim the variables in the local variable tables, because even
        // clipping the tables individually may leave some inconsistencies
        // between them.
            codeAttribute.attributesAccept(clazz, method, this);

        int startIndex =
            (method.getAccessFlags() & ClassConstants.INTERNAL_ACC_STATIC) != 0 ||
            reuseThis ? 0 : 1;

        int parameterSize =
            ClassUtil.internalMethodParameterSize(method.getDescriptor(clazz),
                                                  method.getAccessFlags());

        int variableSize = codeAttribute.u2maxLocals;
        int codeLength   = codeAttribute.u4codeLength;

        boolean remapping = false;

        // Loop over all variables.
        for (int oldIndex = 0; oldIndex < variableSize; oldIndex++)
        {
            // By default, the variable will be mapped onto itself.
            variableMap[oldIndex] = oldIndex;

            // Only try remapping the variable if it's not a parameter.
            if (oldIndex >= parameterSize &&
                oldIndex < MAX_VARIABLES_SIZE)
            {
                // Try to remap the variable to a variable with a smaller index.
                for (int newIndex = startIndex; newIndex < oldIndex; newIndex++)
                {
                    if (areNonOverlapping(oldIndex, newIndex, codeLength))
                    {
                        variableMap[oldIndex] = newIndex;

                        updateLiveness(oldIndex, newIndex, codeLength);

                        remapping = true;

                        // This variable has been remapped. Go to the next one.
                        break;
                    }
                }
            }
        }

        // Have we been able to remap any variables?
        if (remapping)
        {
            if (DEBUG)
            {
                System.out.println("VariableOptimizer: "+clazz.getName()+"."+method.getName(clazz)+method.getDescriptor(clazz));
                for (int index= 0; index < variableSize; index++)
                {
                    System.out.println("  v"+index+" -> "+variableMap[index]);
                }
            }

            // Remap the variables.
            variableRemapper.setVariableMap(variableMap);
            variableRemapper.visitCodeAttribute(clazz, method, codeAttribute);

            // Visit the method, if required.
            if (extraVariableMemberVisitor != null)
            {
                method.accept(clazz, extraVariableMemberVisitor);
            }
        }
        else
        {
            // Just clean up any empty variables.
            variableCleaner.visitCodeAttribute(clazz, method, codeAttribute);
        }
    }
 
Example 12
Source File: ClassReferenceFixer.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)
{
    // Fix the attributes.
    codeAttribute.attributesAccept(clazz, method, this);
}
 
Example 13
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 14
Source File: CodeAttributeComposer.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)
    {
        if (DEBUG)
        {
            System.out.println("CodeAttributeComposer: putting results in ["+clazz.getName()+"."+method.getName(clazz)+method.getDescriptor(clazz)+"]");
        }

        if (level != -1)
        {
            throw new IllegalArgumentException("Code fragment not ended ["+level+"]");
        }

        level++;

        // Make sure the code attribute has sufficient space for the composed
        // code.
        if (codeAttribute.u4codeLength < codeLength)
        {
            codeAttribute.code = new byte[codeLength];
        }

        // Copy the composed code over into the code attribute.
        codeAttribute.u4codeLength = codeLength;
        System.arraycopy(code, 0, codeAttribute.code, 0, codeLength);

        // Remove exceptions with empty code blocks (done before).
        //exceptionTableLength =
        //    removeEmptyExceptions(exceptionTable, exceptionTableLength);

        // Make sure the exception table has sufficient space for the composed
        // exceptions.
        if (codeAttribute.exceptionTable.length < exceptionTableLength)
        {
            codeAttribute.exceptionTable = new ExceptionInfo[exceptionTableLength];
        }

        // Copy the exception table.
        codeAttribute.u2exceptionTableLength = exceptionTableLength;
        System.arraycopy(exceptionTable, 0, codeAttribute.exceptionTable, 0, exceptionTableLength);

        // Update the maximum stack size and local variable frame size.
        stackSizeUpdater.visitCodeAttribute(clazz, method, codeAttribute);
        variableSizeUpdater.visitCodeAttribute(clazz, method, codeAttribute);

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

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

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

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

        level--;
    }
 
Example 15
Source File: ClassReferenceInitializer.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)
{
    // Initialize the nested attributes.
    codeAttribute.attributesAccept(clazz, method, this);
}
 
Example 16
Source File: ReferencedClassVisitor.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)
{
    // Visit the attributes of the code attribute.
    codeAttribute.attributesAccept(clazz, method, this);
}
 
Example 17
Source File: AllAttributeVisitor.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.attributesAccept(clazz, method, attributeVisitor);
}