Java Code Examples for proguard.optimize.info.ParameterUsageMarker#isParameterUsed()

The following examples show how to use proguard.optimize.info.ParameterUsageMarker#isParameterUsed() . 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: MethodStaticizer.java    From proguard with GNU General Public License v2.0 6 votes vote down vote up
public void visitProgramMethod(ProgramClass programClass, ProgramMethod programMethod)
{
    // Is the 'this' parameter being used?
    if (!ParameterUsageMarker.isParameterUsed(programMethod, 0))
    {
        // Make the method static.
        programMethod.u2accessFlags =
            (programMethod.getAccessFlags() & ~AccessConstants.FINAL) |
            AccessConstants.STATIC;

        // Visit the method, if required.
        if (extraStaticMemberVisitor != null)
        {
            extraStaticMemberVisitor.visitProgramMethod(programClass, programMethod);
        }
    }
}
 
Example 2
Source File: MethodStaticizer.java    From java-n-IDE-for-Android with Apache License 2.0 6 votes vote down vote up
public void visitProgramMethod(ProgramClass programClass, ProgramMethod programMethod)
{
    // Is the 'this' parameter being used?
    if (!ParameterUsageMarker.isParameterUsed(programMethod, 0))
    {
        // Make the method static.
        programMethod.u2accessFlags =
            (programMethod.getAccessFlags() & ~ClassConstants.INTERNAL_ACC_FINAL) |
            ClassConstants.INTERNAL_ACC_STATIC;

        // Visit the method, if required.
        if (extraStaticMemberVisitor != null)
        {
            extraStaticMemberVisitor.visitProgramMethod(programClass, programMethod);
        }
    }
}
 
Example 3
Source File: MethodStaticizer.java    From proguard with GNU General Public License v2.0 6 votes vote down vote up
public void visitProgramMethod(ProgramClass programClass, ProgramMethod programMethod)
{
    // Is the 'this' parameter being used?
    if (!ParameterUsageMarker.isParameterUsed(programMethod, 0))
    {
        // Make the method static.
        programMethod.u2accessFlags =
            (programMethod.getAccessFlags() & ~ClassConstants.ACC_FINAL) |
            ClassConstants.ACC_STATIC;

        // Visit the method, if required.
        if (extraStaticMemberVisitor != null)
        {
            extraStaticMemberVisitor.visitProgramMethod(programClass, programMethod);
        }
    }
}
 
Example 4
Source File: MethodStaticizer.java    From bazel with Apache License 2.0 6 votes vote down vote up
public void visitProgramMethod(ProgramClass programClass, ProgramMethod programMethod)
{
    // Is the 'this' parameter being used?
    if (!ParameterUsageMarker.isParameterUsed(programMethod, 0))
    {
        // Make the method static.
        programMethod.u2accessFlags =
            (programMethod.getAccessFlags() & ~ClassConstants.ACC_FINAL) |
            ClassConstants.ACC_STATIC;

        // Visit the method, if required.
        if (extraStaticMemberVisitor != null)
        {
            extraStaticMemberVisitor.visitProgramMethod(programClass, programMethod);
        }
    }
}
 
Example 5
Source File: EvaluationShrinker.java    From proguard with GNU General Public License v2.0 5 votes vote down vote up
public void visitProgramMethod(ProgramClass programClass, ProgramMethod programMethod)
{
    // Make the method invocation static, if possible.
    if ((programMethod.getAccessFlags() & AccessConstants.STATIC) == 0 &&
        !ParameterUsageMarker.isParameterUsed(programMethod, 0))
    {
        replaceByStaticInvocation(programClass,
                                  invocationOffset,
                                  invocationInstruction);
    }
}
 
Example 6
Source File: EvaluationShrinker.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
public void visitProgramMethod(ProgramClass programClass, ProgramMethod programMethod)
{
    // Get the total size of the parameters.
    int parameterSize = ParameterUsageMarker.getParameterSize(programMethod);

    // Make the method invocation static, if possible.
    if ((programMethod.getAccessFlags() & ClassConstants.INTERNAL_ACC_STATIC) == 0 &&
        !ParameterUsageMarker.isParameterUsed(programMethod, 0))
    {
        replaceByStaticInvocation(programClass,
                                  invocationOffset,
                                  invocationInstruction);
    }

    // Remove unused parameters.
    for (int index = 0; index < parameterSize; index++)
    {
        if (!ParameterUsageMarker.isParameterUsed(programMethod, index))
        {
            TracedStack stack =
                partialEvaluator.getStackBefore(invocationOffset);

            int stackIndex = stack.size() - parameterSize + index;

            if (DEBUG)
            {
                System.out.println("  ["+invocationOffset+"] Ignoring parameter #"+index+" of "+programClass.getName()+"."+programMethod.getName(programClass)+programMethod.getDescriptor(programClass)+"] (stack entry #"+stackIndex+" ["+stack.getBottom(stackIndex)+"])");
                System.out.println("    Full stack: "+stack);
            }

            markStackSimplificationBefore(invocationOffset, stackIndex);
        }
    }
}
 
