Java Code Examples for proguard.classfile.editor.ConstantPoolEditor

The following examples show how to use proguard.classfile.editor.ConstantPoolEditor. These examples are extracted from open source projects. 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 Project: proguard   Source File: ClassRenamer.java    License: GNU General Public License v2.0 6 votes vote down vote up
public void visitProgramMember(ProgramClass  programClass,
                                 ProgramMember programMember)
{
    // Has the class member name changed?
    String name    = programMember.getName(programClass);
    String newName = MemberObfuscator.newMemberName(programMember);
    if (newName != null && !newName.equals(name))
    {
        programMember.u2nameIndex =
            new ConstantPoolEditor(programClass).addUtf8Constant(newName);

        if (extraMemberVisitor != null)
        {
            programMember.accept(programClass, extraMemberVisitor);
        }
    }
}
 
Example 2
Source Project: proguard   Source File: ClassRenamer.java    License: GNU General Public License v2.0 6 votes vote down vote up
public void visitClassConstant(Clazz clazz, ClassConstant classConstant)
{
    // Update the Class entry if required.
    String name    = clazz.getName();
    String newName = ClassObfuscator.newClassName(clazz);
    if (newName != null && !newName.equals(name))
    {
        // Refer to a new Utf8 entry.
        classConstant.u2nameIndex =
            new ConstantPoolEditor((ProgramClass)clazz).addUtf8Constant(newName);

        if (extraClassVisitor != null)
        {
            clazz.accept(extraClassVisitor);
        }
    }
}
 
Example 3
Source Project: java-n-IDE-for-Android   Source File: TargetClassChanger.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Explicitly adds a new class constant for the given class in the given
 * program class.
 */
private int addNewClassConstant(ProgramClass programClass,
                                String       className,
                                Clazz        referencedClass)
{
    ConstantPoolEditor constantPoolEditor =
        new ConstantPoolEditor(programClass);

    int nameIndex =
        constantPoolEditor.addUtf8Constant(className);

    int classConstantIndex =
        constantPoolEditor.addConstant(new ClassConstant(nameIndex,
                                                         referencedClass));
    return classConstantIndex;
}
 
Example 4
public void visitSignatureAttribute(Clazz clazz, Method method, SignatureAttribute signatureAttribute)
{
    // Compute the new signature.
    String signature    = clazz.getString(signatureAttribute.u2signatureIndex);
    String newSignature = shrinkDescriptor(method, signature);

    // Update the signature.
    signatureAttribute.u2signatureIndex =
        new ConstantPoolEditor((ProgramClass)clazz).addUtf8Constant(newSignature);

    // Update the referenced classes.
    signatureAttribute.referencedClasses =
        shrinkReferencedClasses(method,
                                signature,
                                signatureAttribute.referencedClasses);
}
 
Example 5
public void visitSignatureAttribute(Clazz clazz, Method method, SignatureAttribute signatureAttribute)
{
    String descriptor      = method.getDescriptor(clazz);
    int    descriptorIndex = descriptor.indexOf(TypeConstants.METHOD_ARGUMENTS_CLOSE);
    String signature       = signatureAttribute.getSignature(clazz);
    int    signatureIndex  = signature.indexOf(TypeConstants.METHOD_ARGUMENTS_CLOSE);

    String newSignature = signature.substring(0, signatureIndex) +
                          descriptor.charAt(descriptorIndex - 1) +
                          signature.substring(signatureIndex);

    // Update the signature.
    signatureAttribute.u2signatureIndex =
        new ConstantPoolEditor((ProgramClass)clazz).addUtf8Constant(newSignature);
}
 
