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 
17 package com.android.dx.dex.file;
18 
19 import com.android.dx.util.AnnotatedOutput;
20 import com.android.dx.util.Hex;
21 
22 /**
23  * Indirect reference to an {@link AnnotationSetItem}.
24  */
25 public 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      */
AnnotationSetRefItem(AnnotationSetItem annotations)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
itemType()52     public ItemType itemType() {
53         return ItemType.TYPE_ANNOTATION_SET_REF_ITEM;
54     }
55 
56     /** {@inheritDoc} */
57     @Override
addContents(DexFile file)58     public void addContents(DexFile file) {
59         MixedItemSection wordData = file.getWordData();
60 
61         annotations = wordData.intern(annotations);
62     }
63 
64     /** {@inheritDoc} */
65     @Override
toHuman()66     public String toHuman() {
67         return annotations.toHuman();
68     }
69 
70     /** {@inheritDoc} */
71     @Override
writeTo0(DexFile file, AnnotatedOutput out)72     protected void writeTo0(DexFile file, AnnotatedOutput out) {
73         int annotationsOff = annotations.getAbsoluteOffset();
74 
75         if (out.annotates()) {
76             out.annotate(4, "  annotations_off: " + Hex.u4(annotationsOff));
77         }
78 
79         out.writeInt(annotationsOff);
80     }
81 }
82