FileDocCategorySizeDatePackage
AnnotationsDirectoryItem.javaAPI DocAndroid 1.5 API12257Wed May 06 22:41:02 BST 2009com.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
null-ok; the class-level annotations, if any
private ArrayList
fieldAnnotations
null-ok; the annotated fields, if any
private ArrayList
methodAnnotations
null-ok; the annotated methods, if any
private ArrayList
parameterAnnotations
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)
Adds a field annotations item to this instance.

param
field non-null; field in question
param
annotations non-null; associated annotations to add

        if (fieldAnnotations == null) {
            fieldAnnotations = new ArrayList<FieldAnnotationStruct>();
        }
        
        fieldAnnotations.add(new FieldAnnotationStruct(field,
                        new AnnotationSetItem(annotations)));
    
public voidaddMethodAnnotations(com.android.dx.rop.cst.CstMethodRef method, com.android.dx.rop.annotation.Annotations annotations)
Adds a method annotations item to this instance.

param
method non-null; method in question
param
annotations non-null; associated annotations to add

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

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

param
method non-null; method in question
param
list non-null; associated list of annotation sets to add

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

        parameterAnnotations.add(new ParameterAnnotationStruct(method, list));
    
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 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 non-null; the method
return
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 non-null; the method
return
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
true if this item is empty, or 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
true if this is an interning candidate, or 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 0 if given null.

param
list null-ok; the list in question
return
>= 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)
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 non-null; annotations to set for this class

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

        classAnnotations = new AnnotationSetItem(annotations);
    
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);
            }
        }