Example 6
public void visitSignatureAttribute(Clazz clazz, Method method, SignatureAttribute signatureAttribute)
{
    if (DEBUG)
    {
        System.out.println("  ["+signatureAttribute.getSignature(clazz)+"]");
    }

    // Compute the new signature.
    String signature = signatureAttribute.getSignature(clazz);

    // Constructors of enum classes and of non-static inner classes may
    // start with one or two synthetic parameters, which are not part
    // of the signature.
    int syntheticParametersSize =
        new InternalTypeEnumeration(method.getDescriptor(clazz)).typesSize() -
        new InternalTypeEnumeration(signature).typesSize();

    String newSignature = shrinkDescriptor(method,
                                           signature,
                                           syntheticParametersSize);

    if (!newSignature.equals(signature))
    {
        // Update the signature.
        signatureAttribute.u2signatureIndex =
            new ConstantPoolEditor((ProgramClass)clazz).addUtf8Constant(newSignature);

        // Update the referenced classes.
        signatureAttribute.referencedClasses =
            shrinkReferencedClasses(method,
                                    signature,
                                    syntheticParametersSize,
                                    signatureAttribute.referencedClasses);

        if (DEBUG)
        {
            System.out.println("    -> ["+newSignature+"]");
        }
    }
}
 
Example 7
Source Project: java-n-IDE-for-Android   Source File: CodePreverifier.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Finds or creates a class constant for the given reference value, and
 * returns its index in the constant pool.
 */
private int createClassConstant(ProgramClass   programClass,
                                ReferenceValue referenceValue)
{
    return new ConstantPoolEditor(programClass).addClassConstant(referenceValue.getType(),
                                                                 referenceValue.getReferencedClass());
}
 
Example 8
public void visitSignatureAttribute(Clazz clazz, Method method, SignatureAttribute signatureAttribute)
{
    String descriptor      = method.getDescriptor(clazz);
    int    descriptorIndex = descriptor.indexOf(ClassConstants.INTERNAL_METHOD_ARGUMENTS_CLOSE);
    String signature       = clazz.getString(signatureAttribute.u2signatureIndex);
    int    signatureIndex  = signature.indexOf(ClassConstants.INTERNAL_METHOD_ARGUMENTS_CLOSE);

    String newSignature = signature.substring(0, signatureIndex) +
                          descriptor.charAt(descriptorIndex - 1) +
                          signature.substring(signatureIndex);

    // Update the signature.
    signatureAttribute.u2signatureIndex =
        new ConstantPoolEditor((ProgramClass)clazz).addUtf8Constant(newSignature);
}
 
Example 9
Source Project: java-n-IDE-for-Android   Source File: ClassRenamer.java    License: Apache License 2.0 5 votes vote down vote up
public void visitProgramMember(ProgramClass programClass,
                                 ProgramMember programMember)
{
    // Has the class member name changed?
    String name    = programMember.getName(programClass);
    String newName = MemberObfuscator.newMemberName(programMember);
    if (newName != null &&
        !newName.equals(name))
    {
        programMember.u2nameIndex =
            new ConstantPoolEditor(programClass).addUtf8Constant(newName);
    }
}
 
Example 10
Source Project: java-n-IDE-for-Android   Source File: ClassRenamer.java    License: Apache License 2.0 5 votes vote down vote up
public void visitClassConstant(Clazz clazz, ClassConstant classConstant)
{
    // Update the Class entry if required.
    String newName = ClassObfuscator.newClassName(clazz);
    if (newName != null)
    {
        // Refer to a new Utf8 entry.
        classConstant.u2nameIndex =
            new ConstantPoolEditor((ProgramClass)clazz).addUtf8Constant(newName);
    }
}
 
Example 11
public void visitSignatureAttribute(Clazz clazz, Method method, SignatureAttribute signatureAttribute)
{
    String descriptor      = method.getDescriptor(clazz);
    int    descriptorIndex = descriptor.indexOf(ClassConstants.METHOD_ARGUMENTS_CLOSE);
    String signature       = signatureAttribute.getSignature(clazz);
    int    signatureIndex  = signature.indexOf(ClassConstants.METHOD_ARGUMENTS_CLOSE);

    String newSignature = signature.substring(0, signatureIndex) +
                          descriptor.charAt(descriptorIndex - 1) +
                          signature.substring(signatureIndex);

    // Update the signature.
    signatureAttribute.u2signatureIndex =
        new ConstantPoolEditor((ProgramClass)clazz).addUtf8Constant(newSignature);
}
 