Example 7
Source File: MethodDescriptorShrinker.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a shrunk descriptor or signature of the given method.
 */
private String shrinkDescriptor(Method method, String descriptor)
{
    // All parameters of non-static methods are shifted by one in the local
    // variable frame.
    int parameterIndex =
        (method.getAccessFlags() & ClassConstants.INTERNAL_ACC_STATIC) != 0 ?
            0 : 1;

    // Go over the parameters.
    InternalTypeEnumeration internalTypeEnumeration =
        new InternalTypeEnumeration(descriptor);

    StringBuffer newDescriptorBuffer = new StringBuffer();

    newDescriptorBuffer.append(internalTypeEnumeration.formalTypeParameters());
    newDescriptorBuffer.append(ClassConstants.INTERNAL_METHOD_ARGUMENTS_OPEN);

    while (internalTypeEnumeration.hasMoreTypes())
    {
        String type = internalTypeEnumeration.nextType();
        if (ParameterUsageMarker.isParameterUsed(method, parameterIndex))
        {
            newDescriptorBuffer.append(type);
        }
        else if (DEBUG)
        {
            System.out.println("  Deleting parameter #"+parameterIndex+" ["+type+"]");
        }

        parameterIndex += ClassUtil.isInternalCategory2Type(type) ? 2 : 1;
    }

    newDescriptorBuffer.append(ClassConstants.INTERNAL_METHOD_ARGUMENTS_CLOSE);
    newDescriptorBuffer.append(internalTypeEnumeration.returnType());

    return newDescriptorBuffer.toString();
}
 
Example 8
Source File: ParameterShrinker.java    From java-n-IDE-for-Android with Apache License 2.0 4 votes vote down vote up
public void visitCodeAttribute(Clazz clazz, Method method, CodeAttribute codeAttribute)
{
    // Get the original parameter size that was saved.
    int oldParameterSize = ParameterUsageMarker.getParameterSize(method);

    // Compute the new parameter size from the shrunk descriptor.
    int newParameterSize =
        ClassUtil.internalMethodParameterSize(method.getDescriptor(clazz),
                                              method.getAccessFlags());

    if (oldParameterSize > newParameterSize)
    {
        // Get the total size of the local variable frame.
        int maxLocals = codeAttribute.u2maxLocals;

        if (DEBUG)
        {
            System.out.println("ParameterShrinker: "+clazz.getName()+"."+method.getName(clazz)+method.getDescriptor(clazz));
            System.out.println("  Old parameter size = " + oldParameterSize);
            System.out.println("  New parameter size = " + newParameterSize);
            System.out.println("  Max locals         = " + maxLocals);
        }

        // Create a variable map.
        int[] variableMap = new int[maxLocals];

        // Move unused parameters right after the parameter block.
        int usedParameterIndex   = 0;
        int unusedParameterIndex = newParameterSize;
        for (int parameterIndex = 0; parameterIndex < oldParameterSize; parameterIndex++)
        {
            // Is the variable required as a parameter?
            if (ParameterUsageMarker.isParameterUsed(method, parameterIndex))
            {
                // Keep the variable as a parameter.
                variableMap[parameterIndex] = usedParameterIndex++;
            }
            else
            {
                if (DEBUG)
                {
                    System.out.println("  Deleting parameter #"+parameterIndex);
                }

                // Shift the variable to the unused parameter block,
                // in case it is still used as a variable.
                variableMap[parameterIndex] = unusedParameterIndex++;

                // Visit the method, if required.
                if (extraVariableMemberVisitor != null)
                {
                    method.accept(clazz, extraVariableMemberVisitor);
                }
            }
        }

        // Fill out the remainder of the map.
        for (int variableIndex = oldParameterSize; variableIndex < maxLocals; variableIndex++)
        {
            variableMap[variableIndex] = variableIndex;
        }

        // Set the map.
        variableRemapper.setVariableMap(variableMap);

        // Remap the variables.
        variableRemapper.visitCodeAttribute(clazz, method, codeAttribute);
    }
}
 
