FileDocCategorySizeDatePackage
AnnotationsDirectoryItem.javaAPI DocAndroid 5.1 API12597Thu Mar 12 22:18:30 GMT 2015com.android.dx.dex.file

AnnotationsDirectoryItem

public final class AnnotationsDirectoryItem extends OffsettedItem
Per-class directory of annotations.

Fields Summary
private static final int
ALIGNMENT
the required alignment for instances of this class
private static final int
HEADER_SIZE
write size of this class's header, in bytes
private static final int
ELEMENT_SIZE
write size of a list element, in bytes
private AnnotationSetItem
classAnnotations
{@code null-ok;} the class-level annotations, if any
private ArrayList
fieldAnnotations
{@code null-ok;} the annotated fields, if any
private ArrayList
methodAnnotations
{@code null-ok;} the annotated methods, if any
private ArrayList
parameterAnnotations
{@code null-ok;} the annotated parameters, if any
Constructors Summary
public AnnotationsDirectoryItem()
Constructs an empty instance.


             
      
        super(ALIGNMENT, -1);

        classAnnotations = null;
        fieldAnnotations = null;
        methodAnnotations = null;
        parameterAnnotations = null;
    
Methods Summary
public voidaddContents(DexFile file)
{@inheritDoc}

        MixedItemSection wordData = file.getWordData();

        if (classAnnotations != null) {
            classAnnotations = wordData.intern(classAnnotations);
        }

        if (fieldAnnotations != null) {
            for (FieldAnnotationStruct item : fieldAnnotations) {
                item.addContents(file);
            }
        }

        if (methodAnnotations != null) {
            for (MethodAnnotationStruct item : methodAnnotations) {
                item.addContents(file);
            }
        }

        if (parameterAnnotations != null) {
            for (ParameterAnnotationStruct item : parameterAnnotations) {
                item.addContents(file);
            }
        }
    
public voidaddFieldAnnotations(com.android.dx.rop.cst.CstFieldRef field, com.android.dx.rop.annotation.Annotations annotations, DexFile dexFile)
Adds a field annotations item to this instance.

param
field {@code non-null;} field in question
param
annotations {@code non-null;} associated annotations to add
param
dexFile {@code non-null;} dex output

        if (fieldAnnotations == null) {
            fieldAnnotations = new ArrayList<FieldAnnotationStruct>();
        }

        fieldAnnotations.add(new FieldAnnotationStruct(field,
                        new AnnotationSetItem(annotations, dexFile)));
    
public voidaddMethodAnnotations(com.android.dx.rop.cst.CstMethodRef method, com.android.dx.rop.annotation.Annotations annotations, DexFile dexFile)
Adds a method annotations item to this instance.

param
method {@code non-null;} method in question
param
annotations {@code non-null;} associated annotations to add
param
dexFile {@code non-null;} dex output

        if (methodAnnotations == null) {
            methodAnnotations = new ArrayList<MethodAnnotationStruct>();
        }

        methodAnnotations.add(new MethodAnnotationStruct(method,
                        new AnnotationSetItem(annotations, dexFile)));
    
public voidaddParameterAnnotations(com.android.dx.rop.cst.CstMethodRef method, com.android.dx.rop.annotation.AnnotationsList list, DexFile dexFile)
Adds a parameter annotations item to this instance.

param
method {@code non-null;} method in question
param
list {@code non-null;} associated list of annotation sets to add
param
dexFile {@code non-null;} dex output

        if (parameterAnnotations == null) {
            parameterAnnotations = new ArrayList<ParameterAnnotationStruct>();
        }

        parameterAnnotations.add(new ParameterAnnotationStruct(method, list, dexFile));
    
public intcompareTo0(OffsettedItem other)
{@inheritDoc}

Note:: This throws an exception if this item is not internable.

see
#isInternable

        if (! isInternable()) {
            throw new UnsupportedOperationException("uninternable instance");
        }

        AnnotationsDirectoryItem otherDirectory =
            (AnnotationsDirectoryItem) other;
        return classAnnotations.compareTo(otherDirectory.classAnnotations);
    
