proguard.util.ArrayUtil Java Examples

The following examples show how to use proguard.util.ArrayUtil. 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: CodeAttributeEditor.java    From proguard with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Extends the size of the accumulated code changes.
 * @param codeLength the length of the code that will be edited next.
 */
public void extend(int codeLength)
{
    // Try to reuse the previous arrays.
    if (preInsertions.length < codeLength)
    {
        preInsertions  = (Instruction[])ArrayUtil.extendArray(preInsertions,  codeLength);
        replacements   = (Instruction[])ArrayUtil.extendArray(replacements,   codeLength);
        postInsertions = (Instruction[])ArrayUtil.extendArray(postInsertions, codeLength);
        deleted        = ArrayUtil.extendArray(deleted, codeLength);
    }
    else
    {
        Arrays.fill(preInsertions,  this.codeLength, codeLength, null);
        Arrays.fill(replacements,   this.codeLength, codeLength, null);
        Arrays.fill(postInsertions, this.codeLength, codeLength, null);
        Arrays.fill(deleted,        this.codeLength, codeLength, false);
    }

    this.codeLength = codeLength;
}
 
Example #2
Source File: CodeAttributeEditor.java    From bazel with Apache License 2.0 6 votes vote down vote up
/**
 * Extends the size of the accumulated code changes.
 * @param codeLength the length of the code that will be edited next.
 */
public void extend(int codeLength)
{
    // Try to reuse the previous arrays.
    if (preInsertions.length < codeLength)
    {
        preInsertions  = (Instruction[])ArrayUtil.extendArray(preInsertions,  codeLength);
        replacements   = (Instruction[])ArrayUtil.extendArray(replacements,   codeLength);
        postInsertions = (Instruction[])ArrayUtil.extendArray(postInsertions, codeLength);
        deleted        = ArrayUtil.extendArray(deleted, codeLength);
    }
    else
    {
        Arrays.fill(preInsertions,  this.codeLength, codeLength, null);
        Arrays.fill(replacements,   this.codeLength, codeLength, null);
        Arrays.fill(postInsertions, this.codeLength, codeLength, null);
        Arrays.fill(deleted,        this.codeLength, codeLength, false);
    }

    this.codeLength = codeLength;
}
 
Example #3
Source File: CodeAttributeComposer.java    From proguard with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Appends the given line number to the line number table.
 * @param lineNumberInfo the line number to be appended.
 */
public void appendLineNumber(LineNumberInfo lineNumberInfo)
{
    if (DEBUG)
    {
        print("         ", "Line number ["+lineNumberInfo.u2startPC+"]");
    }

    // Remap the line number right away.
    visitLineNumberInfo(null, null, null, lineNumberInfo);

    if (DEBUG)
    {
        System.out.println(" -> ["+lineNumberInfo.u2startPC+"] line "+lineNumberInfo.u2lineNumber+(lineNumberInfo.getSource()==null ? "":" ["+lineNumberInfo.getSource()+"]"));
    }

    // Add the line number.
    lineNumberTable =
        (LineNumberInfo[])ArrayUtil.add(lineNumberTable,
                                        lineNumberTableLength++,
                                        lineNumberInfo);
}
 
Example #4
Source File: CodeAttributeComposer.java    From bazel with Apache License 2.0 6 votes vote down vote up
/**
 * Appends the given line number to the line number table.
 * @param lineNumberInfo the line number to be appended.
 */
public void appendLineNumber(LineNumberInfo lineNumberInfo)
{
    if (DEBUG)
    {
        print("         ", "Line number ["+lineNumberInfo.u2startPC+"]");
    }

    // Remap the line number right away.
    visitLineNumberInfo(null, null, null, lineNumberInfo);

    if (DEBUG)
    {
        System.out.println(" -> ["+lineNumberInfo.u2startPC+"] line "+lineNumberInfo.u2lineNumber+(lineNumberInfo.getSource()==null ? "":" ["+lineNumberInfo.getSource()+"]"));
    }

    // Add the line number.
    lineNumberTable =
        (LineNumberInfo[])ArrayUtil.add(lineNumberTable,
                                        lineNumberTableLength++,
                                        lineNumberInfo);
}
 
