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.Constant;
20 import com.android.dx.rop.cst.CstFieldRef;
21 import com.android.dx.util.AnnotatedOutput;
22 import com.android.dx.util.Hex;
24 import java.util.Collection;
25 import java.util.TreeMap;
28 * Field refs list section of a {@code .dex} file.
30 public final class FieldIdsSection extends MemberIdsSection {
32 * {@code non-null;} map from field constants to {@link
33 * FieldIdItem} instances
35 private final TreeMap<CstFieldRef, FieldIdItem> fieldIds;
38 * Constructs an instance. The file offset is initially unknown.
40 * @param file {@code non-null;} file that this instance is part of
42 public FieldIdsSection(DexFile file) {
43 super("field_ids", file);
45 fieldIds = new TreeMap<CstFieldRef, FieldIdItem>();
50 public Collection<? extends Item> items() {
51 return fieldIds.values();
56 public IndexedItem get(Constant cst) {
58 throw new NullPointerException("cst == null");
63 IndexedItem result = fieldIds.get((CstFieldRef) cst);
66 throw new IllegalArgumentException("not found");
73 * Writes the portion of the file header that refers to this instance.
75 * @param out {@code non-null;} where to write
77 public void writeHeaderPart(AnnotatedOutput out) {
80 int sz = fieldIds.size();
81 int offset = (sz == 0) ? 0 : getFileOffset();
83 if (out.annotates()) {
84 out.annotate(4, "field_ids_size: " + Hex.u4(sz));
85 out.annotate(4, "field_ids_off: " + Hex.u4(offset));
93 * Interns an element into this instance.
95 * @param field {@code non-null;} the reference to intern
96 * @return {@code non-null;} the interned reference
98 public FieldIdItem intern(CstFieldRef field) {
100 throw new NullPointerException("field == null");
105 FieldIdItem result = fieldIds.get(field);
107 if (result == null) {
108 result = new FieldIdItem(field);
109 fieldIds.put(field, result);
116 * Gets the index of the given reference, which must have been added
119 * @param ref {@code non-null;} the reference to look up
120 * @return {@code >= 0;} the reference's index
122 public int indexOf(CstFieldRef ref) {
124 throw new NullPointerException("ref == null");
127 throwIfNotPrepared();
129 FieldIdItem item = fieldIds.get(ref);
132 throw new IllegalArgumentException("not found");
135 return item.getIndex();