voiddebugPrint(java.io.PrintWriter out)
Prints out the contents of this instance, in a debugging-friendly way. This is meant to be called from {@link ClassDefItem#debugPrint}.

param
out {@code non-null;} where to output to

        if (classAnnotations != null) {
            out.println("  class annotations: " + classAnnotations);
        }

        if (fieldAnnotations != null) {
            out.println("  field annotations:");
            for (FieldAnnotationStruct item : fieldAnnotations) {
                out.println("    " + item.toHuman());
            }
        }

        if (methodAnnotations != null) {
            out.println("  method annotations:");
            for (MethodAnnotationStruct item : methodAnnotations) {
                out.println("    " + item.toHuman());
            }
        }

        if (parameterAnnotations != null) {
            out.println("  parameter annotations:");
            for (ParameterAnnotationStruct item : parameterAnnotations) {
                out.println("    " + item.toHuman());
            }
        }
    
public com.android.dx.rop.annotation.AnnotationsgetMethodAnnotations(com.android.dx.rop.cst.CstMethodRef method)
Gets the method annotations for a given method, if any. This is meant for use by debugging / dumping code.

param
method {@code non-null;} the method
return
{@code null-ok;} the method annotations, if any

        if (methodAnnotations == null) {
            return null;
        }

        for (MethodAnnotationStruct item : methodAnnotations) {
            if (item.getMethod().equals(method)) {
                return item.getAnnotations();
            }
        }

        return null;
    
public com.android.dx.rop.annotation.AnnotationsListgetParameterAnnotations(com.android.dx.rop.cst.CstMethodRef method)
Gets the parameter annotations for a given method, if any. This is meant for use by debugging / dumping code.

param
method {@code non-null;} the method
return
{@code null-ok;} the parameter annotations, if any

        if (parameterAnnotations == null) {
            return null;
        }

        for (ParameterAnnotationStruct item : parameterAnnotations) {
            if (item.getMethod().equals(method)) {
                return item.getAnnotationsList();
            }
        }

        return null;
    
public inthashCode()
{@inheritDoc}

        if (classAnnotations == null) {
            return 0;
        }

        return classAnnotations.hashCode();
    
public booleanisEmpty()
Returns whether this item is empty (has no contents).

return
{@code true} if this item is empty, or {@code false} if not

        return (classAnnotations == null) &&
            (fieldAnnotations == null) &&
            (methodAnnotations == null) &&
            (parameterAnnotations == null);
    
public booleanisInternable()
Returns whether this item is a candidate for interning. The only interning candidates are ones that only have a non-null set of class annotations, with no other lists.

return
{@code true} if this is an interning candidate, or {@code false} if not

        return (classAnnotations != null) &&
            (fieldAnnotations == null) &&
            (methodAnnotations == null) &&
            (parameterAnnotations == null);
    
public ItemTypeitemType()
{@inheritDoc}

        return ItemType.TYPE_ANNOTATIONS_DIRECTORY_ITEM;
    
private static intlistSize(java.util.ArrayList list)
Gets the list size of the given list, or {@code 0} if given {@code null}.

param
list {@code null-ok;} the list in question
return
{@code >= 0;} its size

        if (list == null) {
            return 0;
        }

        return list.size();
    
protected voidplace0(Section addedTo, int offset)
{@inheritDoc}

        // We just need to set the write size here.

        int elementCount = listSize(fieldAnnotations)
            + listSize(methodAnnotations) + listSize(parameterAnnotations);
        setWriteSize(HEADER_SIZE + (elementCount * ELEMENT_SIZE));
    
public voidsetClassAnnotations(com.android.dx.rop.annotation.Annotations annotations, DexFile dexFile)
Sets the direct annotations on this instance. These are annotations made on the class, per se, as opposed to on one of its members. It is only valid to call this method at most once per instance.

param
annotations {@code non-null;} annotations to set for this class
param
dexFile {@code non-null;} dex output

        if (annotations == null) {
            throw new NullPointerException("annotations == null");
        }

        if (classAnnotations != null) {
            throw new UnsupportedOperationException(
                    "class annotations already set");
        }

        classAnnotations = new AnnotationSetItem(annotations, dexFile);
    
public java.lang.StringtoHuman()
{@inheritDoc}

        throw new RuntimeException("unsupported");
    
protected voidwriteTo0(DexFile file, com.android.dx.util.AnnotatedOutput out)
{@inheritDoc}

        boolean annotates = out.annotates();
        int classOff = OffsettedItem.getAbsoluteOffsetOr0(classAnnotations);
        int fieldsSize = listSize(fieldAnnotations);
        int methodsSize = listSize(methodAnnotations);
        int parametersSize = listSize(parameterAnnotations);

        if (annotates) {
            out.annotate(0, offsetString() + " annotations directory");
            out.annotate(4, "  class_annotations_off: " + Hex.u4(classOff));
            out.annotate(4, "  fields_size:           " +
                    Hex.u4(fieldsSize));
            out.annotate(4, "  methods_size:          " +
                    Hex.u4(methodsSize));
            out.annotate(4, "  parameters_size:       " +
                    Hex.u4(parametersSize));
        }

        out.writeInt(classOff);
        out.writeInt(fieldsSize);
        out.writeInt(methodsSize);
        out.writeInt(parametersSize);

        if (fieldsSize != 0) {
            Collections.sort(fieldAnnotations);
            if (annotates) {
                out.annotate(0, "  fields:");
            }
            for (FieldAnnotationStruct item : fieldAnnotations) {
                item.writeTo(file, out);
            }
        }

        if (methodsSize != 0) {
            Collections.sort(methodAnnotations);
            if (annotates) {
                out.annotate(0, "  methods:");
            }
            for (MethodAnnotationStruct item : methodAnnotations) {
                item.writeTo(file, out);
            }
        }

        if (parametersSize != 0) {
            Collections.sort(parameterAnnotations);
            if (annotates) {
                out.annotate(0, "  parameters:");
            }
            for (ParameterAnnotationStruct item : parameterAnnotations) {
                item.writeTo(file, out);
            }
        }