proguard.classfile.attribute.Attribute Java Examples

The following examples show how to use proguard.classfile.attribute.Attribute. 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: AttributesEditor.java    From java-n-IDE-for-Android with Apache License 2.0 6 votes vote down vote up
/**
 * Appends the given attribute to the given array of attributes, creating a
 * new array if necessary.
 */
private Attribute[] addAttribute(int         attributesCount,
                                 Attribute[] attributes,
                                 Attribute   attribute)
{
    // Is the array too small to contain the additional attribute?
    if (attributes.length <= attributesCount)
    {
        // Create a new array and copy the attributes into it.
        Attribute[] newAttributes = new Attribute[attributesCount + 1];
        System.arraycopy(attributes, 0,
                         newAttributes, 0,
                         attributesCount);
        attributes = newAttributes;
    }

    // Append the attribute.
    attributes[attributesCount] = attribute;

    return attributes;
}
 
Example #2
Source File: ProgramClassReader.java    From java-n-IDE-for-Android with Apache License 2.0 6 votes vote down vote up
public void visitProgramMethod(ProgramClass programClass, ProgramMethod programMethod)
{
    // Read the general method information.
    programMethod.u2accessFlags     = dataInput.readUnsignedShort();
    programMethod.u2nameIndex       = dataInput.readUnsignedShort();
    programMethod.u2descriptorIndex = dataInput.readUnsignedShort();

    // Read the method attributes.
    programMethod.u2attributesCount = dataInput.readUnsignedShort();

    programMethod.attributes = new Attribute[programMethod.u2attributesCount];
    for (int index = 0; index < programMethod.u2attributesCount; index++)
    {
        Attribute attribute = createAttribute(programClass);
        attribute.accept(programClass, programMethod, this);
        programMethod.attributes[index] = attribute;
    }
}
 
Example #3
Source File: ProgramClassReader.java    From java-n-IDE-for-Android with Apache License 2.0 6 votes vote down vote up
public void visitProgramField(ProgramClass programClass, ProgramField programField)
{
    // Read the general field information.
    programField.u2accessFlags     = dataInput.readUnsignedShort();
    programField.u2nameIndex       = dataInput.readUnsignedShort();
    programField.u2descriptorIndex = dataInput.readUnsignedShort();

    // Read the field attributes.
    programField.u2attributesCount = dataInput.readUnsignedShort();

    programField.attributes = new Attribute[programField.u2attributesCount];
    for (int index = 0; index < programField.u2attributesCount; index++)
    {
        Attribute attribute = createAttribute(programClass);
        attribute.accept(programClass, programField, this);
        programField.attributes[index] = attribute;
    }
}
 
Example #4
Source File: AttributesEditor.java    From java-n-IDE-for-Android with Apache License 2.0 6 votes vote down vote up
/**
 * Tries put the given attribute in place of an existing attribute of the
 * same name, returning whether it was present.
 */
private boolean replaceAttribute(int         attributesCount,
                                 Attribute[] attributes,
                                 Attribute   attribute)
{
    // Find the attribute with the same name.
    int index = findAttribute(attributesCount,
                              attributes,
                              attribute.getAttributeName(targetClass));
    if (index < 0)
    {
        return false;
    }

    attributes[index] = attribute;

    return true;
}
 
Example #5
Source File: AttributesEditor.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Finds the index of the attribute with the given name in the given
 * array of attributes.
 */
private int findAttribute(int         attributesCount,
                          Attribute[] attributes,
                          String      attributeName)
{
    for (int index = 0; index < attributesCount; index++)
    {
        if (attributes[index].getAttributeName(targetClass).equals(attributeName))
        {
            return index;
        }
    }

    return -1;
}
 
Example #6
Source File: ProgramMethod.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Creates an initialized ProgramMethod.
 */