Example 12
public void visitSignatureAttribute(Clazz clazz, Method method, SignatureAttribute signatureAttribute)
{
    if (DEBUG)
    {
        System.out.println("  ["+signatureAttribute.getSignature(clazz)+"]");
    }

    // Compute the new signature.
    String signature    = signatureAttribute.getSignature(clazz);
    String newSignature = shrinkDescriptor(method, signature);

    if (!newSignature.equals(signature))
    {
        // Update the signature.
        signatureAttribute.u2signatureIndex =
            new ConstantPoolEditor((ProgramClass)clazz).addUtf8Constant(newSignature);

        // Update the referenced classes.
        signatureAttribute.referencedClasses =
            shrinkReferencedClasses(method,
                                    signature,
                                    signatureAttribute.referencedClasses);

        if (DEBUG)
        {
            System.out.println("    -> ["+newSignature+"]");
        }
    }
}
 
Example 13
Source Project: proguard   Source File: ClassRenamer.java    License: GNU General Public License v2.0 5 votes vote down vote up
public void visitProgramMember(ProgramClass  programClass,
                                 ProgramMember programMember)
{
    // Has the class member name changed?
    String name    = programMember.getName(programClass);
    String newName = MemberObfuscator.newMemberName(programMember);
    if (newName != null &&
        !newName.equals(name))
    {
        programMember.u2nameIndex =
            new ConstantPoolEditor(programClass).addUtf8Constant(newName);
    }
}
 
Example 14
Source Project: proguard   Source File: ClassRenamer.java    License: GNU General Public License v2.0 5 votes vote down vote up
public void visitClassConstant(Clazz clazz, ClassConstant classConstant)
{
    // Update the Class entry if required.
    String newName = ClassObfuscator.newClassName(clazz);
    if (newName != null)
    {
        // Refer to a new Utf8 entry.
        classConstant.u2nameIndex =
            new ConstantPoolEditor((ProgramClass)clazz).addUtf8Constant(newName);
    }
}
 
Example 15
Source Project: bazel   Source File: DuplicateInitializerFixer.java    License: Apache License 2.0 5 votes vote down vote up
public void visitSignatureAttribute(Clazz clazz, Method method, SignatureAttribute signatureAttribute)
{
    String descriptor      = method.getDescriptor(clazz);
    int    descriptorIndex = descriptor.indexOf(ClassConstants.METHOD_ARGUMENTS_CLOSE);
    String signature       = signatureAttribute.getSignature(clazz);
    int    signatureIndex  = signature.indexOf(ClassConstants.METHOD_ARGUMENTS_CLOSE);

    String newSignature = signature.substring(0, signatureIndex) +
                          descriptor.charAt(descriptorIndex - 1) +
                          signature.substring(signatureIndex);

    // Update the signature.
    signatureAttribute.u2signatureIndex =
        new ConstantPoolEditor((ProgramClass)clazz).addUtf8Constant(newSignature);
}
 
Example 16
Source Project: bazel   Source File: MethodDescriptorShrinker.java    License: Apache License 2.0 5 votes vote down vote up
public void visitSignatureAttribute(Clazz clazz, Method method, SignatureAttribute signatureAttribute)
{
    if (DEBUG)
    {
        System.out.println("  ["+signatureAttribute.getSignature(clazz)+"]");
    }

    // Compute the new signature.
    String signature    = signatureAttribute.getSignature(clazz);
    String newSignature = shrinkDescriptor(method, signature);

    if (!newSignature.equals(signature))
    {
        // Update the signature.
        signatureAttribute.u2signatureIndex =
            new ConstantPoolEditor((ProgramClass)clazz).addUtf8Constant(newSignature);

        // Update the referenced classes.
        signatureAttribute.referencedClasses =
            shrinkReferencedClasses(method,
                                    signature,
                                    signatureAttribute.referencedClasses);

        if (DEBUG)
        {
            System.out.println("    -> ["+newSignature+"]");
        }
    }
}
 