Example #5
Source File: StackSizeComputer.java    From bazel with Apache License 2.0 6 votes vote down vote up
public void visitCodeAttribute0(Clazz clazz, Method method, CodeAttribute codeAttribute)
{
    if (DEBUG)
    {
        System.out.println("StackSizeComputer: "+clazz.getName()+"."+method.getName(clazz)+method.getDescriptor(clazz));
    }

    int codeLength = codeAttribute.u4codeLength;

    // Make sure the global arrays are sufficiently large.
    evaluated        = ArrayUtil.ensureArraySize(evaluated, codeLength, false);
    stackSizesBefore = ArrayUtil.ensureArraySize(stackSizesBefore, codeLength, 0);
    stackSizesAfter  = ArrayUtil.ensureArraySize(stackSizesAfter,  codeLength, 0);

    // The initial stack is always empty.
    stackSize    = 0;
    maxStackSize = 0;

    // Evaluate the instruction block starting at the entry point of the method.
    evaluateInstructionBlock(clazz, method, codeAttribute, 0);

    // Evaluate the exception handlers.
    codeAttribute.exceptionsAccept(clazz, method, this);
}
 
Example #6
Source File: StackSizeComputer.java    From proguard with GNU General Public License v2.0 6 votes vote down vote up
public void visitCodeAttribute0(Clazz clazz, Method method, CodeAttribute codeAttribute)
{
    if (DEBUG)
    {
        System.out.println("StackSizeComputer: "+clazz.getName()+"."+method.getName(clazz)+method.getDescriptor(clazz));
    }

    int codeLength = codeAttribute.u4codeLength;

    // Make sure the global arrays are sufficiently large.
    evaluated        = ArrayUtil.ensureArraySize(evaluated, codeLength, false);
    stackSizesBefore = ArrayUtil.ensureArraySize(stackSizesBefore, codeLength, 0);
    stackSizesAfter  = ArrayUtil.ensureArraySize(stackSizesAfter,  codeLength, 0);

    // The initial stack is always empty.
    stackSize    = 0;
    maxStackSize = 0;

    // Evaluate the instruction block starting at the entry point of the method.
    evaluateInstructionBlock(clazz, method, codeAttribute, 0);

    // Evaluate the exception handlers.
    codeAttribute.exceptionsAccept(clazz, method, this);
}
 
Example #7
Source File: CodeAttributeComposer.java    From bazel with Apache License 2.0 5 votes vote down vote up
/**
 * Appends the given exception to the exception table.
 * @param exceptionInfo the exception to be appended.
 */
public void appendException(ExceptionInfo exceptionInfo)
{
    if (DEBUG)
    {
        print("         ", "Exception ["+exceptionInfo.u2startPC+" -> "+exceptionInfo.u2endPC+": "+exceptionInfo.u2handlerPC+"]");
    }

    // Remap the exception right away.
    visitExceptionInfo(null, null, null, exceptionInfo);

    if (DEBUG)
    {
        System.out.println(" -> ["+exceptionInfo.u2startPC+" -> "+exceptionInfo.u2endPC+": "+exceptionInfo.u2handlerPC+"]");
    }

    // Don't add the exception if its instruction range is empty.
    if (exceptionInfo.u2startPC == exceptionInfo.u2endPC)
    {
        if (DEBUG)
        {
            println("         ", "  (not added because of empty instruction range)");
        }

        return;
    }

    // Add the exception.
    exceptionTable =
        (ExceptionInfo[])ArrayUtil.add(exceptionTable,
                                       exceptionTableLength++,
                                       exceptionInfo);
}
 
Example #8
Source File: ParameterAnnotationsAttributeEditor.java    From proguard with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Adds a given annotation to the annotations attribute.
 */
public void addAnnotation(int parameterIndex, Annotation annotation)
{
    ArrayUtil.add(targetParameterAnnotationsAttribute.parameterAnnotations[parameterIndex],
                  targetParameterAnnotationsAttribute.u2parameterAnnotationsCount[parameterIndex]++,
                  annotation);
}
 
