proguard.evaluation.value.Value Java Examples

The following examples show how to use proguard.evaluation.value.Value. 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: ConstantParameterFilter.java    From proguard with GNU General Public License v2.0 6 votes vote down vote up
public void visitProgramMethod(ProgramClass programClass, ProgramMethod programMethod)
{
    // All parameters of non-static methods are shifted by one in the local
    // variable frame.
    int firstParameterIndex =
        (programMethod.getAccessFlags() & ClassConstants.ACC_STATIC) != 0 ?
            0 : 1;

    int parameterCount =
        ClassUtil.internalMethodParameterCount(programMethod.getDescriptor(programClass));

    for (int index = firstParameterIndex; index < parameterCount; index++)
    {
        Value value = StoringInvocationUnit.getMethodParameterValue(programMethod, index);
        if (value != null &&
            value.isParticular())
        {
            constantParameterVisitor.visitProgramMethod(programClass, programMethod);
        }
    }
}
 
Example #2
Source File: ParameterUsageMarker.java    From java-n-IDE-for-Android with Apache License 2.0 6 votes vote down vote up
public void visitVariableInstruction(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, VariableInstruction variableInstruction)
{
    if (partialEvaluator.isTraced(offset) &&
        variableInstruction.isLoad())
    {
        int parameterIndex = variableInstruction.variableIndex;
        if (parameterIndex < codeAttribute.u2maxLocals)
        {
            Value producer =
                partialEvaluator.getVariablesBefore(offset).getProducerValue(parameterIndex);
            if (producer != null &&
                producer.instructionOffsetValue().contains(PartialEvaluator.AT_METHOD_ENTRY))
            {
                // Mark the variable.
                markParameterUsed(method, parameterIndex);

                // Account for Category 2 instructions, which take up two entries.
                if (variableInstruction.isCategory2())
                {
                    markParameterUsed(method, parameterIndex + 1);
                }
            }
        }
    }
}
 
Example #3
Source File: TracedStack.java    From bazel with Apache License 2.0 6 votes vote down vote up
public String toString()
{
    StringBuffer buffer = new StringBuffer();

    for (int index = 0; index < this.size(); index++)
    {
        Value value               = this.values[index];
        Value producerValue       = producerStack.getBottom(index);
        Value actualProducerValue = actualProducerStack.getBottom(index);
        buffer = buffer.append('[')
                       .append(producerValue == null ? "empty:" :
                                                       producerValue.equals(actualProducerValue) ? producerValue.toString() :
                                                                                                   producerValue.toString() + actualProducerValue.toString())
                       .append(value         == null ? "empty"  : value.toString())
                       .append(']');
    }

    return buffer.toString();
}
 
Example #4
Source File: MethodOptimizationInfo.java    From java-n-IDE-for-Android with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a new MethodOptimizationInfo for the given method.
 */
public MethodOptimizationInfo(Clazz clazz, Method method)
{
    // Set up an array of the right size for storing information about the
    // passed parameters.
    int parameterCount =
        ClassUtil.internalMethodParameterCount(method.getDescriptor(clazz));

    if ((method.getAccessFlags() & ClassConstants.INTERNAL_ACC_STATIC) == 0)
    {
        parameterCount++;
    }

    if (parameterCount > 0)
    {
        parameters = new Value[parameterCount];
    }
}
 
Example #5
Source File: ConstantParameterFilter.java    From bazel with Apache License 2.0 6 votes vote down vote up
public void visitProgramMethod(ProgramClass programClass, ProgramMethod programMethod)
{
    // All parameters of non-static methods are shifted by one in the local
    // variable frame.
    int firstParameterIndex =
        (programMethod.getAccessFlags() & ClassConstants.ACC_STATIC) != 0 ?
            0 : 1;

    int parameterCount =
        ClassUtil.internalMethodParameterCount(programMethod.getDescriptor(programClass));

    for (int index = firstParameterIndex; index < parameterCount; index++)
    {
        Value value = StoringInvocationUnit.getMethodParameterValue(programMethod, index);
        if (value != null &&
            value.isParticular())
        {
            constantParameterVisitor.visitProgramMethod(programClass, programMethod);
        }
    }
}
 
Example #6
Source File: MethodOptimizationInfo.java    From proguard with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Creates a new MethodOptimizationInfo for the given method.
 */
public MethodOptimizationInfo(Clazz clazz, Method method)
{
    // Set up an array of the right size for storing information about the
    // passed parameters.
    int parameterCount =
        ClassUtil.internalMethodParameterCount(method.getDescriptor(clazz));

    if ((method.getAccessFlags() & ClassConstants.ACC_STATIC) == 0)
    {
        parameterCount++;
    }

    if (parameterCount > 0)
    {
        parameters = new Value[parameterCount];
    }
}
 
