proguard.classfile.instruction.ConstantInstruction Java Examples

The following examples show how to use proguard.classfile.instruction.ConstantInstruction. 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: BridgeMethodFixer.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)
{
    switch (constantInstruction.opcode)
    {
        case InstructionConstants.OP_INVOKEVIRTUAL:
        case InstructionConstants.OP_INVOKESPECIAL:
        case InstructionConstants.OP_INVOKESTATIC:
        case InstructionConstants.OP_INVOKEINTERFACE:
            // Get the name of the bridged method.
            clazz.constantPoolEntryAccept(constantInstruction.constantIndex, this);

            // Check if the name is different.
            if (!method.getName(clazz).equals(bridgedMethodName))
            {
                if (DEBUG)
                {
                    System.out.println("BridgeMethodFixer: ["+clazz.getName()+"."+method.getName(clazz)+method.getDescriptor(clazz)+"] does not bridge to ["+bridgedMethodName+"]");
                }

                // Clear the bridge flag.
                ((ProgramMethod)method).u2accessFlags &= ~ClassConstants.INTERNAL_ACC_BRIDGE;
            }
            break;
    }
}
 
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: MethodInvocationFixer.java    From java-n-IDE-for-Android with Apache License 2.0 6 votes vote down vote up
private void debug(Clazz clazz,
                   Method method,
                   int                 offset,
                   ConstantInstruction constantInstruction,
                   Instruction replacementInstruction)
{
    System.out.println("MethodInvocationFixer:");
    System.out.println("  Class       = "+clazz.getName());
    System.out.println("  Method      = "+method.getName(clazz)+method.getDescriptor(clazz));
    System.out.println("  Instruction = "+constantInstruction.toString(offset));
    System.out.println("  -> Class    = "+referencedClass);
    System.out.println("     Method   = "+referencedMethod);
    if ((referencedClass.getAccessFlags() & ClassConstants.INTERNAL_ACC_INTERFACE) != 0)
    {
        System.out.println("     Parameter size   = "+(ClassUtil.internalMethodParameterSize(referencedMethod.getDescriptor(referencedMethodClass), false)));
    }
    System.out.println("  Replacement instruction = "+replacementInstruction.toString(offset));
}
 
Example #4
Source File: InstructionWriter.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)
{
    try
    {
        // Try to write out the instruction.
        constantInstruction.write(codeAttribute, offset);
    }
    catch (IllegalArgumentException exception)
    {
        // Create a new constant instruction that will fit.
        Instruction replacementInstruction =
            new ConstantInstruction(constantInstruction.opcode,
                                    constantInstruction.constantIndex,
                                    constantInstruction.constant).shrink();

        replaceInstruction(offset, replacementInstruction);

        // Write out a dummy constant instruction for now.
        constantInstruction.constantIndex = 0;
        constantInstruction.constant      = 0;
        constantInstruction.write(codeAttribute, offset);
    }
}
 
Example #5
Source File: InstructionSequenceMatcher.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)
{
    Instruction patternInstruction = patternInstructions[patternInstructionIndex];

    // Check if the instruction matches the next instruction in the sequence.
    boolean condition =
        matchingOpcodes(constantInstruction, patternInstruction) &&
        matchingConstantIndices(clazz,
                                constantInstruction.constantIndex,
                                ((ConstantInstruction)patternInstruction).constantIndex) &&
        matchingArguments(constantInstruction.constant,
                          ((ConstantInstruction)patternInstruction).constant);

    // Check if the instruction sequence is matching now.
    checkMatch(condition,
               clazz,
               method,
               codeAttribute,
               offset,
               constantInstruction);
}
 
Example #6
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 #7
Source File: DuplicateInitializerInvocationFixer.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)
{
    if (constantInstruction.opcode == InstructionConstants.OP_INVOKESPECIAL)
    {
        hasBeenFixed = false;
        clazz.constantPoolEntryAccept(constantInstruction.constantIndex, this);

        if (hasBeenFixed)
        {
            Instruction extraInstruction =
                new SimpleInstruction(InstructionConstants.OP_ICONST_0);

            codeAttributeEditor.insertBeforeInstruction(offset,
                                                        extraInstruction);

            if (DEBUG)
            {
                System.out.println("  ["+clazz.getName()+"."+method.getName(clazz)+method.getDescriptor(clazz)+"] Inserting "+extraInstruction.toString()+" before "+constantInstruction.toString(offset));
            }

            if (extraAddedInstructionVisitor != null)
            {
                extraInstruction.accept(null, null, null, offset, extraAddedInstructionVisitor);
            }
        }
    }
}
 
Example #8
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 #9
Source File: EvaluationShrinker.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Replaces the instruction at a given offset by a static invocation.
 */
