Java Code Examples for proguard.classfile.attribute.CodeAttribute

The following examples show how to use proguard.classfile.attribute.CodeAttribute. These examples are extracted from open source projects. 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
public void visitConstantInstruction(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, ConstantInstruction constantInstruction)
{
    byte opcode = constantInstruction.opcode;
    // Check for instructions that might cause side effects.
    if (opcode == InstructionConstants.OP_GETSTATIC     ||
        opcode == InstructionConstants.OP_PUTSTATIC     ||
        opcode == InstructionConstants.OP_GETFIELD      ||
        opcode == InstructionConstants.OP_PUTFIELD      ||
        opcode == InstructionConstants.OP_INVOKEVIRTUAL ||
        opcode == InstructionConstants.OP_INVOKESPECIAL ||
        opcode == InstructionConstants.OP_INVOKESTATIC  ||
        opcode == InstructionConstants.OP_INVOKEINTERFACE)
    {
        // Check if the field is write-only or volatile, or if the invoked
        // method is causing any side effects.
        clazz.constantPoolEntryAccept(constantInstruction.constantIndex, this);
    }
}
 
Example 2
Source Project: java-n-IDE-for-Android   Source File: ProgramMember.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Returns the line number range of the given class member as "m:n",
 * if it can find it, or <code>null</code> otherwise.
 */
public String getLineNumberRange(Clazz clazz)
{
    CodeAttribute codeAttribute =
        (CodeAttribute)getAttribute(clazz, ClassConstants.ATTR_Code);
    if (codeAttribute  == null)
    {
        return null;
    }

    LineNumberTableAttribute lineNumberTableAttribute =
        (LineNumberTableAttribute)codeAttribute.getAttribute(clazz,
                                                             ClassConstants.ATTR_LineNumberTable);
    if (lineNumberTableAttribute  == null)
    {
        return null;
    }

    return "" +
           lineNumberTableAttribute.getLineNumber(0) +
           ":" +
           lineNumberTableAttribute.getLineNumber(Integer.MAX_VALUE);
}
 
Example 3
public void visitLocalVariableTypeInfo(Clazz clazz, Method method, CodeAttribute codeAttribute, LocalVariableTypeInfo localVariableTypeInfo)
{
    // Create a new line number.
    LocalVariableTypeInfo newLocalVariableTypeInfo =
        new LocalVariableTypeInfo(localVariableTypeInfo.u2startPC,
                                  localVariableTypeInfo.u2length,
                                  constantAdder.addConstant(clazz, localVariableTypeInfo.u2nameIndex),
                                  constantAdder.addConstant(clazz, localVariableTypeInfo.u2signatureIndex),
                                  localVariableTypeInfo.u2index);

    // TODO: Clone array.
    newLocalVariableTypeInfo.referencedClasses = localVariableTypeInfo.referencedClasses;

    // Add it to the target.
    localVariableTypeTableAttributeEditor.addLocalVariableTypeInfo(newLocalVariableTypeInfo);
}
 
Example 4
Source Project: java-n-IDE-for-Android   Source File: BranchTargetFinder.java    License: Apache License 2.0 6 votes vote down vote up
public void visitSimpleInstruction(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, SimpleInstruction simpleInstruction)
{
    // Mark the instruction.
    instructionMarks[offset] |= INSTRUCTION;

    // Check if this is the first instruction of a subroutine.
    checkSubroutine(offset);

    byte opcode = simpleInstruction.opcode;
    if (opcode == InstructionConstants.OP_IRETURN ||
        opcode == InstructionConstants.OP_LRETURN ||
        opcode == InstructionConstants.OP_FRETURN ||
        opcode == InstructionConstants.OP_DRETURN ||
        opcode == InstructionConstants.OP_ARETURN ||
        opcode == InstructionConstants.OP_ATHROW)
    {
        // Mark the branch origin.
        markBranchOrigin(offset);

        // Mark the next instruction.
        markAfterBranchOrigin(offset + simpleInstruction.length(offset));
    }
}
 
