proguard.classfile.attribute.CodeAttribute Java Examples

The following examples show how to use proguard.classfile.attribute.CodeAttribute. 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: ProgramMember.java    From java-n-IDE-for-Android with 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 #2
Source File: SideEffectInstructionChecker.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)
{
    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 #3
Source File: BranchTargetFinder.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)
{
    // 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 #4
Source File: LocalVariableTypeInfoAdder.java    From java-n-IDE-for-Android with Apache License 2.0 6 votes vote down vote up
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 #5
Source File: DuplicateInitializerFixer.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)
{
    // 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 #6
Source File: SideEffectInstructionChecker.java    From proguard with GNU General Public License v2.0 5 votes vote down vote up
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
Source File: AttributeShrinker.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)
{
    // Compact the attributes array.
    codeAttribute.u2attributesCount =
        shrinkArray(codeAttribute.attributes,
                    codeAttribute.u2attributesCount);
}
 
Example #8
Source File: UsageMarker.java    From java-n-IDE-for-Android with 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 #9
Source File: FullFrame.java    From java-n-IDE-for-Android with 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 #10
Source File: EvaluationSimplifier.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
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 #11
Source File: StackMapAttribute.java    From java-n-IDE-for-Android with 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 #12
Source File: ExceptionInstructionChecker.java    From proguard with GNU General Public License v2.0 5 votes vote down vote up
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 #13
Source File: NonEmptyAttributeFilter.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
public void visitLineNumberTableAttribute(Clazz clazz, Method method, CodeAttribute codeAttribute, LineNumberTableAttribute lineNumberTableAttribute)
{
    if (lineNumberTableAttribute.u2lineNumberTableLength > 0)
    {
        lineNumberTableAttribute.accept(clazz, method, codeAttribute, attributeVisitor);
    }
}
 
Example #14
Source File: ReadWriteFieldMarker.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)
{
    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 #15
Source File: EvaluationSimplifier.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)
{
    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 #16
Source File: RequiredAttributeFilter.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)
{
    if (requiredAttributeVisitor != null)
    {
        codeAttribute.accept(clazz, method, requiredAttributeVisitor);
    }
}
 
Example #17
Source File: BasicBranchUnit.java    From bazel with 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 #18
Source File: ProgramClassWriter.java    From java-n-IDE-for-Android with 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 #19
Source File: ExceptionInstructionChecker.java    From proguard with GNU General Public License v2.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)
    {
        return instruction.mayThrowExceptions();

//        mayThrowExceptions = false;
//
//        instruction.accept(clazz, method,  codeAttribute, offset, this);
//
//        return mayThrowExceptions;
    }
 
Example #20
Source File: ProgramClassWriter.java    From java-n-IDE-for-Android with 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 #21
Source File: CodeAttributeEditor.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
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 #22
Source File: GsonInstrumentationAdder.java    From proguard with GNU General Public License v2.0 5 votes vote down vote up
@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 #23
Source File: InstructionSequenceReplacer.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)
{
    replacementInstruction =
        new ConstantInstruction(constantInstruction.opcode,
                                instructionSequenceMatcher.matchedConstantIndex(constantInstruction.constantIndex),
                                instructionSequenceMatcher.matchedArgument(constantInstruction.constant));
}
 
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 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 #25
Source File: EnumFieldReferenceInitializer.java    From proguard with GNU General Public License v2.0 5 votes vote down vote up
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 #26
Source File: MethodInliner.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)
{
    // 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 #27
Source File: ExceptionInstructionChecker.java    From bazel with 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 #28
Source File: ClassDetailVisitor.java    From atlas with 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 #29
Source File: TopType.java    From proguard with GNU General Public License v2.0 4 votes vote down vote up
public void variablesAccept(Clazz clazz, Method method, CodeAttribute codeAttribute, int instructionOffset, int variableIndex, VerificationTypeVisitor verificationTypeVisitor)
{
    verificationTypeVisitor.visitVariablesTopType(clazz, method, codeAttribute, instructionOffset, variableIndex, this);
}
 
Example #30
Source File: VariableInstruction.java    From proguard with GNU General Public License v2.0 4 votes vote down vote up
public void accept(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, InstructionVisitor instructionVisitor)
{
    instructionVisitor.visitVariableInstruction(clazz, method, codeAttribute, offset, this);
}