Java Code Examples for proguard.classfile.constant.Constant

The following examples show how to use proguard.classfile.constant.Constant. 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
public ConfigurationLoggingInstructionSequenceReplacer(InstructionSequenceMatcher instructionSequenceMatcher,
                                                       Constant[]                 patternConstants,
                                                       Instruction[]              patternInstructions,
                                                       Constant[]                 replacementConstants,
                                                       Instruction[]              replacementInstructions,
                                                       BranchTargetFinder         branchTargetFinder,
                                                       CodeAttributeEditor        codeAttributeEditor,
                                                       InstructionVisitor         extraInstructionVisitor    )
{
    super(instructionSequenceMatcher,
          patternConstants,
          patternInstructions,
          replacementConstants,
          replacementInstructions,
          branchTargetFinder,
          codeAttributeEditor,
          extraInstructionVisitor    );
}
 
Example 2
public ConfigurationLoggingInstructionSequenceReplacer(Constant[]          patternConstants,
                                                       Instruction[]       patternInstructions,
                                                       Constant[]          replacementConstants,
                                                       Instruction[]       replacementInstructions,
                                                       BranchTargetFinder  branchTargetFinder,
                                                       CodeAttributeEditor codeAttributeEditor,
                                                       InstructionVisitor  extraInstructionVisitor )
{
    super(patternConstants,
          patternInstructions,
          replacementConstants,
          replacementInstructions,
          branchTargetFinder,
          codeAttributeEditor,
          extraInstructionVisitor );
}
 
Example 3
/**
 * Creates an array of InstructionSequenceReplacer instances.
 *
 * @param constants               any constants referenced by the pattern
 *                                instructions and replacement instructions.
 * @param instructionSequences    the instruction sequences to be replaced,
 *                                with subsequently the sequence pair index,
 *                                the from/to index (0 or 1), and the
 *                                instruction index in the sequence.
 * @param branchTargetFinder      a branch target finder that has been
 *                                initialized to indicate branch targets
 *                                in the visited code.
 * @param codeAttributeEditor     a code editor that can be used for
 *                                accumulating changes to the code.
 * @param extraInstructionVisitor an optional extra visitor for all deleted
 *                                load instructions.
 */
private static InstructionVisitor[] createInstructionSequenceReplacers(Constant[]          constants,
                                                                       Instruction[][][]   instructionSequences,
                                                                       BranchTargetFinder  branchTargetFinder,
                                                                       CodeAttributeEditor codeAttributeEditor,
                                                                       InstructionVisitor  extraInstructionVisitor)
{
    InstructionVisitor[] instructionSequenceReplacers =
        new InstructionSequenceReplacer[instructionSequences.length];

    for (int index = 0; index < instructionSequenceReplacers.length; index++)
    {
        Instruction[][] instructionSequencePair = instructionSequences[index];
        instructionSequenceReplacers[index] =
            new ConfigurationLoggingInstructionSequenceReplacer(constants,
                                                                instructionSequencePair[PATTERN_INDEX],
                                                                constants,
                                                                instructionSequencePair[REPLACEMENT_INDEX],
                                                                branchTargetFinder,
                                                                codeAttributeEditor,
                                                                extraInstructionVisitor);
    }

    return instructionSequenceReplacers;
}
 
Example 4
private static InstructionVisitor[] createInstructionSequenceReplacers(Constant[]          constants,
                                                                       Instruction[][][]   insSequences,
                                                                       BranchTargetFinder  branchTargetFinder,
                                                                       CodeAttributeEditor codeAttributeEditor)
{
    InstructionVisitor[] isReplacers = new InstructionSequenceReplacer[insSequences.length];

    Arrays.setAll(
        isReplacers,
        index -> new InstructionSequenceReplacer(constants,
                                                 insSequences[index][0],
                                                 constants,
                                                 insSequences[index][1],
                                                 branchTargetFinder,
                                                 codeAttributeEditor,
                                                 null)
    );

    return isReplacers;
}
 
Example 5
Source Project: java-n-IDE-for-Android   Source File: ClassShrinker.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Removes all indices that point to unused constant pool entries
 * from the given array.
 * @return the new number of indices.
 */
private int shrinkConstantIndexArray(Constant[] constantPool, int[] array, int length)
{
    int counter = 0;

    // Shift the used objects together.
    for (int index = 0; index < length; index++)
    {
        if (usageMarker.isUsed(constantPool[array[index]]))
        {
            array[counter++] = array[index];
        }
    }

    // Clear the remaining array elements.
    for (int index = counter; index < length; index++)
    {
        array[index] = 0;
    }

    return counter;
}
 