private void replaceByStaticInvocation(Clazz               clazz,
                                       int                 offset,
                                       ConstantInstruction constantInstruction)
{
    // Remember the replacement instruction.
    Instruction replacementInstruction =
         new ConstantInstruction(InstructionConstants.OP_INVOKESTATIC,
                                 constantInstruction.constantIndex).shrink();

    if (DEBUG) System.out.println("  Replacing by static invocation "+constantInstruction.toString(offset)+" -> "+replacementInstruction.toString());

    codeAttributeEditor.replaceInstruction(offset, replacementInstruction);
}
 
Example #10
Source File: BranchTargetFinder.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)
{
    // Mark the instruction.
    instructionMarks[offset] |= INSTRUCTION;

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

    // Check if the instruction is a 'new' instruction.
    if (constantInstruction.opcode == InstructionConstants.OP_NEW)
    {
        // Push the 'new' instruction offset on the stack.
        recentCreationOffsets[recentCreationOffsetIndex++] = offset;
    }
    else
    {
        // Check if the instruction is an initializer invocation.
        isInitializer = false;
        clazz.constantPoolEntryAccept(constantInstruction.constantIndex, this);
        if (isInitializer)
        {
            // Pop the 'new' instruction offset from the stack.
            int recentCreationOffset = recentCreationOffsets[--recentCreationOffsetIndex];

            // Fill it out in the creation offsets.
            creationOffsets[offset] = recentCreationOffset;

            // Fill out the initialization offsets.
            if (recentCreationOffset == AT_METHOD_ENTRY)
            {
                superInitializationOffset = offset;
            }
            else
            {
                initializationOffsets[recentCreationOffset] = offset;
            }
        }
    }
}
 
Example #11
Source File: SuperInvocationMarker.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)
{
    if (constantInstruction.opcode == InstructionConstants.OP_INVOKESPECIAL)
    {
        invokesSuperMethods = false;

        clazz.constantPoolEntryAccept(constantInstruction.constantIndex, this);

        if (invokesSuperMethods)
        {
            setInvokesSuperMethods(method);
        }
    }
}
 
Example #12
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 #13
Source File: EvaluationShrinker.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)
{
    // Mark the initializer invocation, if this is a 'new' instruction.
    if (constantInstruction.opcode == InstructionConstants.OP_NEW)
    {
        markInitialization(offset);
    }

    markStackProducers(clazz, offset, constantInstruction);
}
 
Example #14
Source File: InvocationUnit.java    From proguard with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Updates the given stack corresponding to the execution of the given
 * field or method reference instruction.
 */
public void invokeMember(Clazz               clazz,
                         Method              method,
                         CodeAttribute       codeAttribute,
                         int                 offset,
                         ConstantInstruction constantInstruction,
                         Stack               stack);
 
Example #15
Source File: EvaluationShrinker.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_INVOKEVIRTUAL:
        case InstructionConstants.OP_INVOKESPECIAL:
        case InstructionConstants.OP_INVOKESTATIC:
        case InstructionConstants.OP_INVOKEINTERFACE:
            this.invocationOffset      = offset;
            this.invocationInstruction = constantInstruction;
            clazz.constantPoolEntryAccept(constantInstruction.constantIndex, this);
            break;
    }
}
 
Example #16
Source File: LivenessAnalyzer.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)
{
    // Special case: variable 0 ('this') in an initializer has to be alive
    // as long as it hasn't been initialized.
     if (offset == partialEvaluator.superInitializationOffset())
    {
        alive |= 1L;
    }
}
 
Example #17
Source File: ExceptionInstructionChecker.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 may throw exceptions.
        switch (opcode)
        {
            case InstructionConstants.OP_GETSTATIC:
            case InstructionConstants.OP_PUTSTATIC:
            case InstructionConstants.OP_GETFIELD:
            case InstructionConstants.OP_PUTFIELD:
            case InstructionConstants.OP_INVOKEVIRTUAL:
            case InstructionConstants.OP_INVOKESPECIAL:
            case InstructionConstants.OP_INVOKESTATIC:
            case InstructionConstants.OP_INVOKEINTERFACE:
            case InstructionConstants.OP_NEW:
            case InstructionConstants.OP_ANEWARRAY:
            case InstructionConstants.OP_CHECKCAST:
            case InstructionConstants.OP_INSTANCEOF:
            case InstructionConstants.OP_MULTIANEWARRAY:
                // These instructions may throw exceptions.
                mayThrowExceptions = true;
        }

//          case InstructionConstants.OP_INVOKEVIRTUAL:
//          case InstructionConstants.OP_INVOKESPECIAL:
//          case InstructionConstants.OP_INVOKESTATIC:
//          case InstructionConstants.OP_INVOKEINTERFACE:
//            // Check if the invoking the method may throw an exception.
//            clazz.constantPoolEntryAccept(constantInstruction.constantIndex, this);
    }
 
Example #18
Source File: InstantiationClassMarker.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)
{
    if (constantInstruction.opcode == InstructionConstants.OP_NEW)
    {
        clazz.constantPoolEntryAccept(constantInstruction.constantIndex, this);
    }
}
 