Example 5
Source Project: java-n-IDE-for-Android   Source File: FullFrame.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Applies the given verification type visitor to all variables.
 */
public void variablesAccept(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, VerificationTypeVisitor verificationTypeVisitor)
{
    for (int index = 0; index < variablesCount; index++)
    {
        variables[index].variablesAccept(clazz, method, codeAttribute, offset, index, verificationTypeVisitor);
    }
}
 
Example 6
public void visitVariableInstruction(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, VariableInstruction variableInstruction)
{
    byte opcode = variableInstruction.opcode;

    // Check for instructions that might cause side effects.
    switch (opcode)
    {
        case Instruction.OP_RET:
            // This instruction may have a side effect.
            hasSideEffects = includeReturnInstructions;
            break;
    }
}
 
Example 7
public void visitSimpleInstruction(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, SimpleInstruction simpleInstruction)
{
    // Check for instructions that may throw exceptions.
    // Note that monitorexit can not sensibly throw exceptions, except the
    // broken and deprecated asynchronous ThreadDeath. Removing the
    // artificial infinite looping exception blocks that recent compilers
    // add does not strictly follow the JVM specs, but it does have the
    // additional benefit of avoiding a bug in the JVM in JDK 1.1.
    switch (simpleInstruction.opcode)
    {
        case InstructionConstants.OP_IDIV:
        case InstructionConstants.OP_LDIV:
        case InstructionConstants.OP_IREM:
        case InstructionConstants.OP_LREM:
        case InstructionConstants.OP_IALOAD:
        case InstructionConstants.OP_LALOAD:
        case InstructionConstants.OP_FALOAD:
        case InstructionConstants.OP_DALOAD:
        case InstructionConstants.OP_AALOAD:
        case InstructionConstants.OP_BALOAD:
        case InstructionConstants.OP_CALOAD:
        case InstructionConstants.OP_SALOAD:
        case InstructionConstants.OP_IASTORE:
        case InstructionConstants.OP_LASTORE:
        case InstructionConstants.OP_FASTORE:
        case InstructionConstants.OP_DASTORE:
        case InstructionConstants.OP_AASTORE:
        case InstructionConstants.OP_BASTORE:
        case InstructionConstants.OP_CASTORE:
        case InstructionConstants.OP_SASTORE:
        case InstructionConstants.OP_NEWARRAY:
        case InstructionConstants.OP_ARRAYLENGTH:
        case InstructionConstants.OP_ATHROW:
        case InstructionConstants.OP_MONITORENTER:
            // These instructions may throw exceptions.
            mayThrowExceptions = true;
    }
}
 
Example 8
public void visitLineNumberTableAttribute(Clazz clazz, Method method, CodeAttribute codeAttribute, LineNumberTableAttribute lineNumberTableAttribute)
{
    if (lineNumberTableAttribute.u2lineNumberTableLength > 0)
    {
        lineNumberTableAttribute.accept(clazz, method, codeAttribute, attributeVisitor);
    }
}
 
Example 9
public void visitConstantInstruction(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, ConstantInstruction constantInstruction)
{
    byte opcode = constantInstruction.opcode;

    // Check for instructions that involve fields.
    switch (opcode)
    {
        case InstructionConstants.OP_LDC:
        case InstructionConstants.OP_LDC_W:
            // Mark the field, if any, as being read from and written to.
            reading = true;
            writing = true;
            clazz.constantPoolEntryAccept(constantInstruction.constantIndex, this);
            break;

        case InstructionConstants.OP_GETSTATIC:
        case InstructionConstants.OP_GETFIELD:
            // Mark the field as being read from.
            reading = true;
            writing = false;
            clazz.constantPoolEntryAccept(constantInstruction.constantIndex, this);
            break;

        case InstructionConstants.OP_PUTSTATIC:
        case InstructionConstants.OP_PUTFIELD:
            // Mark the field as being written to.
            reading = false;
            writing = true;
            clazz.constantPoolEntryAccept(constantInstruction.constantIndex, this);
            break;
    }
}
 
