Java Code Examples for proguard.classfile.util.ClassUtil#internalMethodParameterSize()

The following examples show how to use proguard.classfile.util.ClassUtil#internalMethodParameterSize() . 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: VariableSizeUpdater.java    From java-n-IDE-for-Android with Apache License 2.0 6 votes vote down vote up
public void visitCodeAttribute(Clazz clazz, Method method, CodeAttribute codeAttribute)
    {
//        DEBUG =
//            clazz.getName().equals("abc/Def") &&
//            method.getName(clazz).equals("abc");

        // The minimum variable size is determined by the arguments.
        codeAttribute.u2maxLocals =
            ClassUtil.internalMethodParameterSize(method.getDescriptor(clazz),
                                                  method.getAccessFlags());

        if (DEBUG)
        {
            System.out.println("VariableSizeUpdater: "+clazz.getName()+"."+method.getName(clazz)+method.getDescriptor(clazz));
            System.out.println("  Max locals: "+codeAttribute.u2maxLocals+" <- parameters");
        }

        // Go over all instructions.
        codeAttribute.instructionsAccept(clazz, method, this);

        // Remove the unused variables of the attributes.
        variableCleaner.visitCodeAttribute(clazz, method, codeAttribute);
    }
 
Example 2
Source File: ConstantInstruction.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
private void visitRefConstant(Clazz clazz, RefConstant methodrefConstant)
{
    String type = methodrefConstant.getType(clazz);

    parameterStackDelta = ClassUtil.internalMethodParameterSize(type);
    typeStackDelta      = ClassUtil.internalTypeSize(ClassUtil.internalMethodReturnType(type));
}
 
Example 3
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 4
Source File: ConstantInstruction.java    From proguard with GNU General Public License v2.0 5 votes vote down vote up
public void visitNameAndTypeConstant(Clazz clazz, NameAndTypeConstant nameAndTypeConstant)
{
    String type = nameAndTypeConstant.getType(clazz);

    parameterStackDelta = ClassUtil.internalMethodParameterSize(type);
    typeStackDelta      = ClassUtil.internalTypeSize(ClassUtil.internalMethodReturnType(type));
}
 
Example 5
Source File: ConstantInstruction.java    From bazel with Apache License 2.0 5 votes vote down vote up
public void visitNameAndTypeConstant(Clazz clazz, NameAndTypeConstant nameAndTypeConstant)
{
    String type = nameAndTypeConstant.getType(clazz);

    parameterStackDelta = ClassUtil.internalMethodParameterSize(type);
    typeStackDelta      = ClassUtil.internalTypeSize(ClassUtil.internalMethodReturnType(type));
}
 
Example 6
Source File: ParameterUsageMarker.java    From java-n-IDE-for-Android with Apache License 2.0 4 votes vote down vote up
public void visitProgramMethod(ProgramClass programClass, ProgramMethod programMethod)
{
    int parameterSize =
        ClassUtil.internalMethodParameterSize(programMethod.getDescriptor(programClass),
                                              programMethod.getAccessFlags());

    if (parameterSize > 0)
    {
        int accessFlags = programMethod.getAccessFlags();

        // Must we mark the 'this' parameter?
        if (markThisParameter &&
            (accessFlags & ClassConstants.INTERNAL_ACC_STATIC) == 0)
        {
            // Mark the 'this' parameter.
            markParameterUsed(programMethod, 0);
        }

        // Must we mark all other parameters?
        if (markAllParameters)
        {
            // Mark all parameters, without the 'this' parameter.
            markUsedParameters(programMethod,
                               (accessFlags & ClassConstants.INTERNAL_ACC_STATIC) != 0 ?
                                   -1L : -2L);
        }

        // Is it a native method?
        if ((accessFlags & ClassConstants.INTERNAL_ACC_NATIVE) != 0)
        {
            // Mark all parameters.
            markUsedParameters(programMethod, -1L);
        }

        // Is it an abstract method?
        else if ((accessFlags & ClassConstants.INTERNAL_ACC_ABSTRACT) != 0)
        {
            // Mark the 'this' parameter.
            markParameterUsed(programMethod, 0);
        }

        // Is it a non-native, concrete method?
        else
        {
            // Is the method not static, but synchronized, or can it have
            // other implementations, or is it a class instance initializer?
            if ((accessFlags & ClassConstants.INTERNAL_ACC_STATIC) == 0 &&
                ((accessFlags & ClassConstants.INTERNAL_ACC_SYNCHRONIZED) != 0 ||
                 programClass.mayHaveImplementations(programMethod)            ||
                 programMethod.getName(programClass).equals(ClassConstants.INTERNAL_METHOD_NAME_INIT)))
            {
                // Mark the 'this' parameter.
                markParameterUsed(programMethod, 0);
            }

            // Mark the parameters that are used by the code.
            programMethod.attributesAccept(programClass, this);
        }

        if (DEBUG)
        {
            System.out.print("ParameterUsageMarker: ["+programClass.getName() +"."+programMethod.getName(programClass)+programMethod.getDescriptor(programClass)+"]: ");
            for (int index = 0; index < parameterSize; index++)
            {
                System.out.print(isParameterUsed(programMethod, index) ? '+' : '-');
            }
            System.out.println();
        }

    }

    // Set the parameter size.
    setParameterSize(programMethod, parameterSize);
}
 
