1/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.dx.dex.file;
18
19import com.android.dx.util.AnnotatedOutput;
20import com.android.dx.util.Hex;
21
22/**
23 * Indirect reference to an {@link AnnotationSetItem}.
24 */
25public final class AnnotationSetRefItem extends OffsettedItem {
26    /** the required alignment for instances of this class */
27    private static final int ALIGNMENT = 4;
28
29    /** write size of this class, in bytes */
30    private static final int WRITE_SIZE = 4;
31
32    /** {@code non-null;} the annotation set to refer to */
33    private AnnotationSetItem annotations;
34
35    /**
36     * Constructs an instance.
37     *
38     * @param annotations {@code non-null;} the annotation set to refer to
39     */
40    public AnnotationSetRefItem(AnnotationSetItem annotations) {
41        super(ALIGNMENT, WRITE_SIZE);
42
43        if (annotations == null) {
44            throw new NullPointerException("annotations == null");
45        }
46
47        this.annotations = annotations;
48    }
49
50    /** {@inheritDoc} */
51    @Override
52    public ItemType itemType() {
53        return ItemType.TYPE_ANNOTATION_SET_REF_ITEM;
54    }
55
56    /** {@inheritDoc} */
57    public void addContents(DexFile file) {
58        MixedItemSection wordData = file.getWordData();
59
60        annotations = wordData.intern(annotations);
61    }
62
63    /** {@inheritDoc} */
64    @Override
65    public String toHuman() {
66        return annotations.toHuman();
67    }
68
69    /** {@inheritDoc} */
70    @Override
71    protected void writeTo0(DexFile file, AnnotatedOutput out) {
72        int annotationsOff = annotations.getAbsoluteOffset();
73
74        if (out.annotates()) {
75            out.annotate(4, "  annotations_off: " + Hex.u4(annotationsOff));
76        }
77
78        out.writeInt(annotationsOff);
79    }
80}
81