Example #9
Source File: BootstrapMethodsAttributeEditor.java    From proguard with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Adds a given bootstrap method to the bootstrap methods attribute.
 * @return the index of the bootstrap method.
 */
public int addBootstrapMethodInfo(BootstrapMethodInfo bootstrapMethodInfo)
{
    targetBootstrapMethodsAttribute.bootstrapMethods =
        (BootstrapMethodInfo[])ArrayUtil.add(targetBootstrapMethodsAttribute.bootstrapMethods,
                                             targetBootstrapMethodsAttribute.u2bootstrapMethodsCount,
                                             bootstrapMethodInfo);

    return targetBootstrapMethodsAttribute.u2bootstrapMethodsCount++;
}
 
Example #10
Source File: InterfacesEditor.java    From proguard with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Adds the specified interface to the target class, if it isn't present yet.
 */
public void addInterface(int interfaceConstantIndex)
{
    // Is the interface not yet present?
    if (findInterfaceIndex(interfaceConstantIndex) < 0)
    {
        // Append the interface.
        targetClass.u2interfaces =
            ArrayUtil.add(targetClass.u2interfaces,
                          targetClass.u2interfacesCount++,
                          interfaceConstantIndex);
    }
}
 
Example #11
Source File: LocalVariableTableAttributeEditor.java    From bazel with Apache License 2.0 5 votes vote down vote up
/**
 * Adds a given line number to the line number table attribute.
 */
public void addLocalVariableInfo(LocalVariableInfo localVariableInfo)
{
    targetLocalVariableTableAttribute.localVariableTable =
        (LocalVariableInfo[])ArrayUtil.add(targetLocalVariableTableAttribute.localVariableTable,
                                           targetLocalVariableTableAttribute.u2localVariableTableLength++,
                                           localVariableInfo);
}
 
Example #12
Source File: LocalVariableTypeTableAttributeEditor.java    From proguard with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Adds a given local variable type to the local variable type table attribute.
 */
public void addLocalVariableTypeInfo(LocalVariableTypeInfo localVariableTypeInfo)
{
    targetLocalVariableTypeTableAttribute.localVariableTypeTable =
        (LocalVariableTypeInfo[])ArrayUtil.add(targetLocalVariableTypeTableAttribute.localVariableTypeTable,
                                               targetLocalVariableTypeTableAttribute.u2localVariableTypeTableLength++,
                                               localVariableTypeInfo);
}
 
Example #13
Source File: CodeAttributeComposer.java    From bazel with Apache License 2.0 5 votes vote down vote up
/**
 * Inserts the given line number at the appropriate position in the line
 * number table.
 * @param minimumIndex   the minimum index where the line number may be
 *                       inserted.
 * @param lineNumberInfo the line number to be inserted.
 * @return the index where the line number was inserted.
 */
public int insertLineNumber(int minimumIndex, LineNumberInfo lineNumberInfo)
{
    if (DEBUG)
    {
        print("         ", "Line number ["+lineNumberInfo.u2startPC+"]");
    }

    // Remap the line number right away.
    visitLineNumberInfo(null, null, null, lineNumberInfo);

    if (DEBUG)
    {
        System.out.println(" -> ["+lineNumberInfo.u2startPC+"] line "+lineNumberInfo.u2lineNumber+(lineNumberInfo.getSource()==null ? "":" ["+lineNumberInfo.getSource()+"]"));
    }

    lineNumberTable =
        (LineNumberInfo[])ArrayUtil.extendArray(lineNumberTable,
                                                lineNumberTableLength + 1);

    // Find the insertion index, starting from the end.
    // Don't insert before a negative line number, in case of a tie.
    int index = lineNumberTableLength++;
    while (index > minimumIndex &&
           (lineNumberTable[index - 1].u2startPC    >  lineNumberInfo.u2startPC ||
            lineNumberTable[index - 1].u2startPC    >= lineNumberInfo.u2startPC &&
            lineNumberTable[index - 1].u2lineNumber >= 0))
    {
        lineNumberTable[index] = lineNumberTable[--index];
    }

    lineNumberTable[index] = lineNumberInfo;

    return index;
}
 
