Java Code Examples for proguard.classfile.constant.Constant#getTag()

The following examples show how to use proguard.classfile.constant.Constant#getTag() . 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: 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 2
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 3
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 4
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 5
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 6
Source File: ConstantTagFilter.java    From bazel with 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 7
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 8
Source File: ConstantTagFilter.java    From proguard with GNU General Public License v2.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 9
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 10
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 11
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 12
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 13
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 14
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));
}
 
Example 15
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 16
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 17
Source File: ProgramClassReader.java    From java-n-IDE-for-Android with Apache License 2.0 4 votes vote down vote up
public void visitProgramClass(ProgramClass programClass)
{
    // Read and check the magic number.
    programClass.u4magic = dataInput.readInt();

    ClassUtil.checkMagicNumber(programClass.u4magic);

    // Read and check the version numbers.
    int u2minorVersion = dataInput.readUnsignedShort();
    int u2majorVersion = dataInput.readUnsignedShort();

    programClass.u4version = ClassUtil.internalClassVersion(u2majorVersion,
                                                            u2minorVersion);

    ClassUtil.checkVersionNumbers(programClass.u4version);

    // Read the constant pool. Note that the first entry is not used.
    programClass.u2constantPoolCount = dataInput.readUnsignedShort();

    programClass.constantPool = new Constant[programClass.u2constantPoolCount];
    for (int index = 1; index < programClass.u2constantPoolCount; index++)
    {
        Constant constant = createConstant();
        constant.accept(programClass, this);
        programClass.constantPool[index] = 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)
        {
            programClass.constantPool[++index] = null;
        }
    }

    // Read the general class information.
    programClass.u2accessFlags = dataInput.readUnsignedShort();
    programClass.u2thisClass   = dataInput.readUnsignedShort();
    programClass.u2superClass  = dataInput.readUnsignedShort();

    // Read the interfaces.
    programClass.u2interfacesCount = dataInput.readUnsignedShort();

    programClass.u2interfaces = new int[programClass.u2interfacesCount];
    for (int index = 0; index < programClass.u2interfacesCount; index++)
    {
        programClass.u2interfaces[index] = dataInput.readUnsignedShort();
    }

    // Read the fields.
    programClass.u2fieldsCount = dataInput.readUnsignedShort();

    programClass.fields = new ProgramField[programClass.u2fieldsCount];
    for (int index = 0; index < programClass.u2fieldsCount; index++)
    {
        ProgramField programField = new ProgramField();
        this.visitProgramField(programClass, programField);
        programClass.fields[index] = programField;
    }

    // Read the methods.
    programClass.u2methodsCount = dataInput.readUnsignedShort();

    programClass.methods = new ProgramMethod[programClass.u2methodsCount];
    for (int index = 0; index < programClass.u2methodsCount; index++)
    {
        ProgramMethod programMethod = new ProgramMethod();
        this.visitProgramMethod(programClass, programMethod);
        programClass.methods[index] = programMethod;
    }

    // Read the class attributes.
    programClass.u2attributesCount = dataInput.readUnsignedShort();

    programClass.attributes = new Attribute[programClass.u2attributesCount];
    for (int index = 0; index < programClass.u2attributesCount; index++)
    {
        Attribute attribute = createAttribute(programClass);
        attribute.accept(programClass, this);
        programClass.attributes[index] = attribute;
    }
}
 
Example 18
Source File: NameAndTypeShrinker.java    From java-n-IDE-for-Android with Apache License 2.0 4 votes vote down vote up
/**
 * Removes all NameAndType 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 == null ||
        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_NameAndType ||
                     NameAndTypeUsageMarker.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;
}