public ProgramMethod(int         u2accessFlags,
                     int         u2nameIndex,
                     int         u2descriptorIndex,
                     int         u2attributesCount,
                     Attribute[] attributes,
                     Clazz[]     referencedClasses)
{
    super(u2accessFlags, u2nameIndex, u2descriptorIndex, u2attributesCount, attributes);

    this.referencedClasses = referencedClasses;
}
 
Example #7
Source File: ProgramClassReader.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
public void visitCodeAttribute(Clazz clazz, Method method, CodeAttribute codeAttribute)
{
    // Read the stack size and local variable frame size.
    codeAttribute.u2maxStack   = dataInput.readUnsignedShort();
    codeAttribute.u2maxLocals  = dataInput.readUnsignedShort();

    // Read the byte code.
    codeAttribute.u4codeLength = dataInput.readInt();

    byte[] code = new byte[codeAttribute.u4codeLength];
    dataInput.readFully(code);
    codeAttribute.code = code;

    // Read the exceptions.
    codeAttribute.u2exceptionTableLength = dataInput.readUnsignedShort();

    codeAttribute.exceptionTable = new ExceptionInfo[codeAttribute.u2exceptionTableLength];
    for (int index = 0; index < codeAttribute.u2exceptionTableLength; index++)
    {
        ExceptionInfo exceptionInfo = new ExceptionInfo();
        this.visitExceptionInfo(clazz, method, codeAttribute, exceptionInfo);
        codeAttribute.exceptionTable[index] = exceptionInfo;
    }

    // Read the code attributes.
    codeAttribute.u2attributesCount = dataInput.readUnsignedShort();

    codeAttribute.attributes = new Attribute[codeAttribute.u2attributesCount];
    for (int index = 0; index < codeAttribute.u2attributesCount; index++)
    {
        Attribute attribute = createAttribute(clazz);
        attribute.accept(clazz, method, codeAttribute, this);
        codeAttribute.attributes[index] = attribute;
    }
}
 
Example #8
Source File: ProgramMember.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Creates an initialized ProgramMember. 
 */
protected ProgramMember(int         u2accessFlags,
                        int         u2nameIndex,
                        int         u2descriptorIndex,
                        int         u2attributesCount,
                        Attribute[] attributes)
{
    this.u2accessFlags     = u2accessFlags;
    this.u2nameIndex       = u2nameIndex;
    this.u2descriptorIndex = u2descriptorIndex;
    this.u2attributesCount = u2attributesCount;
    this.attributes        = attributes;
}
 
Example #9
Source File: ProgramMember.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the (first) attribute with the given name.
 */
private Attribute getAttribute(Clazz clazz, String name)
{
    for (int index = 0; index < u2attributesCount; index++)
    {
        Attribute attribute = attributes[index];
        if (attribute.getAttributeName(clazz).equals(name))
        {
            return attribute;
        }
    }

    return null;
}
 
Example #10
Source File: ProgramField.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Creates an initialized ProgramField.
 */
public ProgramField(int         u2accessFlags,
                    int         u2nameIndex,
                    int         u2descriptorIndex,
                    int         u2attributesCount,
                    Attribute[] attributes,
                    Clazz referencedClass)
{
    super(u2accessFlags, u2nameIndex, u2descriptorIndex, u2attributesCount, attributes);

    this.referencedClass = referencedClass;
}
 
Example #11
Source File: ProgramClassWriter.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
public void visitAnyAttribute(Clazz clazz, Attribute attribute)
{
    // Write the attribute name index.
    dataOutput.writeShort(attribute.u2attributeNameIndex);

    // We'll write the attribute body into an array first, so we can
    // automatically figure out its length.
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

    // Temporarily replace the current data output.
    RuntimeDataOutput oldDataOutput = dataOutput;
    dataOutput = new RuntimeDataOutput(new DataOutputStream(byteArrayOutputStream));

    // Write the attribute body into the array. Note that the
    // accept method with two dummy null arguments never throws
    // an UnsupportedOperationException.
    attribute.accept(clazz, null, null, attributeBodyWriter);

    // Restore the original data output.
    dataOutput = oldDataOutput;

    // Write the attribute length and body.
    byte[] info = byteArrayOutputStream.toByteArray();

    dataOutput.writeInt(info.length);
    dataOutput.write(info);
}
 