Example #14
Source File: CodeAttributeComposer.java    From bazel with Apache License 2.0 5 votes vote down vote up
/**
 * Make sure the code arrays have at least the given size.
 */
private void ensureCodeLength(int newCodeLength)
{
    if (code.length < newCodeLength)
    {
        // Add 20% to avoid extending the arrays too often.
        newCodeLength = newCodeLength * 6 / 5;

        code                  = ArrayUtil.extendArray(code,                  newCodeLength);
        oldInstructionOffsets = ArrayUtil.extendArray(oldInstructionOffsets, newCodeLength);

        instructionWriter.extend(newCodeLength);
    }
}
 
Example #15
Source File: LocalVariableTypeTableAttributeEditor.java    From bazel with Apache License 2.0 5 votes vote down vote up
/**
 * Adds a given local variable type to the local variable type table attribute.
 */
public void addLocalVariableTypeInfo(LocalVariableTypeInfo localVariableTypeInfo)
{
    targetLocalVariableTypeTableAttribute.localVariableTypeTable =
        (LocalVariableTypeInfo[])ArrayUtil.add(targetLocalVariableTypeTableAttribute.localVariableTypeTable,
                                               targetLocalVariableTypeTableAttribute.u2localVariableTypeTableLength++,
                                               localVariableTypeInfo);
}
 
Example #16
Source File: ParameterAnnotationsAttributeEditor.java    From bazel with Apache License 2.0 5 votes vote down vote up
/**
 * Adds a given annotation to the annotations attribute.
 */
public void addAnnotation(int parameterIndex, Annotation annotation)
{
    ArrayUtil.add(targetParameterAnnotationsAttribute.parameterAnnotations[parameterIndex],
                  targetParameterAnnotationsAttribute.u2parameterAnnotationsCount[parameterIndex]++,
                  annotation);
}
 
Example #17
Source File: BootstrapMethodsAttributeEditor.java    From bazel with Apache License 2.0 5 votes vote down vote up
/**
 * Adds a given bootstrap method to the bootstrap methods attribute.
 * @return the index of the bootstrap method.
 */
public int addBootstrapMethodInfo(BootstrapMethodInfo bootstrapMethodInfo)
{
    targetBootstrapMethodsAttribute.bootstrapMethods =
        (BootstrapMethodInfo[])ArrayUtil.add(targetBootstrapMethodsAttribute.bootstrapMethods,
                                             targetBootstrapMethodsAttribute.u2bootstrapMethodsCount,
                                             bootstrapMethodInfo);

    return targetBootstrapMethodsAttribute.u2bootstrapMethodsCount++;
}
 
Example #18
Source File: InterfacesEditor.java    From bazel with Apache License 2.0 5 votes vote down vote up
/**
 * Adds the specified interface to the target class, if it isn't present yet.
 */
public void addInterface(int interfaceConstantIndex)
{
    // Is the interface not yet present?
    if (findInterfaceIndex(interfaceConstantIndex) < 0)
    {
        // Append the interface.
        targetClass.u2interfaces =
            ArrayUtil.add(targetClass.u2interfaces,
                          targetClass.u2interfacesCount++,
                          interfaceConstantIndex);
    }
}
 
Example #19
Source File: CodeAttributeComposer.java    From proguard with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Make sure the code arrays have at least the given size.
 */
private void ensureCodeLength(int newCodeLength)
{
    if (code.length < newCodeLength)
    {
        // Add 20% to avoid extending the arrays too often.
        newCodeLength = newCodeLength * 6 / 5;

        code                  = ArrayUtil.extendArray(code,                  newCodeLength);
        oldInstructionOffsets = ArrayUtil.extendArray(oldInstructionOffsets, newCodeLength);

        instructionWriter.extend(newCodeLength);
    }
}
 
Example #20
Source File: CodeAttributeComposer.java    From proguard with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Inserts the given line number at the appropriate position in the line
 * number table.
 * @param minimumIndex   the minimum index where the line number may be
 *                       inserted.
 * @param lineNumberInfo the line number to be inserted.
 * @return the index where the line number was inserted.
 */
