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.dexgen.dex.file;
18 
19 import com.android.dexgen.util.AnnotatedOutput;
20 import com.android.dexgen.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} */
addContents(DexFile file)57     public void addContents(DexFile file) {
58         MixedItemSection wordData = file.getWordData();
59 
60         annotations = wordData.intern(annotations);
61     }
62 
63     /** {@inheritDoc} */
64     @Override
toHuman()65     public String toHuman() {
66         return annotations.toHuman();
67     }
68 
69     /** {@inheritDoc} */
70     @Override
writeTo0(DexFile file, AnnotatedOutput out)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