Example 17
Source Project: bazel   Source File: ClassRenamer.java    License: Apache License 2.0 5 votes vote down vote up
public void visitProgramMember(ProgramClass  programClass,
                                 ProgramMember programMember)
{
    // Has the class member name changed?
    String name    = programMember.getName(programClass);
    String newName = MemberObfuscator.newMemberName(programMember);
    if (newName != null &&
        !newName.equals(name))
    {
        programMember.u2nameIndex =
            new ConstantPoolEditor(programClass).addUtf8Constant(newName);
    }
}
 
Example 18
Source Project: bazel   Source File: ClassRenamer.java    License: Apache License 2.0 5 votes vote down vote up
public void visitClassConstant(Clazz clazz, ClassConstant classConstant)
{
    // Update the Class entry if required.
    String newName = ClassObfuscator.newClassName(clazz);
    if (newName != null)
    {
        // Refer to a new Utf8 entry.
        classConstant.u2nameIndex =
            new ConstantPoolEditor((ProgramClass)clazz).addUtf8Constant(newName);
    }
}
 
Example 19
public void visitProgramMethod(ProgramClass programClass, ProgramMethod programMethod)
{
    // Is it a class instance initializer?
    String name = programMethod.getName(programClass);
    if (name.equals(ClassConstants.METHOD_NAME_INIT))
    {
        // Is there already another initializer with the same descriptor?
        String descriptor    = programMethod.getDescriptor(programClass);
        Method similarMethod = programClass.findMethod(name, descriptor);
        if (!programMethod.equals(similarMethod))
        {
            // Should this initializer be preserved?
            if (KeepMarker.isKept(programMethod))
            {
                // Fix the other initializer.
                // We'll just proceed if it is being kept as well;
                // apparently the descriptor types didn't matter so much.
                programMethod = (ProgramMethod)similarMethod;
            }

            int index = descriptor.indexOf(TypeConstants.METHOD_ARGUMENTS_CLOSE);

            // Try to find a new, unique descriptor.
            int typeCounter = 0;
            while (true)
            {
                // Construct the new descriptor by inserting a new type
                // as an additional last argument.
                StringBuffer newDescriptorBuffer =
                    new StringBuffer(descriptor.substring(0, index));

                for (int arrayDimension = 0; arrayDimension < typeCounter / TYPES.length; arrayDimension++)
                {
                    newDescriptorBuffer.append(TypeConstants.ARRAY);
                }

                newDescriptorBuffer.append(TYPES[typeCounter % TYPES.length]);
                newDescriptorBuffer.append(descriptor.substring(index));

                String newDescriptor = newDescriptorBuffer.toString();

                // Is the new initializer descriptor unique?
                if (programClass.findMethod(name, newDescriptor) == null)
                {
                    if (DEBUG)
                    {
                        System.out.println("DuplicateInitializerFixer:");
                        System.out.println("  ["+programClass.getName()+"."+name+descriptor+"] ("+ClassUtil.externalClassAccessFlags(programMethod.getAccessFlags())+") -> ["+newDescriptor+"]");
                    }

                    // Update the descriptor.
                    programMethod.u2descriptorIndex =
                        new ConstantPoolEditor(programClass).addUtf8Constant(newDescriptor);

                    // Fix the local variable frame size, the method
                    // signature, and the parameter annotations, if
                    // necessary.
                    programMethod.attributesAccept(programClass,
                                                   this);

                    // Update the optimization info.
                    MethodOptimizationInfo methodOptimizationInfo =
                        ProgramMethodOptimizationInfo.getMethodOptimizationInfo(programMethod);
                    if (methodOptimizationInfo instanceof ProgramMethodOptimizationInfo)
                    {
                        ProgramMethodOptimizationInfo programMethodOptimizationInfo =
                            (ProgramMethodOptimizationInfo)methodOptimizationInfo;

                        int parameterCount =
                            ClassUtil.internalMethodParameterCount(newDescriptor,
                                                                   programMethod.getAccessFlags());
                        programMethodOptimizationInfo.insertParameter(parameterCount - 1);

                        int parameterSize =
                            programMethodOptimizationInfo.getParameterSize();
                        programMethodOptimizationInfo.setParameterSize(parameterSize + 1);
                        programMethodOptimizationInfo.setParameterUsed(parameterSize);
                    }

                    // Visit the initializer, if required.
                    if (extraFixedInitializerVisitor != null)
                    {
                        extraFixedInitializerVisitor.visitProgramMethod(programClass, programMethod);
                    }

                    // We're done with this constructor.
                    return;
                }

                typeCounter++;
            }
        }
    }
}
 