public int insertLineNumber(int minimumIndex, LineNumberInfo lineNumberInfo)
{
    if (DEBUG)
    {
        print("         ", "Line number ["+lineNumberInfo.u2startPC+"]");
    }

    // Remap the line number right away.
    visitLineNumberInfo(null, null, null, lineNumberInfo);

    if (DEBUG)
    {
        System.out.println(" -> ["+lineNumberInfo.u2startPC+"] line "+lineNumberInfo.u2lineNumber+(lineNumberInfo.getSource()==null ? "":" ["+lineNumberInfo.getSource()+"]"));
    }

    lineNumberTable =
        (LineNumberInfo[])ArrayUtil.extendArray(lineNumberTable,
                                                lineNumberTableLength + 1);

    // Find the insertion index, starting from the end.
    // Don't insert before a negative line number, in case of a tie.
    int index = lineNumberTableLength++;
    while (index > minimumIndex &&
           (lineNumberTable[index - 1].u2startPC    >  lineNumberInfo.u2startPC ||
            lineNumberTable[index - 1].u2startPC    >= lineNumberInfo.u2startPC &&
            lineNumberTable[index - 1].u2lineNumber >= 0))
    {
        lineNumberTable[index] = lineNumberTable[--index];
    }

    lineNumberTable[index] = lineNumberInfo;

    return index;
}
 
Example #21
Source File: CodeAttributeComposer.java    From proguard with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Appends the given exception to the exception table.
 * @param exceptionInfo the exception to be appended.
 */
public void appendException(ExceptionInfo exceptionInfo)
{
    if (DEBUG)
    {
        print("         ", "Exception ["+exceptionInfo.u2startPC+" -> "+exceptionInfo.u2endPC+": "+exceptionInfo.u2handlerPC+"]");
    }

    // Remap the exception right away.
    visitExceptionInfo(null, null, null, exceptionInfo);

    if (DEBUG)
    {
        System.out.println(" -> ["+exceptionInfo.u2startPC+" -> "+exceptionInfo.u2endPC+": "+exceptionInfo.u2handlerPC+"]");
    }

    // Don't add the exception if its instruction range is empty.
    if (exceptionInfo.u2startPC == exceptionInfo.u2endPC)
    {
        if (DEBUG)
        {
            println("         ", "  (not added because of empty instruction range)");
        }

        return;
    }

    // Add the exception.
    exceptionTable =
        (ExceptionInfo[])ArrayUtil.add(exceptionTable,
                                       exceptionTableLength++,
                                       exceptionInfo);
}
 
Example #22
Source File: ProgramMethodOptimizationInfo.java    From proguard with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Notifies this object that a parameter is inserted at the given
 * index.
 * @param parameterIndex the parameter index,
 *                       not taking into account the entry size,
 *                       but taking into account the 'this' parameter,
 *                       if any.
 */
public synchronized void insertParameter(int parameterIndex)
{
    // The used parameter bits are indexed with their variable indices
    // (which take into account the sizes of the entries).
    //usedParameters   = insertBit(usedParameters,     parameterIndex, 1L);
    //parameterSize++;

    escapedParameters  = insertBit(escapedParameters,  parameterIndex, 1L);
    escapingParameters = insertBit(escapingParameters, parameterIndex, 1L);
    modifiedParameters = insertBit(modifiedParameters, parameterIndex, 1L);
    returnedParameters = insertBit(returnedParameters, parameterIndex, 1L);
    parameters         = ArrayUtil.insert(parameters, parameters.length, parameterIndex, null);
}
 
Example #23
Source File: LocalVariableTableAttributeEditor.java    From proguard with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Adds a given line number to the line number table attribute.
 */
public void addLocalVariableInfo(LocalVariableInfo localVariableInfo)
{
    targetLocalVariableTableAttribute.localVariableTable =
        (LocalVariableInfo[])ArrayUtil.add(targetLocalVariableTableAttribute.localVariableTable,
                                           targetLocalVariableTableAttribute.u2localVariableTableLength++,
                                           localVariableInfo);
}
 