Example 9
Source File: MethodDescriptorShrinker.java    From java-n-IDE-for-Android with Apache License 2.0 4 votes vote down vote up
public void visitAnyParameterAnnotationsAttribute(Clazz clazz, Method method, ParameterAnnotationsAttribute parameterAnnotationsAttribute)
{
    int[]          annotationsCounts = parameterAnnotationsAttribute.u2parameterAnnotationsCount;
    Annotation[][] annotations       = parameterAnnotationsAttribute.parameterAnnotations;

    // All parameters of non-static methods are shifted by one in the local
    // variable frame.
    int parameterIndex =
        (method.getAccessFlags() & ClassConstants.INTERNAL_ACC_STATIC) != 0 ?
            0 : 1;

    int annotationIndex    = 0;
    int newAnnotationIndex = 0;

    // Go over the parameters.
    String descriptor = method.getDescriptor(clazz);
    InternalTypeEnumeration internalTypeEnumeration =
        new InternalTypeEnumeration(descriptor);

    while (internalTypeEnumeration.hasMoreTypes())
    {
        String type = internalTypeEnumeration.nextType();
        if (ParameterUsageMarker.isParameterUsed(method, parameterIndex))
        {
            annotationsCounts[newAnnotationIndex] = annotationsCounts[annotationIndex];
            annotations[newAnnotationIndex++]     = annotations[annotationIndex];
        }

        annotationIndex++;

        parameterIndex += ClassUtil.isInternalCategory2Type(type) ? 2 : 1;
    }

    // Update the number of parameters.
    parameterAnnotationsAttribute.u2parametersCount = newAnnotationIndex;

    // Clear the unused entries.
    while (newAnnotationIndex < annotationIndex)
    {
        annotationsCounts[newAnnotationIndex] = 0;
        annotations[newAnnotationIndex++]     = null;
    }
}
 
Example 10
Source File: MethodDescriptorShrinker.java    From java-n-IDE-for-Android with Apache License 2.0 4 votes vote down vote up
/**
 * Shrinks the array of referenced classes of the given method.
 */
private Clazz[] shrinkReferencedClasses(Method  method,
                                        String  descriptor,
                                        Clazz[] referencedClasses)
{
    if (referencedClasses != null)
    {
        // All parameters of non-static methods are shifted by one in the local
        // variable frame.
        int parameterIndex =
            (method.getAccessFlags() & ClassConstants.INTERNAL_ACC_STATIC) != 0 ?
                0 : 1;

        int referencedClassIndex    = 0;
        int newReferencedClassIndex = 0;

        // Go over the parameters.
        InternalTypeEnumeration internalTypeEnumeration =
            new InternalTypeEnumeration(descriptor);

        // Also look at the formal type parameters.
        String type  = internalTypeEnumeration.formalTypeParameters();
        int    count = new DescriptorClassEnumeration(type).classCount();
        for (int counter = 0; counter < count; counter++)
        {
            referencedClasses[newReferencedClassIndex++] =
                referencedClasses[referencedClassIndex++];
        }

        while (internalTypeEnumeration.hasMoreTypes())
        {
            // Consider the classes referenced by this parameter type.
            type  = internalTypeEnumeration.nextType();
            count = new DescriptorClassEnumeration(type).classCount();

            if (ParameterUsageMarker.isParameterUsed(method, parameterIndex))
            {
                // Copy the referenced classes.
                for (int counter = 0; counter < count; counter++)
                {
                    referencedClasses[newReferencedClassIndex++] =
                        referencedClasses[referencedClassIndex++];
                }
            }
            else
            {
                // Skip the referenced classes.
                referencedClassIndex += count;
            }

            parameterIndex += ClassUtil.isInternalCategory2Type(type) ? 2 : 1;
        }

        // Also look at the return value.
        type  = internalTypeEnumeration.returnType();
        count = new DescriptorClassEnumeration(type).classCount();
        for (int counter = 0; counter < count; counter++)
        {
            referencedClasses[newReferencedClassIndex++] =
                referencedClasses[referencedClassIndex++];
        }

        // Clear the unused entries.
        while (newReferencedClassIndex < referencedClassIndex)
        {
            referencedClasses[newReferencedClassIndex++] = null;
        }
    }

    return referencedClasses;
}
 