Example 20
public void visitProgramMethod(ProgramClass programClass, ProgramMethod programMethod)
{
    if (DEBUG)
    {
        System.out.println("MethodDescriptorShrinker: ["+programClass.getName()+"."+programMethod.getName(programClass)+programMethod.getDescriptor(programClass)+"]");
    }

    // Update the descriptor if it has any unused parameters.
    String descriptor    = programMethod.getDescriptor(programClass);
    String newDescriptor = shrinkDescriptor(programMethod, descriptor, 0);

    if (!newDescriptor.equals(descriptor))
    {
        // Shrink the signature and parameter annotations,
        // before shrinking the descriptor itself.
        programMethod.attributesAccept(programClass, this);

        String name    = programMethod.getName(programClass);
        String newName = name;

        // Append a code, if the method isn't a class instance initializer.
        if (!name.equals(ClassConstants.METHOD_NAME_INIT))
        {
            newName += TypeConstants.SPECIAL_MEMBER_SEPARATOR + Long.toHexString(Math.abs((descriptor).hashCode()));
        }

        ConstantPoolEditor constantPoolEditor =
            new ConstantPoolEditor(programClass);

        // Update the name, if necessary.
        if (!newName.equals(name))
        {
            programMethod.u2nameIndex =
                constantPoolEditor.addUtf8Constant(newName);
        }

        // Update the referenced classes.
        programMethod.referencedClasses =
            shrinkReferencedClasses(programMethod,
                                    descriptor,
                                    0,
                                    programMethod.referencedClasses);

        // Finally, update the descriptor itself.
        programMethod.u2descriptorIndex =
            constantPoolEditor.addUtf8Constant(newDescriptor);

        if (DEBUG)
        {
            System.out.println("    -> ["+newName+newDescriptor+"]");
        }

        // Visit the method, if required.
        if (extraMemberVisitor != null)
        {
            extraMemberVisitor.visitProgramMethod(programClass, programMethod);
        }
    }
}
 
Example 21
Source Project: proguard   Source File: SourceFileRenamer.java    License: GNU General Public License v2.0 4 votes vote down vote up
public void visitSourceFileAttribute(Clazz clazz, SourceFileAttribute sourceFileAttribute)
{
    // Fix the source file attribute.
    sourceFileAttribute.u2sourceFileIndex =
        new ConstantPoolEditor((ProgramClass)clazz).addUtf8Constant(newSourceFileAttribute);
}
 
Example 22
Source Project: proguard   Source File: SourceFileRenamer.java    License: GNU General Public License v2.0 4 votes vote down vote up
public void visitSourceDirAttribute(Clazz clazz, SourceDirAttribute sourceDirAttribute)
{
    // Fix the source file attribute.
    sourceDirAttribute.u2sourceDirIndex =
        new ConstantPoolEditor((ProgramClass)clazz).addUtf8Constant(newSourceFileAttribute);
}
 
Example 23
/**
 * 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);
            }
        }
    }
}
 
Example 24
/**
 * Replaces the long pushing instruction at the given offset by a simpler
 * push instruction, if possible.
 */
private void replaceLongPushInstruction(Clazz       clazz,
                                        int         offset,
                                        Instruction instruction,
                                        int         maxVariableIndex)
{
    Value pushedValue = partialEvaluator.getStackAfter(offset).getTop(0);
    if (pushedValue.isParticular())
    {
        long value = pushedValue.longValue().value();
        if (value == 0L ||
            value == 1L)
        {
            replaceConstantPushInstruction(clazz,
                                   offset,
                                   instruction,
                                   InstructionConstants.OP_LCONST_0,
                                   (int)value);
        }
        else
        {
            ConstantPoolEditor constantPoolEditor =
                new ConstantPoolEditor((ProgramClass)clazz);

            Instruction replacementInstruction =
                new ConstantInstruction(InstructionConstants.OP_LDC2_W,
                                        constantPoolEditor.addLongConstant(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_LLOAD,
                                               variableIndex);
            }
        }
    }
}
 