Example #12
Source File: ProgramMember.java    From proguard with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Creates an initialized ProgramMember.
 */
protected ProgramMember(int         u2accessFlags,
                        int         u2nameIndex,
                        int         u2descriptorIndex,
                        int         u2attributesCount,
                        Attribute[] attributes)
{
    this.u2accessFlags     = u2accessFlags;
    this.u2nameIndex       = u2nameIndex;
    this.u2descriptorIndex = u2descriptorIndex;
    this.u2attributesCount = u2attributesCount;
    this.attributes        = attributes;
}
 
Example #13
Source File: ProgramMember.java    From proguard with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns the (first) attribute with the given name.
 */
private Attribute getAttribute(Clazz clazz, String name)
{
    for (int index = 0; index < u2attributesCount; index++)
    {
        Attribute attribute = attributes[index];
        if (attribute.getAttributeName(clazz).equals(name))
        {
            return attribute;
        }
    }

    return null;
}
 
Example #14
Source File: ClassMerger.java    From proguard with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns whether the given class has any attributes that can not be copied when
 * merging it into another class.
 */
private boolean hasNonCopiableAttributes(Clazz clazz)
{
    AttributeCounter counter = new AttributeCounter();

    // Copy over the other attributes.
    clazz.attributesAccept(
        new AttributeNameFilter(
            new OrMatcher(new FixedStringMatcher(Attribute.INNER_CLASSES),
                          new FixedStringMatcher(Attribute.ENCLOSING_METHOD)),
        counter));

    return counter.getCount() > 0;
}
 
Example #15
Source File: ClassMerger.java    From proguard with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns whether the given class has a Signature attributes containing
 * type variables or parameterized types.
 */
private boolean hasSignatureAttribute(Clazz clazz)
{
    AttributeCounter counter = new AttributeCounter();

    clazz.attributesAccept(
        new AttributeNameFilter(
        new FixedStringMatcher(Attribute.SIGNATURE),
            counter));

    return counter.getCount() > 0;
}
 
Example #16
Source File: MarkedAnnotationDeleter.java    From proguard with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void visitRuntimeInvisibleTypeAnnotationsAttribute(Clazz clazz,
                                                          Member member,
                                                          RuntimeInvisibleTypeAnnotationsAttribute runtimeInvisibleTypeAnnotationsAttribute)
{
    cleanAnnotationsAttribute(clazz,
                              member,
                              runtimeInvisibleTypeAnnotationsAttribute,
                              Attribute.RUNTIME_INVISIBLE_TYPE_ANNOTATIONS);
}
 
Example #17
Source File: ProgramClass.java    From proguard with GNU General Public License v2.0 5 votes vote down vote up
public void attributeAccept(String name, AttributeVisitor attributeVisitor)
{
    for (int index = 0; index < u2attributesCount; index++)
    {
        Attribute attribute = attributes[index];
        if (attribute.getAttributeName(this).equals(name))
        {
            attribute.accept(this, attributeVisitor);
        }
    }
}
 
Example #18
Source File: MarkedAnnotationDeleter.java    From proguard with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void visitRuntimeInvisibleParameterAnnotationsAttribute(Clazz clazz,
                                                               Method method,
                                                               RuntimeInvisibleParameterAnnotationsAttribute runtimeInvisibleParameterAnnotationsAttribute)
{
    cleanParameterAnnotationsAttribute(clazz,
                                       method,
                                       runtimeInvisibleParameterAnnotationsAttribute,
                                       Attribute.RUNTIME_INVISIBLE_PARAMETER_ANNOTATIONS);
}
 