Example 11
Source File: ParameterShrinker.java    From proguard with GNU General Public License v2.0 4 votes vote down vote up
public void visitCodeAttribute(Clazz clazz, Method method, CodeAttribute codeAttribute)
{
    // Get the original parameter size that was saved.
    int oldParameterSize = ParameterUsageMarker.getParameterSize(method);

    // Compute the new parameter size from the shrunk descriptor.
    int newParameterSize =
        ClassUtil.internalMethodParameterSize(method.getDescriptor(clazz),
                                              method.getAccessFlags());

    if (oldParameterSize > newParameterSize)
    {
        // Get the total size of the local variable frame.
        int maxLocals = codeAttribute.u2maxLocals;

        if (DEBUG)
        {
            System.out.println("ParameterShrinker: "+clazz.getName()+"."+method.getName(clazz)+method.getDescriptor(clazz));
            System.out.println("  Old parameter size = " + oldParameterSize);
            System.out.println("  New parameter size = " + newParameterSize);
            System.out.println("  Max locals         = " + maxLocals);
        }

        // Create a variable map.
        int[] variableMap = new int[maxLocals];

        // Move unused parameters right after the parameter block.
        int usedParameterIndex   = 0;
        int unusedParameterIndex = newParameterSize;
        for (int parameterIndex = 0; parameterIndex < oldParameterSize; parameterIndex++)
        {
            // Is the variable required as a parameter?
            if (ParameterUsageMarker.isParameterUsed(method, parameterIndex))
            {
                // Keep the variable as a parameter.
                variableMap[parameterIndex] = usedParameterIndex++;
            }
            else
            {
                if (DEBUG)
                {
                    System.out.println("  Deleting parameter #"+parameterIndex);
                }

                // Shift the variable to the unused parameter block,
                // in case it is still used as a variable.
                variableMap[parameterIndex] = unusedParameterIndex++;

                // Visit the method, if required.
                if (extraVariableMemberVisitor != null)
                {
                    method.accept(clazz, extraVariableMemberVisitor);
                }
            }
        }

        // Fill out the remainder of the map.
        for (int variableIndex = oldParameterSize; variableIndex < maxLocals; variableIndex++)
        {
            variableMap[variableIndex] = variableIndex;
        }

        // Set the map.
        variableRemapper.setVariableMap(variableMap);

        // Remap the variables.
        variableRemapper.visitCodeAttribute(clazz, method, codeAttribute);
    }
}
 
Example 12
Source File: ParameterShrinker.java    From bazel with Apache License 2.0 4 votes vote down vote up
public void visitCodeAttribute(Clazz clazz, Method method, CodeAttribute codeAttribute)
{
    // Get the original parameter size that was saved.
    int oldParameterSize = ParameterUsageMarker.getParameterSize(method);

    // Compute the new parameter size from the shrunk descriptor.
    int newParameterSize =
        ClassUtil.internalMethodParameterSize(method.getDescriptor(clazz),
                                              method.getAccessFlags());

    if (oldParameterSize > newParameterSize)
    {
        // Get the total size of the local variable frame.
        int maxLocals = codeAttribute.u2maxLocals;

        if (DEBUG)
        {
            System.out.println("ParameterShrinker: "+clazz.getName()+"."+method.getName(clazz)+method.getDescriptor(clazz));
            System.out.println("  Old parameter size = " + oldParameterSize);
            System.out.println("  New parameter size = " + newParameterSize);
            System.out.println("  Max locals         = " + maxLocals);
        }

        // Create a variable map.
        int[] variableMap = new int[maxLocals];

        // Move unused parameters right after the parameter block.
        int usedParameterIndex   = 0;
        int unusedParameterIndex = newParameterSize;
        for (int parameterIndex = 0; parameterIndex < oldParameterSize; parameterIndex++)
        {
            // Is the variable required as a parameter?
            if (ParameterUsageMarker.isParameterUsed(method, parameterIndex))
            {
                // Keep the variable as a parameter.
                variableMap[parameterIndex] = usedParameterIndex++;
            }
            else
            {
                if (DEBUG)
                {
                    System.out.println("  Deleting parameter #"+parameterIndex);
                }

                // Shift the variable to the unused parameter block,
                // in case it is still used as a variable.
                variableMap[parameterIndex] = unusedParameterIndex++;

                // Visit the method, if required.
                if (extraVariableMemberVisitor != null)
                {
                    method.accept(clazz, extraVariableMemberVisitor);
                }
            }
        }

        // Fill out the remainder of the map.
        for (int variableIndex = oldParameterSize; variableIndex < maxLocals; variableIndex++)
        {
            variableMap[variableIndex] = variableIndex;
        }

        // Set the map.
        variableRemapper.setVariableMap(variableMap);

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