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

AnnotationItem

public final class AnnotationItem extends OffsettedItem
Single annotation, which consists of a type and a set of name-value element pairs.

Fields Summary
private static final int
VISIBILITY_BUILD
annotation visibility constant: visible at build time only
private static final int
VISIBILITY_RUNTIME
annotation visibility constant: visible at runtime
private static final int
VISIBILITY_SYSTEM
annotation visibility constant: visible at runtime only to system
private static final int
ALIGNMENT
the required alignment for instances of this class
private static final TypeIdSorter
TYPE_ID_SORTER
{@code non-null;} unique instance of {@link #TypeIdSorter}
private final com.android.dx.rop.annotation.Annotation
annotation
{@code non-null;} the annotation to represent
private TypeIdItem
type
{@code null-ok;} type reference for the annotation type; set during {@link #addContents}
private byte[]
encodedForm
{@code null-ok;} encoded form, ready for writing to a file; set during {@link #place0}
Constructors Summary
public AnnotationItem(com.android.dx.rop.annotation.Annotation annotation, DexFile dexFile)
Constructs an instance.

param
annotation {@code non-null;} annotation to represent
param
dexFile {@code non-null;} dex output

        /*
         * The write size isn't known up-front because (the variable-lengthed)
         * leb128 type is used to represent some things.
         */
        super(ALIGNMENT, -1);

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

        this.annotation = annotation;
        this.type = null;
        this.encodedForm = null;
        addContents(dexFile);
    
Methods Summary
public voidaddContents(DexFile file)
{@inheritDoc}

        type = file.getTypeIds().intern(annotation.getType());
        ValueEncoder.addContents(file, annotation);
    
public voidannotateTo(com.android.dx.util.AnnotatedOutput out, java.lang.String prefix)
Write a (listing file) annotation for this instance to the given output, that consumes no bytes of output. This is for annotating a reference to this instance at the point of the reference.

param
out {@code non-null;} where to output to
param
prefix {@code non-null;} prefix for each line of output

        out.annotate(0, prefix + "visibility: " +
                annotation.getVisibility().toHuman());
        out.annotate(0, prefix + "type: " + annotation.getType().toHuman());

        for (NameValuePair pair : annotation.getNameValuePairs()) {
            CstString name = pair.getName();
            Constant value = pair.getValue();

            out.annotate(0, prefix + name.toHuman() + ": " +
                    ValueEncoder.constantToHuman(value));
        }
    
protected intcompareTo0(OffsettedItem other)
{@inheritDoc}

        AnnotationItem otherAnnotation = (AnnotationItem) other;

        return annotation.compareTo(otherAnnotation.annotation);
    
public inthashCode()
{@inheritDoc}

        return annotation.hashCode();
    
public ItemTypeitemType()
{@inheritDoc}

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

        // Encode the data and note the size.

        ByteArrayAnnotatedOutput out = new ByteArrayAnnotatedOutput();
        ValueEncoder encoder = new ValueEncoder(addedTo.getFile(), out);

        encoder.writeAnnotation(annotation, false);
        encodedForm = out.toByteArray();

        // Add one for the visibility byte in front of the encoded annotation.
        setWriteSize(encodedForm.length + 1);
    
public static voidsortByTypeIdIndex(com.android.dx.dex.file.AnnotationItem[] array)
Sorts an array of instances, in place, by type id index, ignoring all other aspects of the elements. This is only valid to use after type id indices are known.

param
array {@code non-null;} array to sort

        Arrays.sort(array, TYPE_ID_SORTER);
    
public java.lang.StringtoHuman()
{@inheritDoc}

        return annotation.toHuman();
    
protected voidwriteTo0(DexFile file, com.android.dx.util.AnnotatedOutput out)
{@inheritDoc}

        boolean annotates = out.annotates();
        AnnotationVisibility visibility = annotation.getVisibility();

        if (annotates) {
            out.annotate(0, offsetString() + " annotation");
            out.annotate(1, "  visibility: VISBILITY_" + visibility);
        }

        switch (visibility) {
            case BUILD:   out.writeByte(VISIBILITY_BUILD); break;
            case RUNTIME: out.writeByte(VISIBILITY_RUNTIME); break;
            case SYSTEM:  out.writeByte(VISIBILITY_SYSTEM); break;
            default: {
                // EMBEDDED shouldn't appear at the top level.
                throw new RuntimeException("shouldn't happen");
            }
        }

        if (annotates) {
            /*
             * The output is to be annotated, so redo the work previously
             * done by place0(), except this time annotations will actually
             * get emitted.
             */
            ValueEncoder encoder = new ValueEncoder(file, out);
            encoder.writeAnnotation(annotation, true);
        } else {
            out.write(encodedForm);
        }