proguard.classfile.constant.Constant Java Examples

The following examples show how to use proguard.classfile.constant.Constant. 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: ConfigurationLoggingInstructionSequencesReplacer.java    From proguard with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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 #2
Source File: ProgramClassReader.java    From java-n-IDE-for-Android with 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 #3
Source File: ClassShrinker.java    From java-n-IDE-for-Android with 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 #4
Source File: MemberReferenceFixer.java    From java-n-IDE-for-Android with Apache License 2.0 6 votes vote down vote up
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 #5
Source File: InstructionSequenceObfuscator.java    From proguard with GNU General Public License v2.0 6 votes vote down vote up
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 #6
Source File: ConstantPoolEditor.java    From java-n-IDE-for-Android with 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 #7
Source File: InstructionSequencesReplacer.java    From proguard with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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 #8
Source File: LibraryClassReader.java    From java-n-IDE-for-Android with 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 #9
Source File: InstructionSequencesReplacer.java    From java-n-IDE-for-Android with Apache License 2.0 6 votes vote down vote up
/**
 * 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 #10
Source File: ConstantPoolEditor.java    From java-n-IDE-for-Android with 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 #11
Source File: ConfigurationLoggingInstructionSequenceReplacer.java    From proguard with GNU General Public License v2.0 6 votes vote down vote up
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 #12
Source File: ConstantPoolEditor.java    From java-n-IDE-for-Android with 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 #13
Source File: ConfigurationLoggingInstructionSequenceReplacer.java    From proguard with GNU General Public License v2.0 6 votes vote down vote up
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 #14
Source File: ConstantPoolEditor.java    From java-n-IDE-for-Android with 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 #15
Source File: ConstantPoolEditor.java    From java-n-IDE-for-Android with 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 #16
Source File: InstructionSequenceReplacer.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new InstructionSequenceReplacer.
 * @param patternConstants        any constants referenced by the pattern
 *                                instruction.
 * @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.
 */
public InstructionSequenceReplacer(Constant[]          patternConstants,
                                   Instruction[]       patternInstructions,
                                   Instruction[]       replacementInstructions,
                                   BranchTargetFinder  branchTargetFinder,
                                   CodeAttributeEditor codeAttributeEditor,
                                   InstructionVisitor extraInstructionVisitor)
{
    this.instructionSequenceMatcher = new InstructionSequenceMatcher(patternConstants, patternInstructions);
    this.replacementInstructions    = replacementInstructions;
    this.branchTargetFinder         = branchTargetFinder;
    this.codeAttributeEditor        = codeAttributeEditor;
    this.extraInstructionVisitor    = extraInstructionVisitor;
}
 
Example #17
Source File: InstructionSequenceMatcher.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
/**
 * 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 #18
Source File: GsonDeserializationInvocationFinder.java    From proguard with GNU General Public License v2.0 5 votes vote down vote up
private FromJsonInvocationMatcher(Constant[]    patternConstants,
                                  Instruction[] patternInstructions,
                                  int           classStackElementIndex,
                                  int           typeStackElementIndex)
{
    super(patternConstants, patternInstructions);
    this.classStackElementIndex = classStackElementIndex;
    this.typeStackElementIndex  = typeStackElementIndex;
}
 
Example #19
Source File: ProgramClassWriter.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
public void visitAnyConstant(Clazz clazz, Constant constant)
{
    // Write the tag.
    dataOutput.writeByte(constant.getTag());

    // Write the actual body.
    constant.accept(clazz, constantBodyWriter);
}
 
Example #20
Source File: Utf8Shrinker.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Removes all UTF-8 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)
{
    // Create a new index map, if necessary.
    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 = constant.getTag() != ClassConstants.CONSTANT_Utf8 ||
                     Utf8UsageMarker.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 #21
Source File: ConfigurationLoggingInstructionSequenceReplacer.java    From proguard with GNU General Public License v2.0 5 votes vote down vote up
public ConfigurationLoggingInstructionSequenceReplacer(Constant[]          patternConstants,
                                                       Instruction[]       patternInstructions,
                                                       Constant[]          replacementConstants,
                                                       Instruction[]       replacementInstructions,
                                                       BranchTargetFinder  branchTargetFinder,
                                                       CodeAttributeEditor codeAttributeEditor     )
{
    super(patternConstants,
          patternInstructions,
          replacementConstants,
          replacementInstructions,
          branchTargetFinder,
          codeAttributeEditor     );
}
 
Example #22
Source File: ConstantTagFilter.java    From java-n-IDE-for-Android with 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
Source File: ConstantPoolEditor.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Adds a given constant pool entry to the end of the constant pool/
 * @return the constant pool index for the added entry.
 */
public int addConstant(Constant constant)
{
    int        constantPoolCount = targetClass.u2constantPoolCount;
    Constant[] constantPool      = targetClass.constantPool;

    // Make sure there is enough space for another constant pool entry.
    if (constantPool.length < constantPoolCount+2)
    {
        targetClass.constantPool = new Constant[constantPoolCount+2];
        System.arraycopy(constantPool, 0,
                         targetClass.constantPool, 0,
                         constantPoolCount);
        constantPool = targetClass.constantPool;
    }

    if (DEBUG)
    {
        System.out.println(targetClass.getName()+": adding ["+constant.getClass().getName()+"] at index "+targetClass.u2constantPoolCount);
    }

    // Create a new Utf8Constant for the given string.
    constantPool[targetClass.u2constantPoolCount++] = constant;

    // Long constants and double constants take up two entries in the
    // constant pool.
    int tag = constant.getTag();
    if (tag == ClassConstants.CONSTANT_Long ||
        tag == ClassConstants.CONSTANT_Double)
    {
        constantPool[targetClass.u2constantPoolCount++] = null;
    }

    return constantPoolCount;
}
 
Example #24
Source File: ConstantPoolEditor.java    From java-n-IDE-for-Android with 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 #25
Source File: ConstantPoolEditor.java    From java-n-IDE-for-Android with 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)));
}
 
Example #26
Source File: ConstantPoolEditor.java    From java-n-IDE-for-Android with 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 #27
Source File: ConstantAdder.java    From java-n-IDE-for-Android with 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 #28
Source File: ConstantPoolEditor.java    From java-n-IDE-for-Android with 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 #29
Source File: ConstantPoolEditor.java    From java-n-IDE-for-Android with 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 #30
Source File: ConstantPoolEditor.java    From java-n-IDE-for-Android with 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));
}