• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2007 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.rop.cst.CstUtf8;
20 import com.android.dexgen.util.AnnotatedOutput;
21 import com.android.dexgen.util.Hex;
22 
23 /**
24  * Representation of a string inside a Dalvik file.
25  */
26 public final class StringIdItem
27         extends IndexedItem implements Comparable<StringIdItem> {
28     /** size of instances when written out to a file, in bytes */
29     public static final int WRITE_SIZE = 4;
30 
31     /** {@code non-null;} the string value */
32     private final CstUtf8 value;
33 
34     /** {@code null-ok;} associated string data object, if known */
35     private StringDataItem data;
36 
37     /**
38      * Constructs an instance.
39      *
40      * @param value {@code non-null;} the string value
41      */
StringIdItem(CstUtf8 value)42     public StringIdItem(CstUtf8 value) {
43         if (value == null) {
44             throw new NullPointerException("value == null");
45         }
46 
47         this.value = value;
48         this.data = null;
49     }
50 
51     /** {@inheritDoc} */
52     @Override
equals(Object other)53     public boolean equals(Object other) {
54         if (!(other instanceof StringIdItem)) {
55             return false;
56         }
57 
58         StringIdItem otherString = (StringIdItem) other;
59         return value.equals(otherString.value);
60     }
61 
62     /** {@inheritDoc} */
63     @Override
hashCode()64     public int hashCode() {
65         return value.hashCode();
66     }
67 
68     /** {@inheritDoc} */
compareTo(StringIdItem other)69     public int compareTo(StringIdItem other) {
70         return value.compareTo(other.value);
71     }
72 
73     /** {@inheritDoc} */
74     @Override
itemType()75     public ItemType itemType() {
76         return ItemType.TYPE_STRING_ID_ITEM;
77     }
78 
79     /** {@inheritDoc} */
80     @Override
writeSize()81     public int writeSize() {
82         return WRITE_SIZE;
83     }
84 
85     /** {@inheritDoc} */
86     @Override
addContents(DexFile file)87     public void addContents(DexFile file) {
88         if (data == null) {
89             // The string data hasn't yet been added, so add it.
90             MixedItemSection stringData = file.getStringData();
91             data = new StringDataItem(value);
92             stringData.add(data);
93         }
94     }
95 
96     /** {@inheritDoc} */
97     @Override
writeTo(DexFile file, AnnotatedOutput out)98     public void writeTo(DexFile file, AnnotatedOutput out) {
99         int dataOff = data.getAbsoluteOffset();
100 
101         if (out.annotates()) {
102             out.annotate(0, indexString() + ' ' + value.toQuoted(100));
103             out.annotate(4, "  string_data_off: " + Hex.u4(dataOff));
104         }
105 
106         out.writeInt(dataOff);
107     }
108 
109     /**
110      * Gets the string value.
111      *
112      * @return {@code non-null;} the value
113      */
getValue()114     public CstUtf8 getValue() {
115         return value;
116     }
117 
118     /**
119      * Gets the associated data object for this instance, if known.
120      *
121      * @return {@code null-ok;} the associated data object or {@code null}
122      * if not yet known
123      */
getData()124     public StringDataItem getData() {
125         return data;
126     }
127 }
128