Example #24
Source File: ProgramMethodOptimizationInfo.java    From proguard with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Notifies this object that the specified parameter is removed.
 * @param parameterIndex the parameter index,
 *                       not taking into account the entry size,
 *                       but taking into account the 'this' parameter,
 *                       if any.
 */
public synchronized void removeParameter(int parameterIndex)
{
    // The used parameter bits are indexed with their variable indices
    // (which take into account the sizes of the entries).
    //usedParameters   = removeBit(usedParameters,     parameterIndex, 1L);
    //parameterSize--;

    escapedParameters  = removeBit(escapedParameters,  parameterIndex, 1L);
    escapingParameters = removeBit(escapingParameters, parameterIndex, 1L);
    modifiedParameters = removeBit(modifiedParameters, parameterIndex, 1L);
    returnedParameters = removeBit(returnedParameters, parameterIndex, 1L);
    ArrayUtil.remove(parameters, parameters.length, parameterIndex);
}
 
Example #25
Source File: ReferenceEscapeChecker.java    From proguard with GNU General Public License v2.0 5 votes vote down vote up
public void visitCodeAttribute(Clazz clazz, Method method, CodeAttribute codeAttribute)
{
    // Evaluate the method.
    if (runPartialEvaluator)
    {
        partialEvaluator.visitCodeAttribute(clazz, method, codeAttribute);
    }

    int codeLength = codeAttribute.u4codeLength;

    // Initialize the global arrays.
    instanceEscaping = ArrayUtil.ensureArraySize(instanceEscaping, codeLength, false);
    instanceReturned = ArrayUtil.ensureArraySize(instanceReturned, codeLength, false);
    instanceModified = ArrayUtil.ensureArraySize(instanceModified, codeLength, false);
    externalInstance = ArrayUtil.ensureArraySize(externalInstance, codeLength, false);

    // Mark the parameters and instances that are escaping from the code.
    codeAttribute.instructionsAccept(clazz, method, partialEvaluator.tracedInstructionFilter(this));

    if (DEBUG)
    {
        System.out.println();
        System.out.println("ReferenceEscapeChecker: ["+clazz.getName()+"."+method.getName(clazz)+method.getDescriptor(clazz)+"]");

        for (int index = 0; index < codeLength; index++)
        {
            if (partialEvaluator.isInstruction(index))
            {
                System.out.println("  " +
                                   (instanceEscaping[index] ? 'E' : '.') +
                                   (instanceReturned[index] ? 'R' : '.') +
                                   (instanceModified[index] ? 'M' : '.') +
                                   (externalInstance[index] ? 'X' : '.') +
                                   ' ' +
                                   InstructionFactory.create(codeAttribute.code, index).toString(index));
            }
        }
    }
}
 
Example #26
Source File: MemberValueSpecification.java    From proguard with GNU General Public License v2.0 5 votes vote down vote up
public boolean equals(Object object)
{
    if (object == null)
    {
        return false;
    }

    MemberValueSpecification other = (MemberValueSpecification)object;
    return
        super.equals(other) &&
        ArrayUtil.equalOrNull(values, other.values);
}
 
Example #27
Source File: TargetClassChanger.java    From proguard with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Updates the retargeted classes in the given array of unique classes.
 * Optionally gets a class to avoid in the results.
 */
private int updateUniqueReferencedClasses(Clazz[] referencedClasses,
                                          int     referencedClassCount,
                                          Clazz   avoidClass)
{
    int newIndex = 0;
    for (int index = 0; index < referencedClassCount; index++)
    {
        Clazz referencedClass = referencedClasses[index];

        // Isn't the subclass being retargeted?
        Clazz targetClass = ClassMerger.getTargetClass(referencedClasses[index]);
        if (targetClass == null)
        {
            // Keep the original class.
            referencedClasses[newIndex++] = referencedClass;
        }
        // Isn't the targeted class present yet?
        else if (!targetClass.equals(avoidClass) &&
                 ArrayUtil.indexOf(referencedClasses, referencedClassCount, targetClass) < 0)
        {
            // Replace the original class by its targeted class.
            referencedClasses[newIndex++] = targetClass;
        }
    }

    // Clear the remaining elements.
    Arrays.fill(referencedClasses, newIndex, referencedClassCount, null);

    return newIndex;
}
 