Example 6
Source Project: java-n-IDE-for-Android   Source File: ConstantPoolEditor.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Finds or creates a IntegerConstant constant pool entry with the given
 * value.
 * @return the constant pool index of the Utf8Constant.
 */
public int addIntegerConstant(int value)
{
    int        constantPoolCount = targetClass.u2constantPoolCount;
    Constant[] constantPool      = targetClass.constantPool;

    // Check if the entry already exists.
    for (int index = 1; index < constantPoolCount; index++)
    {
        Constant constant = constantPool[index];

        if (constant != null &&
            constant.getTag() == ClassConstants.CONSTANT_Integer)
        {
            IntegerConstant integerConstant = (IntegerConstant)constant;
            if (integerConstant.getValue() == value)
            {
                return index;
            }
        }
    }

    return addConstant(new IntegerConstant(value));
}
 
Example 7
Source Project: java-n-IDE-for-Android   Source File: ConstantPoolEditor.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Finds or creates a LongConstant constant pool entry with the given value.
 * @return the constant pool index of the LongConstant.
 */
public int addLongConstant(long value)
{
    int        constantPoolCount = targetClass.u2constantPoolCount;
    Constant[] constantPool      = targetClass.constantPool;

    // Check if the entry already exists.
    for (int index = 1; index < constantPoolCount; index++)
    {
        Constant constant = constantPool[index];

        if (constant != null &&
            constant.getTag() == ClassConstants.CONSTANT_Long)
        {
            LongConstant longConstant = (LongConstant)constant;
            if (longConstant.getValue() == value)
            {
                return index;
            }
        }
    }

    return addConstant(new LongConstant(value));
}
 
Example 8
Source Project: java-n-IDE-for-Android   Source File: ConstantPoolEditor.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Finds or creates a FloatConstant constant pool entry with the given
 * value.
 * @return the constant pool index of the FloatConstant.
 */
public int addFloatConstant(float value)
{
    int        constantPoolCount = targetClass.u2constantPoolCount;
    Constant[] constantPool      = targetClass.constantPool;

    // Check if the entry already exists.
    for (int index = 1; index < constantPoolCount; index++)
    {
        Constant constant = constantPool[index];

        if (constant != null &&
            constant.getTag() == ClassConstants.CONSTANT_Float)
        {
            FloatConstant floatConstant = (FloatConstant)constant;
            if (floatConstant.getValue() == value)
            {
                return index;
            }
        }
    }

    return addConstant(new FloatConstant(value));
}
 
Example 9
Source Project: java-n-IDE-for-Android   Source File: ConstantPoolEditor.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Finds or creates a DoubleConstant constant pool entry with the given
 * value.
 * @return the constant pool index of the DoubleConstant.
 */
public int addDoubleConstant(double value)
{
    int        constantPoolCount = targetClass.u2constantPoolCount;
    Constant[] constantPool      = targetClass.constantPool;

    // Check if the entry already exists.
    for (int index = 1; index < constantPoolCount; index++)
    {
        Constant constant = constantPool[index];

        if (constant != null &&
            constant.getTag() == ClassConstants.CONSTANT_Double)
        {
            DoubleConstant doubleConstant = (DoubleConstant)constant;
            if (doubleConstant.getValue() == value)
            {
                return index;
            }
        }
    }

    return addConstant(new DoubleConstant(value));
}
 
Example 10
Source Project: java-n-IDE-for-Android   Source File: ConstantPoolEditor.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Finds or creates a Utf8Constant constant pool entry for the given string.
 * @return the constant pool index of the Utf8Constant.
 */
public int addUtf8Constant(String string)
{
    int        constantPoolCount = targetClass.u2constantPoolCount;
    Constant[] constantPool      = targetClass.constantPool;

    // Check if the entry already exists.
    for (int index = 1; index < constantPoolCount; index++)
    {
        Constant constant = constantPool[index];

        if (constant != null &&
            constant.getTag() == ClassConstants.CONSTANT_Utf8)
        {
            Utf8Constant utf8Constant = (Utf8Constant)constant;
            if (utf8Constant.getString().equals(string))
            {
                return index;
            }
        }
    }

    return addConstant(new Utf8Constant(string));
}
 