Example 10
public void visitConstantInstruction(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, ConstantInstruction constantInstruction)
{
    switch (constantInstruction.opcode)
    {
        case InstructionConstants.OP_GETSTATIC:
        case InstructionConstants.OP_GETFIELD:
            replaceAnyPushInstruction(clazz, offset, constantInstruction);
            break;

        case InstructionConstants.OP_INVOKEVIRTUAL:
        case InstructionConstants.OP_INVOKESPECIAL:
        case InstructionConstants.OP_INVOKESTATIC:
        case InstructionConstants.OP_INVOKEINTERFACE:
            if (constantInstruction.stackPushCount(clazz) > 0 &&
                !sideEffectInstructionChecker.hasSideEffects(clazz,
                                                             method,
                                                             codeAttribute,
                                                             offset,
                                                             constantInstruction))
            {
                replaceAnyPushInstruction(clazz, offset, constantInstruction);
            }

            break;

        case InstructionConstants.OP_CHECKCAST:
            replaceReferencePushInstruction(clazz, offset, constantInstruction);
            break;
    }
}
 
Example 11
public void visitCodeAttribute(Clazz clazz, Method method, CodeAttribute codeAttribute)
{
    if (requiredAttributeVisitor != null)
    {
        codeAttribute.accept(clazz, method, requiredAttributeVisitor);
    }
}
 
Example 12
/**
     * Returns whether the given instruction may throw exceptions.
     */
    public boolean mayThrowExceptions(Clazz         clazz,
                                      Method        method,
                                      CodeAttribute codeAttribute,
                                      int           offset,
                                      Instruction   instruction)
    {
        return instruction.mayThrowExceptions();

//        mayThrowExceptions = false;
//
//        instruction.accept(clazz, method,  codeAttribute, offset, this);
//
//        return mayThrowExceptions;
    }
 
Example 13
Source Project: java-n-IDE-for-Android   Source File: ProgramClassWriter.java    License: Apache License 2.0 5 votes vote down vote up
public void visitFullFrame(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, FullFrame fullFrame)
{
    dataOutput.writeShort(fullFrame.u2offsetDelta);

    // Write the verification types of the local variables.
    dataOutput.writeShort(fullFrame.variablesCount);
    fullFrame.variablesAccept(clazz, method, codeAttribute, offset, this);

    // Write the verification types of the stack entries.
    dataOutput.writeShort(fullFrame.stackCount);
    fullFrame.stackAccept(clazz, method, codeAttribute, offset, this);
}
 
Example 14
public void visitLineNumberTableAttribute(Clazz clazz, Method method, CodeAttribute codeAttribute, LineNumberTableAttribute lineNumberTableAttribute)
{
    // Remap all line number table entries.
    lineNumberTableAttribute.lineNumbersAccept(clazz, method, codeAttribute, this);

    // Remove line numbers with empty code blocks.
    lineNumberTableAttribute.u2lineNumberTableLength =
       removeEmptyLineNumbers(lineNumberTableAttribute.lineNumberTable,
                              lineNumberTableAttribute.u2lineNumberTableLength,
                              codeAttribute.u4codeLength);
}
 
Example 15
@Override
public void visitAnyInstruction(Clazz         clazz,
                                Method        method,
                                CodeAttribute codeAttribute,
                                int           offset,
                                Instruction   instruction)
{
    if (instruction.actualOpcode() == OP_RETURN ||
        instruction.actualOpcode() == OP_ARETURN)
    {
        String fullyQualifiedMethodName = clazz.getName() + "#" +
                                          method.getName(clazz) + method.getDescriptor(clazz);
        if (DEBUG)
        {
            System.out.println("GsonInstrumentationAdder: instrumenting " +
                               fullyQualifiedMethodName);
        }

        InstructionSequenceBuilder ____ = new InstructionSequenceBuilder((ProgramClass)clazz,
                                                                         programClassPool,
                                                                         libraryClassPool);
        ____.ldc("Type token cache after invoking " + fullyQualifiedMethodName + ":")
            .aload_0()
            .getfield(clazz.getName(),
                      GsonClassConstants.FIELD_NAME_TYPE_TOKEN_CACHE,
                      GsonClassConstants.FIELD_TYPE_TYPE_TOKEN_CACHE)
            .invokestatic(OptimizedClassConstants.NAME_GSON_UTIL,
                          OptimizedClassConstants.METHOD_NAME_DUMP_TYPE_TOKEN_CACHE,
                          OptimizedClassConstants.METHOD_TYPE_DUMP_TYPE_TOKEN_CACHE);

        codeAttributeEditor.insertBeforeInstruction(offset, ____.instructions());
    }
}
 
