AnnotationSetItem.java revision fa07a1972e3cff56d5615c18a8797ff58fc9f739
1/*
2 * [The "BSD licence"]
3 * Copyright (c) 2009 Ben Gruver
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 *    derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29package org.jf.dexlib;
30
31import java.util.ArrayList;
32import java.util.List;
33
34public class AnnotationSetItem extends OffsettedItem<AnnotationSetItem> {
35    private final ArrayList<OffsettedItemReference<AnnotationItem>> annotationReferences =
36            new ArrayList<OffsettedItemReference<AnnotationItem>>();
37
38    private final ListSizeField annotationCountField;
39    private final FieldListField<OffsettedItemReference<AnnotationItem>> annotationsListField;
40
41    public AnnotationSetItem(final DexFile dexFile, int offset) {
42        super(offset);
43
44        fields = new Field[] {
45                annotationCountField = new ListSizeField(annotationReferences, new IntegerField("size")),
46                annotationsListField = new FieldListField<OffsettedItemReference<AnnotationItem>>(
47                        annotationReferences, "annotation") {
48                    protected OffsettedItemReference<AnnotationItem> make() {
49                        return new OffsettedItemReference<AnnotationItem>(dexFile.AnnotationsSection,
50                                new IntegerField(null), "annotation_off");
51                    }
52                }
53        };
54    }
55
56    public AnnotationSetItem(final DexFile dexFile, List<AnnotationItem> annotations) {
57        this(dexFile, -1);
58
59        for (AnnotationItem annotationItem: annotations) {
60            OffsettedItemReference<AnnotationItem> annotationReference = annotationsListField.make();
61            annotationReference.setReference(annotationItem);
62            this.annotationReferences.add(annotationReference);
63        }
64    }
65
66    protected int getAlignment() {
67        return 4;
68    }
69
70    public ItemType getItemType() {
71        return ItemType.TYPE_ANNOTATION_SET_ITEM;
72    }
73
74    public String getConciseIdentity() {
75        return "annotation_set_item @0x" + Integer.toHexString(getOffset());
76    }
77
78    public List<AnnotationItem> getAnnotationItems() {
79        List<AnnotationItem> annotationItems = new ArrayList<AnnotationItem>();
80
81        for (OffsettedItemReference<AnnotationItem> annotationItemReference: annotationReferences) {
82            annotationItems.add(annotationItemReference.getReference());
83        }
84        return annotationItems;
85    }
86}
87