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
17package com.android.dx.dex.file;
18
19import com.android.dx.rop.cst.Constant;
20import com.android.dx.rop.cst.CstFieldRef;
21import com.android.dx.util.AnnotatedOutput;
22import com.android.dx.util.Hex;
23import java.util.Collection;
24import java.util.TreeMap;
25
26/**
27 * Field refs list section of a {@code .dex} file.
28 */
29public final class FieldIdsSection extends MemberIdsSection {
30    /**
31     * {@code non-null;} map from field constants to {@link
32     * FieldIdItem} instances
33     */
34    private final TreeMap<CstFieldRef, FieldIdItem> fieldIds;
35
36    /**
37     * Constructs an instance. The file offset is initially unknown.
38     *
39     * @param file {@code non-null;} file that this instance is part of
40     */
41    public FieldIdsSection(DexFile file) {
42        super("field_ids", file);
43
44        fieldIds = new TreeMap<CstFieldRef, FieldIdItem>();
45    }
46
47    /** {@inheritDoc} */
48    @Override
49    public Collection<? extends Item> items() {
50        return fieldIds.values();
51    }
52
53    /** {@inheritDoc} */
54    @Override
55    public IndexedItem get(Constant cst) {
56        if (cst == null) {
57            throw new NullPointerException("cst == null");
58        }
59
60        throwIfNotPrepared();
61
62        IndexedItem result = fieldIds.get((CstFieldRef) cst);
63
64        if (result == null) {
65            throw new IllegalArgumentException("not found");
66        }
67
68        return result;
69    }
70
71    /**
72     * Writes the portion of the file header that refers to this instance.
73     *
74     * @param out {@code non-null;} where to write
75     */
76    public void writeHeaderPart(AnnotatedOutput out) {
77        throwIfNotPrepared();
78
79        int sz = fieldIds.size();
80        int offset = (sz == 0) ? 0 : getFileOffset();
81
82        if (out.annotates()) {
83            out.annotate(4, "field_ids_size:  " + Hex.u4(sz));
84            out.annotate(4, "field_ids_off:   " + Hex.u4(offset));
85        }
86
87        out.writeInt(sz);
88        out.writeInt(offset);
89    }
90
91    /**
92     * Interns an element into this instance.
93     *
94     * @param field {@code non-null;} the reference to intern
95     * @return {@code non-null;} the interned reference
96     */
97    public FieldIdItem intern(CstFieldRef field) {
98        if (field == null) {
99            throw new NullPointerException("field == null");
100        }
101
102        throwIfPrepared();
103
104        FieldIdItem result = fieldIds.get(field);
105
106        if (result == null) {
107            result = new FieldIdItem(field);
108            fieldIds.put(field, result);
109        }
110
111        return result;
112    }
113
114    /**
115     * Gets the index of the given reference, which must have been added
116     * to this instance.
117     *
118     * @param ref {@code non-null;} the reference to look up
119     * @return {@code >= 0;} the reference's index
120     */
121    public int indexOf(CstFieldRef ref) {
122        if (ref == null) {
123            throw new NullPointerException("ref == null");
124        }
125
126        throwIfNotPrepared();
127
128        FieldIdItem item = fieldIds.get(ref);
129
130        if (item == null) {
131            throw new IllegalArgumentException("not found");
132        }
133
134        return item.getIndex();
135    }
136}
137