• 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.dx.dex.file;
18 
19 import com.android.dx.rop.cst.Constant;
20 import com.android.dx.rop.cst.CstBaseMethodRef;
21 import com.android.dx.util.AnnotatedOutput;
22 import com.android.dx.util.Hex;
23 import java.util.Collection;
24 import java.util.TreeMap;
25 
26 /**
27  * Method refs list section of a {@code .dex} file.
28  */
29 public final class MethodIdsSection extends MemberIdsSection {
30     /**
31      * {@code non-null;} map from method constants to {@link
32      * MethodIdItem} instances
33      */
34     private final TreeMap<CstBaseMethodRef, MethodIdItem> methodIds;
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      */
MethodIdsSection(DexFile file)41     public MethodIdsSection(DexFile file) {
42         super("method_ids", file);
43 
44         methodIds = new TreeMap<CstBaseMethodRef, MethodIdItem>();
45     }
46 
47     /** {@inheritDoc} */
48     @Override
items()49     public Collection<? extends Item> items() {
50         return methodIds.values();
51     }
52 
53     /** {@inheritDoc} */
54     @Override
get(Constant cst)55     public IndexedItem get(Constant cst) {
56         if (cst == null) {
57             throw new NullPointerException("cst == null");
58         }
59 
60         throwIfNotPrepared();
61 
62         IndexedItem result = methodIds.get((CstBaseMethodRef) 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      */
writeHeaderPart(AnnotatedOutput out)76     public void writeHeaderPart(AnnotatedOutput out) {
77         throwIfNotPrepared();
78 
79         int sz = methodIds.size();
80         int offset = (sz == 0) ? 0 : getFileOffset();
81 
82         if (out.annotates()) {
83             out.annotate(4, "method_ids_size: " + Hex.u4(sz));
84             out.annotate(4, "method_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 method {@code non-null;} the reference to intern
95      * @return {@code non-null;} the interned reference
96      */
intern(CstBaseMethodRef method)97     public synchronized MethodIdItem intern(CstBaseMethodRef method) {
98         if (method == null) {
99             throw new NullPointerException("method == null");
100         }
101 
102         throwIfPrepared();
103 
104         MethodIdItem result = methodIds.get(method);
105 
106         if (result == null) {
107             result = new MethodIdItem(method);
108             methodIds.put(method, 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      */
indexOf(CstBaseMethodRef ref)121     public int indexOf(CstBaseMethodRef ref) {
122         if (ref == null) {
123             throw new NullPointerException("ref == null");
124         }
125 
126         throwIfNotPrepared();
127 
128         MethodIdItem item = methodIds.get(ref);
129 
130         if (item == null) {
131             throw new IllegalArgumentException("not found");
132         }
133 
134         return item.getIndex();
135     }
136 }
137