Example #7
Source File: TracedBranchUnit.java    From java-n-IDE-for-Android with Apache License 2.0 6 votes vote down vote up
public void branchConditionally(Clazz         clazz,
                                CodeAttribute codeAttribute,
                                int           offset,
                                int           branchTarget,
                                int           conditional)
{
    if      (conditional == Value.ALWAYS)
    {
        // Always branch.
        super.branch(clazz, codeAttribute, offset, branchTarget);
    }
    else if (conditional != Value.NEVER)
    {
        // Maybe branch.
        super.branchConditionally(clazz, codeAttribute, offset, branchTarget, conditional);
    }
    else
    {
        super.setCalled();
    }
}
 
Example #8
Source File: TracedBranchUnit.java    From proguard with GNU General Public License v2.0 6 votes vote down vote up
public void branchConditionally(Clazz         clazz,
                                CodeAttribute codeAttribute,
                                int           offset,
                                int           branchTarget,
                                int           conditional)
{
    if      (conditional == Value.ALWAYS)
    {
        // Always branch.
        super.branch(clazz, codeAttribute, offset, branchTarget);
    }
    else if (conditional != Value.NEVER)
    {
        // Maybe branch.
        super.branchConditionally(clazz, codeAttribute, offset, branchTarget, conditional);
    }
    else
    {
        super.setCalled();
    }
}
 
Example #9
Source File: Processor.java    From java-n-IDE-for-Android with Apache License 2.0 6 votes vote down vote up
public void visitLookUpSwitchInstruction(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, LookUpSwitchInstruction lookUpSwitchInstruction)
{
    IntegerValue indexValue = stack.ipop();

    // If there is no definite branch in any of the cases below,
    // branch to the default offset.
    branchUnit.branch(clazz, codeAttribute,
                      offset,
                      offset + lookUpSwitchInstruction.defaultOffset);

    for (int index = 0; index < lookUpSwitchInstruction.jumpOffsets.length; index++)
    {
        int conditional = indexValue.equal(valueFactory.createIntegerValue(
            lookUpSwitchInstruction.cases[index]));
        branchUnit.branchConditionally(clazz, codeAttribute,
                                       offset,
                                       offset + lookUpSwitchInstruction.jumpOffsets[index],
                                       conditional);

        // If this branch is always taken, we can skip the rest.
        if (conditional == Value.ALWAYS)
        {
            break;
        }
    }
}
 
Example #10
Source File: LoadingInvocationUnit.java    From java-n-IDE-for-Android with Apache License 2.0 6 votes vote down vote up
protected Value getMethodReturnValue(Clazz       clazz,
                                     RefConstant refConstant,
                                     String      type)
{
    if (loadMethodReturnValues)
    {
        // Do we know this method?
        Member referencedMember = refConstant.referencedMember;
        if (referencedMember != null)
        {
            // Retrieve the stored method return value.
            Value value = StoringInvocationUnit.getMethodReturnValue((Method)referencedMember);
            if (value != null &&
                value.isParticular())
            {
                return value;
            }
        }
    }

    return super.getMethodReturnValue(clazz,
                                      refConstant,
                                      type);
}
 
Example #11
Source File: MemberDescriptorSpecializer.java    From java-n-IDE-for-Android with Apache License 2.0 6 votes vote down vote up
public void visitProgramField(ProgramClass programClass, ProgramField programField)
{
    Value parameterValue = StoringInvocationUnit.getFieldValue(programField);
    if (parameterValue.computationalType() == Value.TYPE_REFERENCE)
    {
        Clazz referencedClass = parameterValue.referenceValue().getReferencedClass();
        if (programField.referencedClass != referencedClass)
        {
            if (DEBUG)
            {
                System.out.println("MemberDescriptorSpecializer: "+programClass.getName()+"."+programField.getName(programClass)+" "+programField.getDescriptor(programClass));
                System.out.println("  "+programField.referencedClass.getName()+" -> "+referencedClass.getName());
            }

            programField.referencedClass = referencedClass;

            // Visit the field, if required.
            if (extraParameterMemberVisitor != null)
            {
                extraParameterMemberVisitor.visitProgramField(programClass, programField);
            }
        }
    }
}
 
Example #12
Source File: Variables.java    From java-n-IDE-for-Android with Apache License 2.0 6 votes vote down vote up
/**
 * Stores the given Value at the given variable index.
 */
public void store(int index, Value value)
{
    if (index < 0 ||
        index >= size)
    {
        throw new IndexOutOfBoundsException("Variable index ["+index+"] out of bounds ["+size+"]");
    }

    // Store the value.
    values[index] = value;

    // Account for the extra space required by Category 2 values.
    if (value.isCategory2())
    {
        values[index + 1] = TOP_VALUE;
    }
}
 
Example #13
Source File: Variables.java    From java-n-IDE-for-Android with Apache License 2.0 6 votes vote down vote up
/**
 * Resets this Variables object, so that it can be reused.
 */