Example 16
public void visitConstantInstruction(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, ConstantInstruction constantInstruction)
{
    replacementInstruction =
        new ConstantInstruction(constantInstruction.opcode,
                                instructionSequenceMatcher.matchedConstantIndex(constantInstruction.constantIndex),
                                instructionSequenceMatcher.matchedArgument(constantInstruction.constant));
}
 
Example 17
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 18
public void visitConstantInstruction(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, ConstantInstruction constantInstruction)
{
    switch (constantInstruction.opcode)
    {
        case InstructionConstants.OP_LDC:
        case InstructionConstants.OP_LDC_W:
        case InstructionConstants.OP_PUTSTATIC:
            clazz.constantPoolEntryAccept(constantInstruction.constantIndex, this);
            break;
    }
}
 
Example 19
Source Project: java-n-IDE-for-Android   Source File: MethodInliner.java    License: Apache License 2.0 5 votes vote down vote up
public void visitCodeAttribute(Clazz clazz, Method method, CodeAttribute codeAttribute)
{
    // TODO: Remove this when the method inliner has stabilized.
    // Catch any unexpected exceptions from the actual visiting method.
    try
    {
        // Process the code.
        visitCodeAttribute0(clazz, method, codeAttribute);
    }
    catch (RuntimeException ex)
    {
        System.err.println("Unexpected error while inlining method:");
        System.err.println("  Target class   = ["+targetClass.getName()+"]");
        System.err.println("  Target method  = ["+targetMethod.getName(targetClass)+targetMethod.getDescriptor(targetClass)+"]");
        if (inlining)
        {
            System.err.println("  Inlined class  = ["+clazz.getName()+"]");
            System.err.println("  Inlined method = ["+method.getName(clazz)+method.getDescriptor(clazz)+"]");
        }
        System.err.println("  Exception      = ["+ex.getClass().getName()+"] ("+ex.getMessage()+")");
        System.err.println("Not inlining this method");

        if (DEBUG)
        {
            targetMethod.accept(targetClass, new ClassPrinter());
            if (inlining)
            {
                method.accept(clazz, new ClassPrinter());
            }

            throw ex;
        }
    }
}
 
Example 20
public void visitCodeAttribute(Clazz clazz, Method method, CodeAttribute codeAttribute)
{
    // The minimum variable size is determined by the arguments.
    int maxLocals =
        ClassUtil.internalMethodParameterSize(method.getDescriptor(clazz),
                                              method.getAccessFlags());

    if (codeAttribute.u2maxLocals < maxLocals)
    {
        codeAttribute.u2maxLocals = maxLocals;
    }
}
 
Example 21
Source Project: bazel   Source File: ExceptionInstructionChecker.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Returns whether the specified instruction may throw exceptions.
 */
public boolean mayThrowExceptions(Clazz         clazz,
                                  Method        method,
                                  CodeAttribute codeAttribute,
                                  int           offset)
{
    Instruction instruction = InstructionFactory.create(codeAttribute.code, offset);

    return mayThrowExceptions(clazz,
                              method,
                              codeAttribute,
                              offset,
                              instruction);
}
 
Example 22
Source Project: java-n-IDE-for-Android   Source File: ProgramClassWriter.java    License: Apache License 2.0 5 votes vote down vote up
public void visitSameZeroFrame(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, SameZeroFrame sameZeroFrame)
{
    if (sameZeroFrame.getTag() == StackMapFrame.SAME_ZERO_FRAME_EXTENDED)
    {
        dataOutput.writeShort(sameZeroFrame.u2offsetDelta);
    }
}
 