Example 11
public void visitProgramClass(ProgramClass programClass)
{
    stackSizesMayHaveChanged = false;

    // Fix the constant pool entries.
    for (int index = 1; index < programClass.u2constantPoolCount; index++)
    {
        Constant constant = programClass.constantPool[index];
        if (constant != null)
        {
            // Fix the entry, replacing it entirely if needed.
            this.constantIndex = index;

            constant.accept(programClass, this);
        }
    }

    // Fix the class members.
    programClass.fieldsAccept(this);
    programClass.methodsAccept(this);

    // Fix the attributes.
    programClass.attributesAccept(this);
}
 
Example 12
Source Project: java-n-IDE-for-Android   Source File: LibraryClassReader.java    License: Apache License 2.0 6 votes vote down vote up
private Constant createConstant()
{
    int u1tag = dataInput.readUnsignedByte();

    switch (u1tag)
    {
        case ClassConstants.CONSTANT_Utf8:               return new Utf8Constant();
        case ClassConstants.CONSTANT_Integer:            return new IntegerConstant();
        case ClassConstants.CONSTANT_Float:              return new FloatConstant();
        case ClassConstants.CONSTANT_Long:               return new LongConstant();
        case ClassConstants.CONSTANT_Double:             return new DoubleConstant();
        case ClassConstants.CONSTANT_String:             return new StringConstant();
        case ClassConstants.CONSTANT_Fieldref:           return new FieldrefConstant();
        case ClassConstants.CONSTANT_Methodref:          return new MethodrefConstant();
        case ClassConstants.CONSTANT_InterfaceMethodref: return new InterfaceMethodrefConstant();
        case ClassConstants.CONSTANT_Class:              return new ClassConstant();
        case ClassConstants.CONSTANT_NameAndType:        return new NameAndTypeConstant();

        default: throw new RuntimeException("Unknown constant type ["+u1tag+"] in constant pool");
    }
}
 
Example 13
Source Project: java-n-IDE-for-Android   Source File: ProgramClassReader.java    License: Apache License 2.0 6 votes vote down vote up
private Constant createConstant()
{
    int u1tag = dataInput.readUnsignedByte();

    switch (u1tag)
    {
        case ClassConstants.CONSTANT_Utf8:               return new Utf8Constant();
        case ClassConstants.CONSTANT_Integer:            return new IntegerConstant();
        case ClassConstants.CONSTANT_Float:              return new FloatConstant();
        case ClassConstants.CONSTANT_Long:               return new LongConstant();
        case ClassConstants.CONSTANT_Double:             return new DoubleConstant();
        case ClassConstants.CONSTANT_String:             return new StringConstant();
        case ClassConstants.CONSTANT_Fieldref:           return new FieldrefConstant();
        case ClassConstants.CONSTANT_Methodref:          return new MethodrefConstant();
        case ClassConstants.CONSTANT_InterfaceMethodref: return new InterfaceMethodrefConstant();
        case ClassConstants.CONSTANT_Class:              return new ClassConstant();
        case ClassConstants.CONSTANT_NameAndType:        return new NameAndTypeConstant();

        default: throw new RuntimeException("Unknown constant type ["+u1tag+"] in constant pool");
    }
}
 
Example 14
/**
 * Creates an array of InstructionSequenceReplacer instances.
 * @param patternConstants        any constants referenced by the pattern
 *                                instruction.
 * @param instructionSequences    the instruction sequences to be replaced,
 *                                with subsequently the sequence pair index,
 *                                the from/to index (0 or 1), and the
 *                                instruction index in the sequence.
 * @param branchTargetFinder      a branch target finder that has been
 *                                initialized to indicate branch targets
 *                                in the visited code.
 * @param codeAttributeEditor     a code editor that can be used for
 *                                accumulating changes to the code.
 * @param extraInstructionVisitor an optional extra visitor for all deleted
 *                                load instructions.
 */
private static InstructionVisitor[] createInstructionSequenceReplacers(Constant[]          patternConstants,
                                                                       Instruction[][][]   instructionSequences,
                                                                       BranchTargetFinder  branchTargetFinder,
                                                                       CodeAttributeEditor codeAttributeEditor,
                                                                       InstructionVisitor  extraInstructionVisitor)
{
    InstructionVisitor[] instructionSequenceReplacers =
        new InstructionSequenceReplacer[instructionSequences.length];

    for (int index = 0; index < instructionSequenceReplacers.length; index++)
    {
        Instruction[][] instructionSequencePair = instructionSequences[index];
        instructionSequenceReplacers[index] =
            new InstructionSequenceReplacer(patternConstants,
                                            instructionSequencePair[PATTERN_INDEX],
                                            instructionSequencePair[REPLACEMENT_INDEX],
                                            branchTargetFinder,
                                            codeAttributeEditor,
                                            extraInstructionVisitor);
    }

    return instructionSequenceReplacers;
}
 
