2 * Copyright (C) 2007 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.dx.dex.file;
19 import com.android.dx.rop.cst.CstUtf8;
20 import com.android.dx.util.AnnotatedOutput;
21 import com.android.dx.util.Hex;
24 * Representation of a string inside a Dalvik file.
26 public final class StringIdItem
27 extends IndexedItem implements Comparable {
28 /** size of instances when written out to a file, in bytes */
29 public static final int WRITE_SIZE = 4;
31 /** {@code non-null;} the string value */
32 private final CstUtf8 value;
34 /** {@code null-ok;} associated string data object, if known */
35 private StringDataItem data;
38 * Constructs an instance.
40 * @param value {@code non-null;} the string value
42 public StringIdItem(CstUtf8 value) {
44 throw new NullPointerException("value == null");
53 public boolean equals(Object other) {
54 if (!(other instanceof StringIdItem)) {
58 StringIdItem otherString = (StringIdItem) other;
59 return value.equals(otherString.value);
64 public int hashCode() {
65 return value.hashCode();
69 public int compareTo(Object other) {
70 StringIdItem otherString = (StringIdItem) other;
71 return value.compareTo(otherString.value);
76 public ItemType itemType() {
77 return ItemType.TYPE_STRING_ID_ITEM;
82 public int writeSize() {
88 public void addContents(DexFile file) {
90 // The string data hasn't yet been added, so add it.
91 MixedItemSection stringData = file.getStringData();
92 data = new StringDataItem(value);
99 public void writeTo(DexFile file, AnnotatedOutput out) {
100 int dataOff = data.getAbsoluteOffset();
102 if (out.annotates()) {
103 out.annotate(0, indexString() + ' ' + value.toQuoted(100));
104 out.annotate(4, " string_data_off: " + Hex.u4(dataOff));
107 out.writeInt(dataOff);
111 * Gets the string value.
113 * @return {@code non-null;} the value
115 public CstUtf8 getValue() {
120 * Gets the associated data object for this instance, if known.
122 * @return {@code null-ok;} the associated data object or {@code null}
125 public StringDataItem getData() {