Example 23
Source Project: bazel   Source File: BasicBranchUnit.java    License: Apache License 2.0 5 votes vote down vote up
public void branchConditionally(Clazz         clazz,
                                CodeAttribute codeAttribute,
                                int           offset,
                                int           branchTarget,
                                int           conditional)
{
    // Accumulate the branch targets.
    traceBranchTargets =
        traceBranchTargets.generalize(new InstructionOffsetValue(branchTarget)).instructionOffsetValue();

    wasCalled = true;
}
 
Example 24
Source Project: atlas   Source File: ClassDetailVisitor.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void visitConstantInstruction(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset,
                                     ConstantInstruction constantInstruction) {
    //println("visitConstantInstruction >>>" + constantInstruction.toString(offset));

    //indent();
    clazz.constantPoolEntryAccept(constantInstruction.constantIndex, this);
    //outdent();
}
 
Example 25
Source Project: java-n-IDE-for-Android   Source File: AttributeShrinker.java    License: Apache License 2.0 5 votes vote down vote up
public void visitCodeAttribute(Clazz clazz, Method method, CodeAttribute codeAttribute)
{
    // Compact the attributes array.
    codeAttribute.u2attributesCount =
        shrinkArray(codeAttribute.attributes,
                    codeAttribute.u2attributesCount);
}
 
Example 26
public void visitAnySwitchInstruction(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, SwitchInstruction switchInstruction)
{
    // First try to simplify it to a simple branch.
    replaceBranchInstruction(clazz, offset, switchInstruction);

    // Otherwise make sure all branch targets are valid.
    if (!codeAttributeEditor.isModified(offset))
    {
        replaceSwitchInstruction(clazz, offset, switchInstruction);
    }
}
 
Example 27
Source Project: java-n-IDE-for-Android   Source File: StackMapAttribute.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Applies the given stack map frame visitor to all stack map frames.
 */
public void stackMapFramesAccept(Clazz clazz, Method method, CodeAttribute codeAttribute, StackMapFrameVisitor stackMapFrameVisitor)
{
    for (int index = 0; index < u2stackMapFramesCount; index++)
    {
        FullFrame stackMapFrame = stackMapFrames[index];

        // We don't need double dispatching here, since there is only one
        // type of StackMapFrame.
        stackMapFrameVisitor.visitFullFrame(clazz, method, codeAttribute, stackMapFrame.getOffsetDelta(), stackMapFrame);
    }
}
 
Example 28
Source Project: java-n-IDE-for-Android   Source File: UsageMarker.java    License: Apache License 2.0 5 votes vote down vote up
public void visitExceptionInfo(Clazz clazz, Method method, CodeAttribute codeAttribute, ExceptionInfo exceptionInfo)
{
    markAsUsed(exceptionInfo);

    if (exceptionInfo.u2catchType != 0)
    {
        markConstant(clazz, exceptionInfo.u2catchType);
    }
}
 
Example 29
Source Project: bazel   Source File: LongType.java    License: Apache License 2.0 4 votes vote down vote up
public void stackAccept(Clazz clazz, Method method, CodeAttribute codeAttribute, int instructionOffset, int stackIndex, VerificationTypeVisitor verificationTypeVisitor)
{
    verificationTypeVisitor.visitStackLongType(clazz, method, codeAttribute, instructionOffset, stackIndex, this);
}
 
Example 30
Source Project: bazel   Source File: ObjectType.java    License: Apache License 2.0 4 votes vote down vote up
public void variablesAccept(Clazz clazz, Method method, CodeAttribute codeAttribute, int instructionOffset, int variableIndex, VerificationTypeVisitor verificationTypeVisitor)
{
    verificationTypeVisitor.visitVariablesObjectType(clazz, method, codeAttribute, instructionOffset, variableIndex, this);
}