Example #19
Source File: InstanceofClassMarker.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)
{
    if (constantInstruction.opcode == InstructionConstants.OP_INSTANCEOF)
    {
        clazz.constantPoolEntryAccept(constantInstruction.constantIndex, this);
    }
}
 
Example #20
Source File: InvocationUnit.java    From bazel with Apache License 2.0 5 votes vote down vote up
/**
 * Updates the given stack corresponding to the execution of the given
 * field or method reference instruction.
 */
public void invokeMember(Clazz               clazz,
                         Method              method,
                         CodeAttribute       codeAttribute,
                         int                 offset,
                         ConstantInstruction constantInstruction,
                         Stack               stack);
 
Example #21
Source File: InvocationUnit.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Updates the given stack corresponding to the execution of the given
 * field or method reference instruction.
 */
public void invokeMember(Clazz clazz,
                         Method method,
                         CodeAttribute codeAttribute,
                         int offset,
                         ConstantInstruction constantInstruction,
                         Stack stack);
 
Example #22
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 #23
Source File: DotClassMarker.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)
{
    if (constantInstruction.opcode == InstructionConstants.OP_LDC ||
        constantInstruction.opcode == InstructionConstants.OP_LDC_W)
    {
        clazz.constantPoolEntryAccept(constantInstruction.constantIndex, this);
    }
}
 
Example #24
Source File: CodeAttributeEditor.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)
{
    // Write out the instruction.
    instructionWriter.visitConstantInstruction(clazz,
                                               method,
                                               codeAttribute,
                                               newOffset,
                                               constantInstruction);

    newOffset += constantInstruction.length(newOffset);
}
 
Example #25
Source File: ConstantPoolRemapper.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)
{
    // Is the new constant pool index different from the original one?
    int newConstantIndex = remapConstantIndex(constantInstruction.constantIndex);
    if (newConstantIndex != constantInstruction.constantIndex)
    {
        // Replace the instruction.
        Instruction replacementInstruction =
            new ConstantInstruction(constantInstruction.opcode,
                                    newConstantIndex,
                                    constantInstruction.constant).shrink();

        codeAttributeEditor.replaceInstruction(offset, replacementInstruction);
    }
}
 
Example #26
Source File: InstructionAdder.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)
{
    // Create a copy of the instruction.
    Instruction newConstantInstruction =
        new ConstantInstruction(constantInstruction.opcode,
                                constantAdder.addConstant(clazz, constantInstruction.constantIndex),
                                constantInstruction.constant).shrink();

    // Add the instruction.
    codeAttributeComposer.appendInstruction(offset, newConstantInstruction);
}
 
Example #27
Source File: DotClassClassVisitor.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;

    // Could this instruction be a .class construct?
    if (opcode == InstructionConstants.OP_LDC ||
        opcode == InstructionConstants.OP_LDC_W)
    {
        clazz.constantPoolEntryAccept(constantInstruction.constantIndex,
                                      this);
    }
}
 
Example #28
Source File: MultiInstructionVisitor.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)
{
    for (int index = 0; index < instructionVisitorCount; index++)
    {
        instructionVisitors[index].visitConstantInstruction(clazz, method, codeAttribute, offset, constantInstruction);
    }
}
 
Example #29
Source File: ClassPrinter.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)
{
    println(constantInstruction.toString(offset));

    indent();
    clazz.constantPoolEntryAccept(constantInstruction.constantIndex, this);
    outdent();
}
 
Example #30
Source File: EvaluationSimplifier.java    From java-n-IDE-for-Android with Apache License 2.0 4 votes vote down vote up
/**
 * Replaces the integer pushing instruction at the given offset by a simpler
 * push instruction, if possible.
 */
private void replaceIntegerPushInstruction(Clazz       clazz,
                                           int         offset,
                                           Instruction instruction,
                                           int         maxVariableIndex)
{
    Value pushedValue = partialEvaluator.getStackAfter(offset).getTop(0);
    if (pushedValue.isParticular())
    {
        int value = pushedValue.integerValue().value();
        if (value << 16 >> 16 == value)
        {
            replaceConstantPushInstruction(clazz,
                                           offset,
                                           instruction,
                                           InstructionConstants.OP_SIPUSH,
                                           value);
        }
        else
        {
            ConstantPoolEditor constantPoolEditor =
                new ConstantPoolEditor((ProgramClass)clazz);

            Instruction replacementInstruction =
                new ConstantInstruction(InstructionConstants.OP_LDC,
                                        constantPoolEditor.addIntegerConstant(value)).shrink();

            replaceInstruction(clazz, offset, instruction, replacementInstruction);
        }
    }
    else if (pushedValue.isSpecific())
    {
        TracedVariables variables = partialEvaluator.getVariablesBefore(offset);
        for (int variableIndex = 0; variableIndex < maxVariableIndex; variableIndex++)
        {
            if (pushedValue.equals(variables.load(variableIndex)))
            {
                replaceVariablePushInstruction(clazz,
                                               offset,
                                               instruction,
                                               InstructionConstants.OP_ILOAD,
                                               variableIndex);
            }
        }
    }
}