Example 7
Source File: VariableOptimizer.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)
    {
//        DEBUG =
//            clazz.getName().equals("abc/Def") &&
//            method.getName(clazz).equals("abc");

        // Initialize the global arrays.
        initializeArrays(codeAttribute);

        // Analyze the liveness of the variables in the code.
        livenessAnalyzer.visitCodeAttribute(clazz, method, codeAttribute);

        // Trim the variables in the local variable tables, because even
        // clipping the tables individually may leave some inconsistencies
        // between them.
            codeAttribute.attributesAccept(clazz, method, this);

        int startIndex =
            (method.getAccessFlags() & ClassConstants.INTERNAL_ACC_STATIC) != 0 ||
            reuseThis ? 0 : 1;

        int parameterSize =
            ClassUtil.internalMethodParameterSize(method.getDescriptor(clazz),
                                                  method.getAccessFlags());

        int variableSize = codeAttribute.u2maxLocals;
        int codeLength   = codeAttribute.u4codeLength;

        boolean remapping = false;

        // Loop over all variables.
        for (int oldIndex = 0; oldIndex < variableSize; oldIndex++)
        {
            // By default, the variable will be mapped onto itself.
            variableMap[oldIndex] = oldIndex;

            // Only try remapping the variable if it's not a parameter.
            if (oldIndex >= parameterSize &&
                oldIndex < MAX_VARIABLES_SIZE)
            {
                // Try to remap the variable to a variable with a smaller index.
                for (int newIndex = startIndex; newIndex < oldIndex; newIndex++)
                {
                    if (areNonOverlapping(oldIndex, newIndex, codeLength))
                    {
                        variableMap[oldIndex] = newIndex;

                        updateLiveness(oldIndex, newIndex, codeLength);

                        remapping = true;

                        // This variable has been remapped. Go to the next one.
                        break;
                    }
                }
            }
        }

        // Have we been able to remap any variables?
        if (remapping)
        {
            if (DEBUG)
            {
                System.out.println("VariableOptimizer: "+clazz.getName()+"."+method.getName(clazz)+method.getDescriptor(clazz));
                for (int index= 0; index < variableSize; index++)
                {
                    System.out.println("  v"+index+" -> "+variableMap[index]);
                }
            }

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

            // Visit the method, if required.
            if (extraVariableMemberVisitor != null)
            {
                method.accept(clazz, extraVariableMemberVisitor);
            }
        }
        else
        {
            // Just clean up any empty variables.
            variableCleaner.visitCodeAttribute(clazz, method, codeAttribute);
        }
    }
 
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: VariableShrinker.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)
{
    if ((method.getAccessFlags() & ClassConstants.INTERNAL_ACC_ABSTRACT) == 0)
    {
        // Compute the parameter size.
        int parameterSize =
            ClassUtil.internalMethodParameterSize(method.getDescriptor(clazz),
                                                  method.getAccessFlags());

        // Get the total size of the local variable frame.
        int maxLocals = codeAttribute.u2maxLocals;

        if (DEBUG)
        {
            System.out.println("VariableShrinker: "+clazz.getName()+"."+method.getName(clazz)+method.getDescriptor(clazz));
            System.out.println("  Parameter size = " + parameterSize);
            System.out.println("  Max locals     = " + maxLocals);
        }

        // Figure out the local variables that are used by the code.
        variableUsageMarker.visitCodeAttribute(clazz, method, codeAttribute);

        // Delete unused local variables from the local variable frame.
        variableEditor.reset(maxLocals);

        for (int variableIndex = parameterSize; variableIndex < maxLocals; variableIndex++)
        {
            // Is the variable not required?
            if (!variableUsageMarker.isVariableUsed(variableIndex))
            {
                if (DEBUG)
                {
                    System.out.println("  Deleting local variable #"+variableIndex);
                }

                // Delete the unused variable.
                variableEditor.deleteVariable(variableIndex);

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

        // Shift all remaining parameters and variables in the byte code.
        variableEditor.visitCodeAttribute(clazz, method, codeAttribute);
    }
}