Example #28
Source File: LambdaExpressionCollector.java    From proguard with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void visitBootstrapMethodInfo(Clazz clazz, BootstrapMethodInfo bootstrapMethodInfo)
{
    ProgramClass programClass = (ProgramClass) clazz;

    MethodHandleConstant bootstrapMethodHandle =
        (MethodHandleConstant) programClass.getConstant(bootstrapMethodInfo.u2methodHandleIndex);

    if (isLambdaMetaFactory(bootstrapMethodHandle.getClassName(clazz)))
    {
        String factoryMethodDescriptor =
            referencedInvokeDynamicConstant.getType(clazz);

        String interfaceClassName =
            ClassUtil.internalClassNameFromClassType(ClassUtil.internalMethodReturnType(factoryMethodDescriptor));

        // Find the actual method that is being invoked.
        MethodHandleConstant invokedMethodHandle =
            (MethodHandleConstant) programClass.getConstant(bootstrapMethodInfo.u2methodArguments[1]);

        referencedInvokedClass  = null;
        referencedInvokedMethod = null;
        clazz.constantPoolEntryAccept(invokedMethodHandle.u2referenceIndex, this);

        // Collect all the useful information.
        LambdaExpression lambdaExpression =
            new LambdaExpression(programClass,
                                 referencedBootstrapMethodIndex,
                                 bootstrapMethodInfo,
                                 factoryMethodDescriptor,
                                 new String[] { interfaceClassName },
                                 new String[0],
                                 referencedInvokeDynamicConstant.getName(clazz),
                                 getMethodTypeConstant(programClass, bootstrapMethodInfo.u2methodArguments[0]).getType(clazz),
                                 invokedMethodHandle.getReferenceKind(),
                                 invokedMethodHandle.getClassName(clazz),
                                 invokedMethodHandle.getName(clazz),
                                 invokedMethodHandle.getType(clazz),
                                 referencedInvokedClass,
                                 referencedInvokedMethod);

        if (isAlternateFactoryMethod(bootstrapMethodHandle.getName(clazz)))
        {
            int flags =
                getIntegerConstant(programClass,
                                   bootstrapMethodInfo.u2methodArguments[3]);

            // For the alternate metafactory, the optional arguments start
            // at index 4.
            int argumentIndex = 4;

            if ((flags & BootstrapMethodInfo.FLAG_MARKERS) != 0)
            {
                int markerInterfaceCount =
                    getIntegerConstant(programClass,
                                       bootstrapMethodInfo.u2methodArguments[argumentIndex++]);

                for (int i = 0; i < markerInterfaceCount; i++)
                {
                    String interfaceName =
                        programClass.getClassName(bootstrapMethodInfo.u2methodArguments[argumentIndex++]);

                    lambdaExpression.interfaces =
                        ArrayUtil.add(lambdaExpression.interfaces,
                                      lambdaExpression.interfaces.length,
                                      interfaceName);
                }
            }

            if ((flags & BootstrapMethodInfo.FLAG_BRIDGES) != 0)
            {
                int bridgeMethodCount =
                    getIntegerConstant(programClass,
                                       bootstrapMethodInfo.u2methodArguments[argumentIndex++]);

                for (int i = 0; i < bridgeMethodCount; i++)
                {
                    MethodTypeConstant methodTypeConstant =
                        getMethodTypeConstant(programClass,
                                              bootstrapMethodInfo.u2methodArguments[argumentIndex++]);

                    lambdaExpression.bridgeMethodDescriptors =
                        ArrayUtil.add(lambdaExpression.bridgeMethodDescriptors,
                                      lambdaExpression.bridgeMethodDescriptors.length,
                                      methodTypeConstant.getType(programClass));
                }
            }

            if ((flags & BootstrapMethodInfo.FLAG_SERIALIZABLE) != 0)
            {
                lambdaExpression.interfaces =
                    ArrayUtil.add(lambdaExpression.interfaces,
                                  lambdaExpression.interfaces.length,
                                  ClassConstants.NAME_JAVA_IO_SERIALIZABLE);
            }
        }

        lambdaExpressions.put(referencedBootstrapMethodIndex, lambdaExpression);
    }
}
 