public void reset(int size)
{
    // Is the values array large enough?
    if (size > values.length)
    {
        // Create a new one.
        values = new Value[size];
    }
    else
    {
        // Clear the variables.
        for (int index = 0; index < values.length; index++)
        {
            values[index] = null;
        }
    }

    this.size = size;
}
 
Example #14
Source File: Stack.java    From java-n-IDE-for-Android with Apache License 2.0 6 votes vote down vote up
/**
 * Copies the values of the given Stack into this Stack.
 */
public void copy(Stack other)
{
    // Is the values array large enough?
    if (other.values.length > values.length)
    {
        // Create a new one.
        values = new Value[other.values.length];
    }

    // Copy the stack contents.
    System.arraycopy(other.values, 0, this.values, 0, other.currentSize);

    // Copy the sizes.
    currentSize   = other.currentSize;
    actualMaxSize = other.actualMaxSize;
}
 
Example #15
Source File: TracedStack.java    From proguard with GNU General Public License v2.0 6 votes vote down vote up
public String toString()
{
    StringBuffer buffer = new StringBuffer();

    for (int index = 0; index < this.size(); index++)
    {
        Value value               = this.values[index];
        Value producerValue       = producerStack.getBottom(index);
        Value actualProducerValue = actualProducerStack.getBottom(index);
        buffer = buffer.append('[')
                       .append(producerValue == null ? "empty:" :
                                                       producerValue.equals(actualProducerValue) ? producerValue.toString() :
                                                                                                   producerValue.toString() + actualProducerValue.toString())
                       .append(value         == null ? "empty"  : value.toString())
                       .append(']');
    }

    return buffer.toString();
}
 
Example #16
Source File: LoadingInvocationUnit.java    From java-n-IDE-for-Android with Apache License 2.0 6 votes vote down vote up
protected Value getMethodParameterValue(Clazz  clazz,
                                        Method method,
                                        int    parameterIndex,
                                        String type,
                                        Clazz  referencedClass)
{
    if (loadMethodParameterValues)
    {
        // Retrieve the stored method parameter value.
        Value value = StoringInvocationUnit.getMethodParameterValue(method, parameterIndex);
        if (value != null &&
            value.isParticular())
        {
            return value;
        }
    }

    return super.getMethodParameterValue(clazz,
                                         method,
                                         parameterIndex,
                                         type,
                                         referencedClass);
}
 
Example #17
Source File: TracedStack.java    From java-n-IDE-for-Android with Apache License 2.0 6 votes vote down vote up
public String toString()
{
    StringBuffer buffer = new StringBuffer();

    for (int index = 0; index < this.size(); index++)
    {
        Value value         = this.values[index];
        Value producerValue = producerStack.getBottom(index);
        buffer = buffer.append('[')
                       .append(producerValue == null ? "empty:" : producerValue.toString())
                       .append(value         == null ? "empty"  : value.toString())
                       .append(']');
    }

    return buffer.toString();
}
 
Example #18
Source File: Stack.java    From java-n-IDE-for-Android with Apache License 2.0 6 votes vote down vote up
/**
 * Pushes the given Value onto the stack.
 */
public void push(Value value)
{
    // Account for the extra space required by Category 2 values.
    if (value.isCategory2())
    {
        values[currentSize++] = TOP_VALUE;
    }

    // Push the value.
    values[currentSize++] = value;

    // Update the maximum actual size;
    if (actualMaxSize < currentSize)
    {
        actualMaxSize = currentSize;
    }
}
 
Example #19
Source File: ParameterUsageMarker.java    From proguard with GNU General Public License v2.0 6 votes vote down vote up
public void visitVariableInstruction(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, VariableInstruction variableInstruction)
{
    if (partialEvaluator.isTraced(offset) &&
        variableInstruction.isLoad())
    {
        int parameterIndex = variableInstruction.variableIndex;
        if (parameterIndex < codeAttribute.u2maxLocals)
        {
            Value producer =
                partialEvaluator.getVariablesBefore(offset).getProducerValue(parameterIndex);
            if (producer != null &&
                producer.instructionOffsetValue().contains(PartialEvaluator.AT_METHOD_ENTRY))
            {
                // Mark the variable.
                markParameterUsed(method, parameterIndex);

                // Account for Category 2 instructions, which take up two entries.
                if (variableInstruction.isCategory2())
                {
                    markParameterUsed(method, parameterIndex + 1);
                }
            }
        }
    }
}
 
Example #20
Source File: Variables.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
public String toString()
{
    StringBuffer buffer = new StringBuffer();

    for (int index = 0; index < size; index++)
    {
        Value value = values[index];
        buffer = buffer.append('[')
                       .append(value == null ? "empty" : value.toString())
                       .append(']');
    }

    return buffer.toString();
}
 