Example #19
Source File: MarkedAnnotationDeleter.java    From proguard with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void visitRuntimeVisibleParameterAnnotationsAttribute(Clazz clazz,
                                                             Method method,
                                                             RuntimeVisibleParameterAnnotationsAttribute runtimeVisibleParameterAnnotationsAttribute)
{
    cleanParameterAnnotationsAttribute(clazz,
                                       method,
                                       runtimeVisibleParameterAnnotationsAttribute,
                                       Attribute.RUNTIME_VISIBLE_PARAMETER_ANNOTATIONS);
}
 
Example #20
Source File: MarkedAnnotationDeleter.java    From proguard with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void visitRuntimeVisibleAnnotationsAttribute(Clazz clazz,
                                                    Member member,
                                                    RuntimeVisibleAnnotationsAttribute runtimeVisibleAnnotationsAttribute)
{
    cleanAnnotationsAttribute(clazz,
                              member,
                              runtimeVisibleAnnotationsAttribute,
                              Attribute.RUNTIME_VISIBLE_ANNOTATIONS);
}
 
Example #21
Source File: AttributeUsageMarker.java    From proguard with GNU General Public License v2.0 4 votes vote down vote up
public void visitAnyAttribute(Clazz clazz, Attribute attribute)
{
    markAsUsed(attribute);
}
 
Example #22
Source File: AttributesEditor.java    From java-n-IDE-for-Android with Apache License 2.0 4 votes vote down vote up
/**
 * Adds the given attribute to the target.
 */
public void addAttribute(Attribute attribute)
{
    // What's the target?
    if (targetAttribute != null)
    {
        // Try to replace an existing attribute.
        if (!replaceAttributes ||
            !replaceAttribute(targetAttribute.u2attributesCount,
                              targetAttribute.attributes,
                              attribute))
        {
            // Otherwise append the attribute.
            targetAttribute.attributes =
                addAttribute(targetAttribute.u2attributesCount,
                             targetAttribute.attributes,
                             attribute);

            targetAttribute.u2attributesCount++;
        }
    }
    else if (targetMember != null)
    {
        // Try to replace an existing attribute.
        if (!replaceAttributes ||
            !replaceAttribute(targetMember.u2attributesCount,
                              targetMember.attributes,
                              attribute))
        {
            // Otherwise append the attribute.
            targetMember.attributes =
                addAttribute(targetMember.u2attributesCount,
                             targetMember.attributes,
                             attribute);

            targetMember.u2attributesCount++;
        }
    }
    else
    {
        // Try to replace an existing attribute.
        if (!replaceAttributes ||
            !replaceAttribute(targetClass.u2attributesCount,
                              targetClass.attributes,
                              attribute))
        {
            // Otherwise append the attribute.
            targetClass.attributes =
                addAttribute(targetClass.u2attributesCount,
                             targetClass.attributes,
                             attribute);

            targetClass.u2attributesCount++;
        }
    }
}
 