Example 15
/**
 * Creates an array of InstructionSequenceReplacer instances.
 * @param patternConstants        any constants referenced by the pattern
 *                                instruction.
 * @param instructionSequences    the instruction sequences to be replaced,
 *                                with subsequently the sequence pair index,
 *                                the from/to index (0 or 1), and the
 *                                instruction index in the sequence.
 * @param branchTargetFinder      a branch target finder that has been
 *                                initialized to indicate branch targets
 *                                in the visited code.
 * @param codeAttributeEditor     a code editor that can be used for
 *                                accumulating changes to the code.
 * @param extraInstructionVisitor an optional extra visitor for all deleted
 *                                load instructions.
 */
private static InstructionVisitor[] createInstructionSequenceReplacers(Constant[]          patternConstants,
                                                                       Instruction[][][]   instructionSequences,
                                                                       BranchTargetFinder  branchTargetFinder,
                                                                       CodeAttributeEditor codeAttributeEditor,
                                                                       InstructionVisitor extraInstructionVisitor)
{
    InstructionVisitor[] instructionSequenceReplacers =
        new InstructionSequenceReplacer[instructionSequences.length];

    for (int index = 0; index < instructionSequenceReplacers.length; index++)
    {
        Instruction[][] instructionSequencePair = instructionSequences[index];
        instructionSequenceReplacers[index] =
            new InstructionSequenceReplacer(patternConstants,
                                            instructionSequencePair[PATTERN_INDEX],
                                            instructionSequencePair[REPLACEMENT_INDEX],
                                            branchTargetFinder,
                                            codeAttributeEditor,
                                            extraInstructionVisitor);
    }

    return instructionSequenceReplacers;
}
 
Example 16
private ToJsonInvocationMatcher(Constant[]   patternConstants,
                               Instruction[] patternInstructions,
                               int           objectStackElementIndex,
                               int           typeStackElementIndex)
{
    super(patternConstants, patternInstructions);
    this.objectStackElementIndex = objectStackElementIndex;
    this.typeStackElementIndex   = typeStackElementIndex;
}
 
Example 17
private FromJsonInvocationMatcher(Constant[]    patternConstants,
                                  Instruction[] patternInstructions,
                                  int           classStackElementIndex,
                                  int           typeStackElementIndex)
{
    super(patternConstants, patternInstructions);
    this.classStackElementIndex = classStackElementIndex;
    this.typeStackElementIndex  = typeStackElementIndex;
}
 
Example 18
public ConfigurationLoggingInstructionSequenceReplacer(Constant[]          patternConstants,
                                                       Instruction[]       patternInstructions,
                                                       Constant[]          replacementConstants,
                                                       Instruction[]       replacementInstructions,
                                                       BranchTargetFinder  branchTargetFinder,
                                                       CodeAttributeEditor codeAttributeEditor     )
{
    super(patternConstants,
          patternInstructions,
          replacementConstants,
          replacementInstructions,
          branchTargetFinder,
          codeAttributeEditor     );
}
 
Example 19
Source Project: bazel   Source File: ConstantTagFilter.java    License: Apache License 2.0 5 votes vote down vote up
public void visitAnyConstant(Clazz clazz, Constant constant)
{
    if (((1 << constant.getTag()) & constantTagMask) != 0)
    {
        constant.accept(clazz, constantVisitor);
    }
}
 
Example 20
MyInstructionSequenceReplacer(Constant[]          constants,
                              Instruction[][][]   insSequences,
                              BranchTargetFinder  branchTargetFinder,
                              CodeAttributeEditor codeAttributeEditor)
{
    super(createInstructionSequenceReplacers(constants, insSequences, branchTargetFinder, codeAttributeEditor));
}
 
Example 21
Source Project: java-n-IDE-for-Android   Source File: ClassShrinker.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Removes all entries that are not marked as being used from the given
 * constant pool.
 * @return the new number of entries.
 */
