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