1579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson/*
2579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson * Copyright (C) 2008 The Android Open Source Project
3579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson *
4579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson * Licensed under the Apache License, Version 2.0 (the "License");
5579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson * you may not use this file except in compliance with the License.
6579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson * You may obtain a copy of the License at
7579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson *
8579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson *      http://www.apache.org/licenses/LICENSE-2.0
9579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson *
10579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson * Unless required by applicable law or agreed to in writing, software
11579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson * distributed under the License is distributed on an "AS IS" BASIS,
12579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson * See the License for the specific language governing permissions and
14579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson * limitations under the License.
15579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson */
16579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson
17579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilsonpackage com.android.dx.dex.file;
18579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson
19579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilsonimport com.android.dx.util.AnnotatedOutput;
20579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilsonimport com.android.dx.util.Hex;
21579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson
22579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson/**
23579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson * Indirect reference to an {@link AnnotationSetItem}.
24579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson */
25579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilsonpublic final class AnnotationSetRefItem extends OffsettedItem {
26579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson    /** the required alignment for instances of this class */
27579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson    private static final int ALIGNMENT = 4;
28579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson
29579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson    /** write size of this class, in bytes */
30579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson    private static final int WRITE_SIZE = 4;
31579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson
32579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson    /** {@code non-null;} the annotation set to refer to */
33579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson    private AnnotationSetItem annotations;
34579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson
35579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson    /**
36579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson     * Constructs an instance.
37579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson     *
38579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson     * @param annotations {@code non-null;} the annotation set to refer to
39579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson     */
40579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson    public AnnotationSetRefItem(AnnotationSetItem annotations) {
41579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson        super(ALIGNMENT, WRITE_SIZE);
42579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson
43579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson        if (annotations == null) {
44579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson            throw new NullPointerException("annotations == null");
45579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson        }
46579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson
47579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson        this.annotations = annotations;
48579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson    }
49579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson
50579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson    /** {@inheritDoc} */
51579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson    @Override
52579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson    public ItemType itemType() {
53579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson        return ItemType.TYPE_ANNOTATION_SET_REF_ITEM;
54579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson    }
55579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson
56579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson    /** {@inheritDoc} */
57579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson    public void addContents(DexFile file) {
58579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson        MixedItemSection wordData = file.getWordData();
59579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson
60579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson        annotations = wordData.intern(annotations);
61579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson    }
62579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson
63579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson    /** {@inheritDoc} */
64579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson    @Override
65579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson    public String toHuman() {
66579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson        return annotations.toHuman();
67579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson    }
68579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson
69579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson    /** {@inheritDoc} */
70579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson    @Override
71579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson    protected void writeTo0(DexFile file, AnnotatedOutput out) {
72579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson        int annotationsOff = annotations.getAbsoluteOffset();
73579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson
74579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson        if (out.annotates()) {
75579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson            out.annotate(4, "  annotations_off: " + Hex.u4(annotationsOff));
76579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson        }
77579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson
78579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson        out.writeInt(annotationsOff);
79579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson    }
80579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson}
81