private int shrinkConstantPool(Constant[] constantPool, int length)
{
    if (constantIndexMap.length < length)
    {
        constantIndexMap = new int[length];
    }

    int     counter = 1;
    boolean isUsed  = false;

    // Shift the used constant pool entries together.
    for (int index = 1; index < length; index++)
    {
        constantIndexMap[index] = counter;

        Constant constant = constantPool[index];

        // Don't update the flag if this is the second half of a long entry.
        if (constant != null)
        {
            isUsed = usageMarker.isUsed(constant);
        }

        if (isUsed)
        {
            constantPool[counter++] = constant;
        }
    }

    // Clear the remaining constant pool elements.
    for (int index = counter; index < length; index++)
    {
        constantPool[index] = null;
    }

    return counter;
}
 
Example 22
Source Project: java-n-IDE-for-Android   Source File: ConstantTagFilter.java    License: Apache License 2.0 5 votes vote down vote up
public void visitAnyConstant(Clazz clazz, Constant constant)
{
    if (constant.getTag() == constantTag)
    {
        constant.accept(clazz, constantVisitor);
    }
}
 
Example 23
/**
 * Creates a new InstructionSequenceMatcher.
 * @param patternConstants        any constants referenced by the pattern
 *                                instruction.
 * @param patternInstructions     the pattern instruction sequence.
 */
public InstructionSequenceMatcher(Constant[]    patternConstants,
                                  Instruction[] patternInstructions)
{
    this.patternConstants    = patternConstants;
    this.patternInstructions = patternInstructions;

    matchedInstructionOffsets = new int[patternInstructions.length];
    matchedConstantIndices    = new int[patternConstants.length];
}
 
Example 24
Source Project: java-n-IDE-for-Android   Source File: ConstantAdder.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Adds a copy of the given constant in the given class and returns
 * its index.
 */
public int addConstant(Clazz clazz, Constant constant)
{
    constant.accept(clazz, this);

    return this.constantIndex;
}
 
Example 25
Source Project: java-n-IDE-for-Android   Source File: ConstantPoolEditor.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Finds or creates a StringConstant constant pool entry with the given
 * value.
 * @return the constant pool index of the StringConstant.
 */
public int addStringConstant(String string,
                             Clazz  referencedClass,
                             Member referencedMember)
{
    int        constantPoolCount = targetClass.u2constantPoolCount;
    Constant[] constantPool      = targetClass.constantPool;

    // Check if the entry already exists.
    for (int index = 1; index < constantPoolCount; index++)
    {
        Constant constant = constantPool[index];

        if (constant != null &&
            constant.getTag() == ClassConstants.CONSTANT_String)
        {
            StringConstant stringConstant = (StringConstant)constant;
            if (stringConstant.getString(targetClass).equals(string))
            {
                return index;
            }
        }
    }

    return addConstant(new StringConstant(addUtf8Constant(string),
                                          referencedClass,
                                          referencedMember));
}
 
Example 26
Source Project: java-n-IDE-for-Android   Source File: ConstantPoolEditor.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Finds or creates a FieldrefConstant constant pool entry with the given
 * class constant pool entry index and name and type constant pool entry
 * index.
 * @return the constant pool index of the FieldrefConstant.
 */
public int addFieldrefConstant(int    classIndex,
                               int    nameAndTypeIndex,
                               Clazz  referencedClass,
                               Member referencedMember)
{
    int        constantPoolCount = targetClass.u2constantPoolCount;
    Constant[] constantPool      = targetClass.constantPool;

    // Check if the entry already exists.
    for (int index = 1; index < constantPoolCount; index++)
    {
        Constant constant = constantPool[index];

        if (constant != null &&
            constant.getTag() == ClassConstants.CONSTANT_Fieldref)
        {
            FieldrefConstant fieldrefConstant = (FieldrefConstant)constant;
            if (fieldrefConstant.u2classIndex         == classIndex &&
                fieldrefConstant.u2nameAndTypeIndex   == nameAndTypeIndex)
            {
                return index;
            }
        }
    }

    return addConstant(new FieldrefConstant(classIndex,
                                            nameAndTypeIndex,
                                            referencedClass,
                                            referencedMember));
}
 
Example 27
Source Project: java-n-IDE-for-Android   Source File: ConstantPoolEditor.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Finds or creates a InterfaceMethodrefConstant constant pool entry with the
 * given class constant pool entry index and name and type constant pool
 * entry index.
 * @return the constant pool index of the InterfaceMethodrefConstant.
 */