Example #23
Source File: MemberAdder.java    From proguard with GNU General Public License v2.0 4 votes vote down vote up
public void visitProgramMethod(ProgramClass programClass, ProgramMethod programMethod)
{
    String name        = programMethod.getName(programClass);
    String descriptor  = programMethod.getDescriptor(programClass);
    int    accessFlags = programMethod.getAccessFlags();

    // Does the target class already have such a method?
    ProgramMethod targetMethod = (ProgramMethod)targetClass.findMethod(name, descriptor);
    if (targetMethod != null)
    {
        // is this source method abstract?
        if ((accessFlags & ClassConstants.ACC_ABSTRACT) != 0)
        {
            // Keep the target method.
            if (DEBUG)
            {
                System.out.println("MemberAdder: skipping abstract method ["+programClass.getName()+"."+programMethod.getName(programClass)+programMethod.getDescriptor(programClass)+"] into ["+targetClass.getName()+"]");
            }

            // Don't add a new method.
            return;
        }

        // Is the target method abstract?
        int targetAccessFlags = targetMethod.getAccessFlags();
        if ((targetAccessFlags & ClassConstants.ACC_ABSTRACT) != 0)
        {
            // Keep the abstract method, but update its contents, in order
            // to keep any references to it valid.
            if (DEBUG)
            {
                System.out.println("MemberAdder: updating method ["+programClass.getName()+"."+programMethod.getName(programClass)+programMethod.getDescriptor(programClass)+"] into ["+targetClass.getName()+"]");
            }

            // Replace the access flags.
            targetMethod.u2accessFlags =
                accessFlags & ~ClassConstants.ACC_FINAL;

            // Add and replace the attributes.
            programMethod.attributesAccept(programClass,
                                           new AttributeAdder(targetClass,
                                                              targetMethod,
                                                              true));

            // Don't add a new method.
            return;
        }

        if (DEBUG)
        {
            System.out.println("MemberAdder: renaming method ["+targetClass.getName()+"."+targetMethod.getName(targetClass)+targetMethod.getDescriptor(targetClass)+"]");
        }

        // TODO: Handle non-abstract method with the same name and descriptor in the target class.
        // We currently avoid this case, since renaming the identical method
        // still causes confused method references.
        //// Rename the private (non-abstract) or static method.
        //targetMethod.u2nameIndex =
        //    constantPoolEditor.addUtf8Constant(newUniqueMemberName(name, descriptor));
    }

    if (DEBUG)
    {
        System.out.println("MemberAdder: copying method ["+programClass.getName()+"."+programMethod.getName(programClass)+programMethod.getDescriptor(programClass)+"] into ["+targetClass.getName()+"]");
    }

    // Create a copy of the method.
    ProgramMethod newProgramMethod =
        new ProgramMethod(accessFlags & ~ClassConstants.ACC_FINAL,
                          constantAdder.addConstant(programClass, programMethod.u2nameIndex),
                          constantAdder.addConstant(programClass, programMethod.u2descriptorIndex),
                          0,
                          programMethod.u2attributesCount > 0 ?
                              new Attribute[programMethod.u2attributesCount] :
                              EMPTY_ATTRIBUTES,
                          programMethod.referencedClasses != null ?
                              (Clazz[])programMethod.referencedClasses.clone() :
                              null);

    // Link to its visitor info.
    newProgramMethod.setVisitorInfo(programMethod);

    // Copy its attributes.
    programMethod.attributesAccept(programClass,
                                   new AttributeAdder(targetClass,
                                                      newProgramMethod,
                                                      false));

    // Add the completed method.
    classEditor.addMethod(newProgramMethod);

    // Visit the newly added method, if necessary.
    if (extraMemberVisitor != null)
    {
        extraMemberVisitor.visitProgramMethod(targetClass, newProgramMethod);
    }
}
 
Example #24
Source File: MarkedAnnotationDeleter.java    From proguard with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void visitAnyAttribute(Clazz clazz, Attribute attribute) {}
 
Example #25
Source File: ClassCleaner.java    From java-n-IDE-for-Android with Apache License 2.0 4 votes vote down vote up
public void visitAnyAttribute(Clazz clazz, Attribute attribute)
{
    clean(attribute);
}
 
Example #26
Source File: AttributeUsageMarker.java    From bazel with Apache License 2.0 4 votes vote down vote up
public void visitAnyAttribute(Clazz clazz, Attribute attribute)
{
    markAsUsed(attribute);
}
 
