OSDN Git Service

c3207311f95722b3f206480455b78e411b74f306
[android-x86/dalvik.git] / dx / src / com / android / dx / dex / file / FieldIdsSection.java
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.CstFieldRef;
21 import com.android.dx.util.AnnotatedOutput;
22 import com.android.dx.util.Hex;
23
24 import java.util.Collection;
25 import java.util.TreeMap;
26
27 /**
28  * Field refs list section of a {@code .dex} file.
29  */
30 public final class FieldIdsSection extends MemberIdsSection {
31     /**
32      * {@code non-null;} map from field constants to {@link
33      * FieldIdItem} instances
34      */
35     private final TreeMap<CstFieldRef, FieldIdItem> fieldIds;
36
37     /**
38      * Constructs an instance. The file offset is initially unknown.
39      *
40      * @param file {@code non-null;} file that this instance is part of
41      */
42     public FieldIdsSection(DexFile file) {
43         super("field_ids", file);
44
45         fieldIds = new TreeMap<CstFieldRef, FieldIdItem>();
46     }
47
48     /** {@inheritDoc} */
49     @Override
50     public Collection<? extends Item> items() {
51         return fieldIds.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 = fieldIds.get((CstFieldRef) 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 {@code non-null;} where to write
76      */
77     public void writeHeaderPart(AnnotatedOutput out) {
78         throwIfNotPrepared();
79
80         int sz = fieldIds.size();
81         int offset = (sz == 0) ? 0 : getFileOffset();
82
83         if (out.annotates()) {
84             out.annotate(4, "field_ids_size:  " + Hex.u4(sz));
85             out.annotate(4, "field_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 field {@code non-null;} the reference to intern
96      * @return {@code non-null;} the interned reference
97      */
98     public FieldIdItem intern(CstFieldRef field) {
99         if (field == null) {
100             throw new NullPointerException("field == null");
101         }
102
103         throwIfPrepared();
104
105         FieldIdItem result = fieldIds.get(field);
106
107         if (result == null) {
108             result = new FieldIdItem(field);
109             fieldIds.put(field, 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 {@code non-null;} the reference to look up
120      * @return {@code >= 0;} the reference's index
121      */
122     public int indexOf(CstFieldRef ref) {
123         if (ref == null) {
124             throw new NullPointerException("ref == null");
125         }
126
127         throwIfNotPrepared();
128
129         FieldIdItem item = fieldIds.get(ref);
130
131         if (item == null) {
132             throw new IllegalArgumentException("not found");
133         }
134
135         return item.getIndex();
136     }
137 }