Example #21
Source File: StoringInvocationUnit.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
public static Value getFieldValue(Field field)
{
    FieldOptimizationInfo info = FieldOptimizationInfo.getFieldOptimizationInfo(field);
    return info != null ?
        info.getValue() :
        null;
}
 
Example #22
Source File: TracedStack.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
public Value pop()
{
    Value value = super.pop();

    producerPop();

    // Account for the extra space required by Category 2 values.
    if (value.isCategory2())
    {
        producerPop();
    }

    return value;
}
 
Example #23
Source File: TracedStack.java    From bazel with Apache License 2.0 5 votes vote down vote up
public void push(Value value)
{
    super.push(value);

    producerPush();

    // Account for the extra space required by Category 2 values.
    if (value.isCategory2())
    {
        producerPush();
    }
}
 
Example #24
Source File: StoringInvocationUnit.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
private static void generalizeFieldValue(Field field, Value value)
{
    FieldOptimizationInfo info = FieldOptimizationInfo.getFieldOptimizationInfo(field);
    if (info != null)
    {
        info.generalizeValue(value);
    }
}
 
Example #25
Source File: TracedVariables.java    From proguard with GNU General Public License v2.0 5 votes vote down vote up
public void store(int index, Value value)
{
    // Store the value itself in the variable.
    super.store(index, value);

    // Store the producer value in its producer variable.
    producerVariables.store(index, producerValue);

    // Account for the extra space required by Category 2 values.
    if (value.isCategory2())
    {
        producerVariables.store(index+1, producerValue);
    }
}
 
Example #26
Source File: EvaluationShrinker.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the opcode of a push instruction corresponding to the given
 * computational type.
 * @param computationalType the computational type to be pushed on the stack.
 */
private byte pushOpcode(int computationalType)
{
    switch (computationalType)
    {
        case Value.TYPE_INTEGER:            return InstructionConstants.OP_ICONST_0;
        case Value.TYPE_LONG:               return InstructionConstants.OP_LCONST_0;
        case Value.TYPE_FLOAT:              return InstructionConstants.OP_FCONST_0;
        case Value.TYPE_DOUBLE:             return InstructionConstants.OP_DCONST_0;
        case Value.TYPE_REFERENCE:
        case Value.TYPE_INSTRUCTION_OFFSET: return InstructionConstants.OP_ACONST_NULL;
    }

    throw new IllegalArgumentException("No push opcode for computational type ["+computationalType+"]");
}
 
Example #27
Source File: ProgramMethodOptimizationInfo.java    From proguard with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Creates a new MethodOptimizationInfo for the given method.
 */
public ProgramMethodOptimizationInfo(Clazz clazz, Method method)
{
    // Set up an array of the right size for storing information about the
    // passed parameters (including 'this', for non-static methods).
    int parameterCount =
        ClassUtil.internalMethodParameterCount(method.getDescriptor(clazz),
                                               method.getAccessFlags());

    parameters = parameterCount == 0 ?
        EMPTY_PARAMETERS :
        new Value[parameterCount];
}
 
Example #28
Source File: Stack.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
public String toString()
{
    StringBuffer buffer = new StringBuffer();

    for (int index = 0; index < currentSize; index++)
    {
        Value value = values[index];
        buffer = buffer.append('[')
                       .append(value == null ? "empty" : value.toString())
                       .append(']');
    }

    return buffer.toString();
}
 
Example #29
Source File: PartialEvaluator.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Initializes the data structures for the variables, stack, etc.
 */
private void initializeParameters(Clazz           clazz,
                                  Method          method,
                                  CodeAttribute codeAttribute,
                                  TracedVariables variables)
{
    // Create the method parameters.
    TracedVariables parameters = new TracedVariables(codeAttribute.u2maxLocals);

    // Remember this instruction's offset with any stored value.
    Value storeValue = new InstructionOffsetValue(AT_METHOD_ENTRY);
    parameters.setProducerValue(storeValue);

    // Initialize the method parameters.
    invocationUnit.enterMethod(clazz, method, parameters);

    if (DEBUG)
    {
        System.out.println("  Params: "+parameters);
    }

    // Initialize the variables with the parameters.
    variables.initialize(parameters);

    // Set the store value of each parameter variable.
    InstructionOffsetValue atMethodEntry = new InstructionOffsetValue(AT_METHOD_ENTRY);

    for (int index = 0; index < parameters.size(); index++)
    {
        variables.setProducerValue(index, atMethodEntry);
    }
}
 
Example #30
Source File: Variables.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Gets the Value of the variable with the given index, without disturbing it.
 */
public Value getValue(int index)
{
    if (index < 0 ||
        index >= size)
    {
        throw new IndexOutOfBoundsException("Variable index ["+index+"] out of bounds ["+size+"]");
    }

    return values[index];
}