Example 25
/**
 * Replaces the float pushing instruction at the given offset by a simpler
 * push instruction, if possible.
 */
private void replaceFloatPushInstruction(Clazz       clazz,
                                         int         offset,
                                         Instruction instruction,
                                         int         maxVariableIndex)
{
    Value pushedValue = partialEvaluator.getStackAfter(offset).getTop(0);
    if (pushedValue.isParticular())
    {
        // Make sure to distinguish between +0.0 and -0.0.
        float value = pushedValue.floatValue().value();
        if (value == 0.0f && Float.floatToIntBits(value) == POS_ZERO_FLOAT_BITS ||
            value == 1.0f ||
            value == 2.0f)
        {
            replaceConstantPushInstruction(clazz,
                                           offset,
                                           instruction,
                                           InstructionConstants.OP_FCONST_0,
                                           (int)value);
        }
        else
        {
            ConstantPoolEditor constantPoolEditor =
                new ConstantPoolEditor((ProgramClass)clazz);

            Instruction replacementInstruction =
                new ConstantInstruction(InstructionConstants.OP_LDC,
                                        constantPoolEditor.addFloatConstant(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_FLOAD,
                                               variableIndex);
            }
        }
    }
}
 
Example 26
/**
 * Replaces the double pushing instruction at the given offset by a simpler
 * push instruction, if possible.
 */
private void replaceDoublePushInstruction(Clazz       clazz,
                                          int         offset,
                                          Instruction instruction,
                                          int         maxVariableIndex)
{
    Value pushedValue = partialEvaluator.getStackAfter(offset).getTop(0);
    if (pushedValue.isParticular())
    {
        // Make sure to distinguish between +0.0 and -0.0.
        double value = pushedValue.doubleValue().value();
        if (value == 0.0 && Double.doubleToLongBits(value) == POS_ZERO_DOUBLE_BITS ||
            value == 1.0)
        {
            replaceConstantPushInstruction(clazz,
                                           offset,
                                           instruction,
                                           InstructionConstants.OP_DCONST_0,
                                           (int)value);
        }
        else
        {
            ConstantPoolEditor constantPoolEditor =
                new ConstantPoolEditor((ProgramClass)clazz);

            Instruction replacementInstruction =
                new ConstantInstruction(InstructionConstants.OP_LDC2_W,
                                        constantPoolEditor.addDoubleConstant(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_DLOAD,
                                               variableIndex);
            }
        }
    }
}
 
Example 27
public void visitProgramMethod(ProgramClass programClass, ProgramMethod programMethod)
{
    // Is it a class instance initializer?
    String name = programMethod.getName(programClass);
    if (name.equals(ClassConstants.INTERNAL_METHOD_NAME_INIT))
    {
        // Is there already another initializer with the same descriptor?
        String descriptor    = programMethod.getDescriptor(programClass);
        Method similarMethod = programClass.findMethod(name, descriptor);
        if (!programMethod.equals(similarMethod))
        {
            // Should this initializer be preserved?
            if (!KeepMarker.isKept(programMethod))
            {
                // Fix the other initializer.
                programMethod = (ProgramMethod)similarMethod;
            }

            int index = descriptor.indexOf(ClassConstants.INTERNAL_METHOD_ARGUMENTS_CLOSE);

            // Try to find a new, unique descriptor.
            for (int typeIndex = 0; typeIndex < TYPES.length; typeIndex++)
            {
                String newDescriptor =
                    descriptor.substring(0, index) +
                    TYPES[typeIndex] +
                    descriptor.substring(index);

                // Is the new initializer descriptor unique?
                if (programClass.findMethod(name, newDescriptor) == null)
                {
                    if (DEBUG)
                    {
                        System.out.println("DuplicateInitializerFixer:");
                        System.out.println("  ["+programClass.getName()+"."+name+descriptor+"] ("+ClassUtil.externalClassAccessFlags(programMethod.getAccessFlags())+") -> ["+newDescriptor+"]");
                    }

                    // Update the descriptor.
                    programMethod.u2descriptorIndex =
                        new ConstantPoolEditor(programClass).addUtf8Constant(newDescriptor);

                    // Fix the local variable frame size, the method
                    // signature, and the parameter annotations, if
                    // necessary.
                    programMethod.attributesAccept(programClass,
                                                   this);

                    // Visit the initializer, if required.
                    if (extraFixedInitializerVisitor != null)
                    {
                        extraFixedInitializerVisitor.visitProgramMethod(programClass, programMethod);
                    }

                    // We're done with this constructor.
                    return;
                }
            }

            throw new IllegalStateException("Can't find unique constructor descriptor for ["+
                                            programClass.getName()+"."+
                                            programMethod.getName(programClass)+
                                            programMethod.getDescriptor(programClass)+"]");
        }
    }
}
 