Example #29
Source File: InstructionUsageMarker.java    From proguard with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Initializes the necessary data structure.
 */
private void initializeNecessary(CodeAttribute codeAttribute)
{
    int codeLength = codeAttribute.u4codeLength;
    int maxLocals  = codeAttribute.u2maxLocals;
    int maxStack   = codeAttribute.u2maxStack;

    // Create new arrays for storing information at each instruction offset.
    reverseDependencies =
        ArrayUtil.ensureArraySize(reverseDependencies, codeLength, null);

    if (stacksNecessaryAfter.length    < codeLength ||
        stacksNecessaryAfter[0].length < maxStack)
    {
        stacksNecessaryAfter = new boolean[codeLength][maxStack];
    }
    else
    {
        for (int offset = 0; offset < codeLength; offset++)
        {
            Arrays.fill(stacksNecessaryAfter[offset], 0, maxStack, false);
        }
    }

    if (stacksUnwantedBefore.length    < codeLength ||
        stacksUnwantedBefore[0].length < maxStack)
    {
        stacksUnwantedBefore = new boolean[codeLength][maxStack];
    }
    else
    {
        for (int offset = 0; offset < codeLength; offset++)
        {
            Arrays.fill(stacksUnwantedBefore[offset], 0, maxStack, false);
        }
    }

    instructionsNecessary =
        ArrayUtil.ensureArraySize(instructionsNecessary,
                                  codeLength,
                                  false);

    extraPushPopInstructionsNecessary =
        ArrayUtil.ensureArraySize(extraPushPopInstructionsNecessary,
                                  codeLength,
                                  false);
}
 
Example #30
Source File: TypeArgumentFinder.java    From proguard with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void visitVariableInstruction(Clazz               clazz,
                                     Method              method,
                                     CodeAttribute       codeAttribute,
                                     int                 offset,
                                     VariableInstruction variableInstruction)
{
    if (variableInstruction.canonicalOpcode() == Instruction.OP_ALOAD)
    {
        // Find the operation that stored the loaded Type.
        LastStoreFinder lastStoreFinder = new LastStoreFinder(variableInstruction.variableIndex);
        codeAttribute.instructionsAccept(clazz, method, 0, offset, lastStoreFinder);

        if (lastStoreFinder.lastStore != null)
        {
            // Find out which instruction produced the stored Type.
            TracedStack stackBeforeStore = partialEvaluator.getStackBefore(lastStoreFinder.lastStoreOffset);
            InstructionOffsetValue instructionOffsetValue = stackBeforeStore.getTopProducerValue(0).instructionOffsetValue();

            // Derive the signature of the subclass of TypeToken from which the Type is retrieved.
            TypeTokenSignatureFinder typeTokenFinder = new TypeTokenSignatureFinder();
            for (int offsetIndex = 0; offsetIndex < instructionOffsetValue.instructionOffsetCount(); offsetIndex++)
            {
                int instructionOffset = instructionOffsetValue.instructionOffset(offsetIndex);
                codeAttribute.instructionAccept(clazz, method, instructionOffset, typeTokenFinder);
            }

            // Derive the classes from the signature of the TypeToken subclass.
            if (typeTokenFinder.typeTokenSignature != null)
            {
                typeArgumentClasses = new String[0];
                Clazz[] referencedClasses = typeTokenFinder.typeTokenSignature.referencedClasses;
                for (Clazz referencedClass : referencedClasses)
                {
                    if (referencedClass!= null &&
                        !referencedClass.getName().equals(GsonClassConstants.NAME_TYPE_TOKEN))
                    {
                        typeArgumentClasses = ArrayUtil.add(typeArgumentClasses,
                                                            typeArgumentClasses.length,
                                                            referencedClass.getName());
                    }
                }
            }
        }
    }
}