Example #27
Source File: MemberAdder.java    From bazel with Apache License 2.0 4 votes vote down vote up
public void visitProgramField(ProgramClass programClass, ProgramField programField)
{
    //String name        = programField.getName(programClass);
    //String descriptor  = programField.getDescriptor(programClass);
    int    accessFlags = programField.getAccessFlags();

    // TODO: Handle field with the same name and descriptor in the target class.
    // We currently avoid this case, since renaming the identical field
    // still causes confused field references.
    //// Does the target class already have such a field?
    //ProgramField targetField = (ProgramField)targetClass.findField(name, descriptor);
    //if (targetField != null)
    //{
    //    // Is the field private or static?
    //    int targetAccessFlags = targetField.getAccessFlags();
    //    if ((targetAccessFlags &
    //         (ClassConstants.ACC_PRIVATE |
    //          ClassConstants.ACC_STATIC)) != 0)
    //    {
    //        if (DEBUG)
    //        {
    //            System.out.println("MemberAdder: renaming field ["+targetClass+"."+targetField.getName(targetClass)+" "+targetField.getDescriptor(targetClass)+"]");
    //        }
    //
    //        // Rename the private or static field.
    //        targetField.u2nameIndex =
    //            constantPoolEditor.addUtf8Constant(newUniqueMemberName(name, targetClass.getName()));
    //    }
    //    else
    //    {
    //        // Keep the non-private and non-static field, but update its
    //        // contents, in order to keep any references to it valid.
    //        if (DEBUG)
    //        {
    //            System.out.println("MemberAdder: updating field ["+programClass+"."+programField.getName(programClass)+" "+programField.getDescriptor(programClass)+"] into ["+targetClass.getName()+"]");
    //        }
    //
    //        // Combine the access flags.
    //        targetField.u2accessFlags = accessFlags | targetAccessFlags;
    //
    //        // Add and replace any attributes.
    //        programField.attributesAccept(programClass,
    //                                      new AttributeAdder(targetClass,
    //                                                         targetField,
    //                                                         true));
    //
    //        // Don't add a new field.
    //        return;
    //    }
    //}

    if (DEBUG)
    {
        System.out.println("MemberAdder: copying field ["+programClass+"."+programField.getName(programClass)+" "+programField.getDescriptor(programClass)+"] into ["+targetClass.getName()+"]");
    }

    // Create a copy of the field.
    ProgramField newProgramField =
        new ProgramField(accessFlags,
                         constantAdder.addConstant(programClass, programField.u2nameIndex),
                         constantAdder.addConstant(programClass, programField.u2descriptorIndex),
                         0,
                         programField.u2attributesCount > 0 ?
                             new Attribute[programField.u2attributesCount] :
                             EMPTY_ATTRIBUTES,
                         programField.referencedClass);

    // Link to its visitor info.
    newProgramField.setVisitorInfo(programField);

    // Copy its attributes.
    programField.attributesAccept(programClass,
                                  new AttributeAdder(targetClass,
                                                     newProgramField,
                                                     false));

    // Add the completed field.
    classEditor.addField(newProgramField);

    // Visit the newly added field, if necessary.
    if (extraMemberVisitor != null)
    {
        extraMemberVisitor.visitProgramField(targetClass, newProgramField);
    }
}
 