Example 28
public void visitProgramMethod(ProgramClass programClass, ProgramMethod programMethod)
{
    // Update the descriptor if it has any unused parameters.
    String descriptor    = programMethod.getDescriptor(programClass);
    String newDescriptor = shrinkDescriptor(programMethod, descriptor);

    if (!descriptor.equals(newDescriptor))
    {
        // Shrink the signature and parameter annotations.
        programMethod.attributesAccept(programClass, this);

        String name    = programMethod.getName(programClass);
        String newName = name;

        // Append a code, if the method isn't a class instance initializer.
        if (!name.equals(ClassConstants.INTERNAL_METHOD_NAME_INIT))
        {
            newName += ClassConstants.SPECIAL_MEMBER_SEPARATOR + Long.toHexString(Math.abs((descriptor).hashCode()));
        }

        if (DEBUG)
        {
            System.out.println("MethodDescriptorShrinker:");
            System.out.println("  Class file        = "+programClass.getName());
            System.out.println("  Method name       = "+name);
            System.out.println("                   -> "+newName);
            System.out.println("  Method descriptor = "+descriptor);
            System.out.println("                   -> "+newDescriptor);
        }

        ConstantPoolEditor constantPoolEditor =
            new ConstantPoolEditor(programClass);

        // Update the name, if necessary.
        if (!newName.equals(name))
        {
            programMethod.u2nameIndex =
                constantPoolEditor.addUtf8Constant(newName);
        }

        // Update the referenced classes.
        programMethod.referencedClasses =
            shrinkReferencedClasses(programMethod,
                                    descriptor,
                                    programMethod.referencedClasses);

        // Finally, update the descriptor itself.
        programMethod.u2descriptorIndex =
            constantPoolEditor.addUtf8Constant(newDescriptor);

        // Visit the method, if required.
        if (extraMemberVisitor != null)
        {
            extraMemberVisitor.visitProgramMethod(programClass, programMethod);
        }
    }
}
 
Example 29
Source Project: java-n-IDE-for-Android   Source File: SourceFileRenamer.java    License: Apache License 2.0 4 votes vote down vote up
public void visitSourceFileAttribute(Clazz clazz, SourceFileAttribute sourceFileAttribute)
{
    // Fix the source file attribute.
    sourceFileAttribute.u2sourceFileIndex =
        new ConstantPoolEditor((ProgramClass)clazz).addUtf8Constant(newSourceFileAttribute);
}
 
Example 30
Source Project: java-n-IDE-for-Android   Source File: SourceFileRenamer.java    License: Apache License 2.0 4 votes vote down vote up
public void visitSourceDirAttribute(Clazz clazz, SourceDirAttribute sourceDirAttribute)
{
    // Fix the source file attribute.
    sourceDirAttribute.u2sourceDirIndex =
        new ConstantPoolEditor((ProgramClass)clazz).addUtf8Constant(newSourceFileAttribute);
}