public int addInterfaceMethodrefConstant(int    classIndex,
                                         int    nameAndTypeIndex,
                                         Clazz  referencedClass,
                                         Member referencedMember)
{
    int        constantPoolCount = targetClass.u2constantPoolCount;
    Constant[] constantPool      = targetClass.constantPool;

    // Check if the entry already exists.
    for (int index = 1; index < constantPoolCount; index++)
    {
        Constant constant = constantPool[index];

        if (constant != null &&
                        constant.getTag() == ClassConstants.CONSTANT_InterfaceMethodref)
        {
            InterfaceMethodrefConstant methodrefConstant = (InterfaceMethodrefConstant)constant;
            if (methodrefConstant.u2classIndex       == classIndex &&
                methodrefConstant.u2nameAndTypeIndex == nameAndTypeIndex)
            {
                return index;
            }
        }
    }

    return addConstant(new InterfaceMethodrefConstant(classIndex,
                                                      nameAndTypeIndex,
                                                      referencedClass,
                                                      referencedMember));
}
 
Example 28
Source Project: java-n-IDE-for-Android   Source File: ConstantPoolEditor.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Finds or creates a MethodrefConstant constant pool entry with the given
 * class constant pool entry index and name and type constant pool entry
 * index.
 * @return the constant pool index of the MethodrefConstant.
 */
public int addMethodrefConstant(int    classIndex,
                                int    nameAndTypeIndex,
                                Clazz  referencedClass,
                                Member referencedMember)
{
    int        constantPoolCount = targetClass.u2constantPoolCount;
    Constant[] constantPool      = targetClass.constantPool;

    // Check if the entry already exists.
    for (int index = 1; index < constantPoolCount; index++)
    {
        Constant constant = constantPool[index];

        if (constant != null &&
            constant.getTag() == ClassConstants.CONSTANT_Methodref)
        {
            MethodrefConstant methodrefConstant = (MethodrefConstant)constant;
            if (methodrefConstant.u2classIndex       == classIndex &&
                methodrefConstant.u2nameAndTypeIndex == nameAndTypeIndex)
            {
                return index;
            }
        }
    }

    return addConstant(new MethodrefConstant(classIndex,
                                             nameAndTypeIndex,
                                             referencedClass,
                                             referencedMember));
}
 
Example 29
Source Project: java-n-IDE-for-Android   Source File: ConstantPoolEditor.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Finds or creates a ClassConstant constant pool entry with the given name.
 * @return the constant pool index of the ClassConstant.
 */
public int addClassConstant(String name,
                            Clazz  referencedClass)
{
    int        constantPoolCount = targetClass.u2constantPoolCount;
    Constant[] constantPool      = targetClass.constantPool;

    // Check if the entry already exists.
    for (int index = 1; index < constantPoolCount; index++)
    {
        Constant constant = constantPool[index];

        if (constant != null &&
            constant.getTag() == ClassConstants.CONSTANT_Class)
        {
            ClassConstant classConstant = (ClassConstant)constant;
            if (classConstant.getName(targetClass).equals(name))
            {
                return index;
            }
        }
    }

    int nameIndex = addUtf8Constant(name);

    return addConstant(new ClassConstant(nameIndex, referencedClass));
}
 
Example 30
Source Project: java-n-IDE-for-Android   Source File: ConstantPoolEditor.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Finds or creates a NameAndTypeConstant constant pool entry with the given
 * name and type.
 * @return the constant pool index of the NameAndTypeConstant.
 */
public int addNameAndTypeConstant(String name,
                                  String type)
{
    int        constantPoolCount = targetClass.u2constantPoolCount;
    Constant[] constantPool      = targetClass.constantPool;

    // Check if the entry already exists.
    for (int index = 1; index < constantPoolCount; index++)
    {
        Constant constant = constantPool[index];

        if (constant != null &&
            constant.getTag() == ClassConstants.CONSTANT_NameAndType)
        {
            NameAndTypeConstant nameAndTypeConstant = (NameAndTypeConstant)constant;
            if (nameAndTypeConstant.getName(targetClass).equals(name) &&
                nameAndTypeConstant.getType(targetClass).equals(type))
            {
                return index;
            }
        }
    }

    return addConstant(new NameAndTypeConstant(addUtf8Constant(name),
                                               addUtf8Constant(type)));
}