Example #28
Source File: MemberAdder.java    From proguard with GNU General Public License v2.0 4 votes vote down vote up
public void visitProgramField(ProgramClass programClass, ProgramField programField)
{
    //String name        = programField.getName(programClass);
    //String descriptor  = programField.getDescriptor(programClass);
    int    accessFlags = programField.getAccessFlags();

    // TODO: Handle field with the same name and descriptor in the target class.
    // We currently avoid this case, since renaming the identical field
    // still causes confused field references.
    //// Does the target class already have such a field?
    //ProgramField targetField = (ProgramField)targetClass.findField(name, descriptor);
    //if (targetField != null)
    //{
    //    // Is the field private or static?
    //    int targetAccessFlags = targetField.getAccessFlags();
    //    if ((targetAccessFlags &
    //         (ClassConstants.ACC_PRIVATE |
    //          ClassConstants.ACC_STATIC)) != 0)
    //    {
    //        if (DEBUG)
    //        {
    //            System.out.println("MemberAdder: renaming field ["+targetClass+"."+targetField.getName(targetClass)+" "+targetField.getDescriptor(targetClass)+"]");
    //        }
    //
    //        // Rename the private or static field.
    //        targetField.u2nameIndex =
    //            constantPoolEditor.addUtf8Constant(newUniqueMemberName(name, targetClass.getName()));
    //    }
    //    else
    //    {
    //        // Keep the non-private and non-static field, but update its
    //        // contents, in order to keep any references to it valid.
    //        if (DEBUG)
    //        {
    //            System.out.println("MemberAdder: updating field ["+programClass+"."+programField.getName(programClass)+" "+programField.getDescriptor(programClass)+"] into ["+targetClass.getName()+"]");
    //        }
    //
    //        // Combine the access flags.
    //        targetField.u2accessFlags = accessFlags | targetAccessFlags;
    //
    //        // Add and replace any attributes.
    //        programField.attributesAccept(programClass,
    //                                      new AttributeAdder(targetClass,
    //                                                         targetField,
    //                                                         true));
    //
    //        // Don't add a new field.
    //        return;
    //    }
    //}

    if (DEBUG)
    {
        System.out.println("MemberAdder: copying field ["+programClass+"."+programField.getName(programClass)+" "+programField.getDescriptor(programClass)+"] into ["+targetClass.getName()+"]");
    }

    // Create a copy of the field.
    ProgramField newProgramField =
        new ProgramField(accessFlags,
                         constantAdder.addConstant(programClass, programField.u2nameIndex),
                         constantAdder.addConstant(programClass, programField.u2descriptorIndex),
                         0,
                         programField.u2attributesCount > 0 ?
                             new Attribute[programField.u2attributesCount] :
                             EMPTY_ATTRIBUTES,
                         programField.referencedClass);

    // Link to its visitor info.
    newProgramField.setVisitorInfo(programField);

    // Copy its attributes.
    programField.attributesAccept(programClass,
                                  new AttributeAdder(targetClass,
                                                     newProgramField,
                                                     false));

    // Add the completed field.
    classEditor.addField(newProgramField);

    // Visit the newly added field, if necessary.
    if (extraMemberVisitor != null)
    {
        extraMemberVisitor.visitProgramField(targetClass, newProgramField);
    }
}
 
Example #29
Source File: MemberAdder.java    From java-n-IDE-for-Android with Apache License 2.0 4 votes vote down vote up
public void visitProgramMethod(ProgramClass programClass, ProgramMethod programMethod)
{
    String name        = programMethod.getName(programClass);
    String descriptor  = programMethod.getDescriptor(programClass);
    int    accessFlags = programMethod.getAccessFlags();

    // Does the target class already have such a method?
    ProgramMethod targetMethod = (ProgramMethod)targetClass.findMethod(name, descriptor);
    if (targetMethod != null)
    {
        // is this source method abstract?
        if ((accessFlags & ClassConstants.INTERNAL_ACC_ABSTRACT) != 0)
        {
            // Keep the target method.
            if (DEBUG)
            {
                System.out.println("MemberAdder: skipping abstract method ["+programClass.getName()+"."+programMethod.getName(programClass)+programMethod.getDescriptor(programClass)+"] into ["+targetClass.getName()+"]");
            }

            // Don't add a new method.
            return;
        }

        // Is the target method abstract?
        int targetAccessFlags = targetMethod.getAccessFlags();
        if ((targetAccessFlags & ClassConstants.INTERNAL_ACC_ABSTRACT) != 0)
        {
            // Keep the abstract method, but update its contents, in order
            // to keep any references to it valid.
            if (DEBUG)
            {
                System.out.println("MemberAdder: updating method ["+programClass.getName()+"."+programMethod.getName(programClass)+programMethod.getDescriptor(programClass)+"] into ["+targetClass.getName()+"]");
            }

            // Replace the access flags.
            targetMethod.u2accessFlags =
                accessFlags & ~ClassConstants.INTERNAL_ACC_FINAL;

            // Add and replace the attributes.
            programMethod.attributesAccept(programClass,
                                           new AttributeAdder(targetClass,
                                                              targetMethod,
                                                              true));

            // Don't add a new method.
            return;
        }

        if (DEBUG)
        {
            System.out.println("MemberAdder: renaming method ["+targetClass.getName()+"."+targetMethod.getName(targetClass)+targetMethod.getDescriptor(targetClass)+"]");
        }

        // Rename the private (non-abstract) or static method.
        targetMethod.u2nameIndex =
            constantPoolEditor.addUtf8Constant(newUniqueMemberName(name, descriptor));
    }

    if (DEBUG)
    {
        System.out.println("MemberAdder: copying method ["+programClass.getName()+"."+programMethod.getName(programClass)+programMethod.getDescriptor(programClass)+"] into ["+targetClass.getName()+"]");
    }

    // Create a copy of the method.
    ProgramMethod newProgramMethod =
        new ProgramMethod(accessFlags & ~ClassConstants.INTERNAL_ACC_FINAL,
                          constantAdder.addConstant(programClass, programMethod.u2nameIndex),
                          constantAdder.addConstant(programClass, programMethod.u2descriptorIndex),
                          0,
                          programMethod.u2attributesCount > 0 ?
                              new Attribute[programMethod.u2attributesCount] :
                              EMPTY_ATTRIBUTES,
                          programMethod.referencedClasses != null ?
                              (Clazz[])programMethod.referencedClasses.clone() :
                              null);

    // Link to its visitor info.
    newProgramMethod.setVisitorInfo(programMethod);

    // Copy its attributes.
    programMethod.attributesAccept(programClass,
                                   new AttributeAdder(targetClass,
                                                      newProgramMethod,
                                                      false));

    // Add the completed method.
    classEditor.addMethod(newProgramMethod);
}
 
Example #30
Source File: AttributeAdder.java    From java-n-IDE-for-Android with Apache License 2.0 4 votes vote down vote up
public void visitCodeAttribute(Clazz clazz, Method method, CodeAttribute codeAttribute)
{
    // Create a new code attribute.
    CodeAttribute newCodeAttribute =
        new CodeAttribute(constantAdder.addConstant(clazz, codeAttribute.u2attributeNameIndex),
                          codeAttribute.u2maxStack,
                          codeAttribute.u2maxLocals,
                          0,
                          EMPTY_BYTES,
                          0,
                          codeAttribute.u2exceptionTableLength > 0 ?
                              new ExceptionInfo[codeAttribute.u2exceptionTableLength] :
                              EMPTY_EXCEPTIONS,
                          0,
                          codeAttribute.u2attributesCount > 0 ?
                              new Attribute[codeAttribute.u2attributesCount] :
                              EMPTY_ATTRIBUTES);

    CodeAttributeComposer codeAttributeComposer = new CodeAttributeComposer();

    codeAttributeComposer.beginCodeFragment(codeAttribute.u4codeLength + 32);

    // Add the instructions.
    codeAttribute.instructionsAccept(clazz,
                                     method,
                                     new InstructionAdder(targetClass,
                                                          codeAttributeComposer));

    // Append a label just after the code.
    codeAttributeComposer.appendLabel(codeAttribute.u4codeLength);

    // Add the exceptions.
    codeAttribute.exceptionsAccept(clazz,
                                   method,
                                   new ExceptionInfoAdder(targetClass,
                                                          codeAttributeComposer));

    codeAttributeComposer.endCodeFragment();

    // Add the attributes.
    codeAttribute.attributesAccept(clazz,
                                   method,
                                   new AttributeAdder(targetClass,
                                                      targetMember,
                                                      newCodeAttribute,
                                                      replaceAttributes));

    // Apply these changes to the new code attribute.
    codeAttributeComposer.visitCodeAttribute(targetClass,
                                             (Method)targetMember,
                                             newCodeAttribute);

    // Add the completed code attribute to the target method.
    attributesEditor.addAttribute(newCodeAttribute);
}