OSDN Git Service

Mechanical refactoring of dx into two parts.
[android-x86/dalvik.git] / dx / src / com / android / dx / dex / file / ClassDefsSection.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.CstType;
21 import com.android.dx.rop.type.Type;
22 import com.android.dx.rop.type.TypeList;
23 import com.android.dx.util.AnnotatedOutput;
24 import com.android.dx.util.Hex;
25 import java.util.ArrayList;
26 import java.util.Collection;
27 import java.util.TreeMap;
28
29 /**
30  * Class definitions list section of a {@code .dex} file.
31  */
32 public final class ClassDefsSection extends UniformItemSection {
33     /**
34      * {@code non-null;} map from type constants for classes to {@link
35      * ClassDefItem} instances that define those classes
36      */
37     private final TreeMap<Type, ClassDefItem> classDefs;
38
39     /** {@code null-ok;} ordered list of classes; set in {@link #orderItems} */
40     private ArrayList<ClassDefItem> orderedDefs;
41
42     /**
43      * Constructs an instance. The file offset is initially unknown.
44      *
45      * @param file {@code non-null;} file that this instance is part of
46      */
47     public ClassDefsSection(DexFile file) {
48         super("class_defs", file, 4);
49
50         classDefs = new TreeMap<Type, ClassDefItem>();
51         orderedDefs = null;
52     }
53
54     /** {@inheritDoc} */
55     @Override
56     public Collection<? extends Item> items() {
57         if (orderedDefs != null) {
58             return orderedDefs;
59         }
60
61         return classDefs.values();
62     }
63
64     /** {@inheritDoc} */
65     @Override
66     public IndexedItem get(Constant cst) {
67         if (cst == null) {
68             throw new NullPointerException("cst == null");
69         }
70
71         throwIfNotPrepared();
72
73         Type type = ((CstType) cst).getClassType();
74         IndexedItem result = classDefs.get(type);
75
76         if (result == null) {
77             throw new IllegalArgumentException("not found");
78         }
79
80         return result;
81     }
82
83     /**
84      * Writes the portion of the file header that refers to this instance.
85      *
86      * @param out {@code non-null;} where to write
87      */
88     public void writeHeaderPart(AnnotatedOutput out) {
89         throwIfNotPrepared();
90
91         int sz = classDefs.size();
92         int offset = (sz == 0) ? 0 : getFileOffset();
93
94         if (out.annotates()) {
95             out.annotate(4, "class_defs_size: " + Hex.u4(sz));
96             out.annotate(4, "class_defs_off:  " + Hex.u4(offset));
97         }
98
99         out.writeInt(sz);
100         out.writeInt(offset);
101     }
102
103     /**
104      * Adds an element to this instance. It is illegal to attempt to add more
105      * than one class with the same name.
106      *
107      * @param clazz {@code non-null;} the class def to add
108      */
109     public void add(ClassDefItem clazz) {
110         Type type;
111
112         try {
113             type = clazz.getThisClass().getClassType();
114         } catch (NullPointerException ex) {
115             // Elucidate the exception.
116             throw new NullPointerException("clazz == null");
117         }
118
119         throwIfPrepared();
120
121         if (classDefs.get(type) != null) {
122             throw new IllegalArgumentException("already added: " + type);
123         }
124
125         classDefs.put(type, clazz);
126     }
127
128     /** {@inheritDoc} */
129     @Override
130     protected void orderItems() {
131         int sz = classDefs.size();
132         int idx = 0;
133
134         orderedDefs = new ArrayList<ClassDefItem>(sz);
135
136         /*
137          * Iterate over all the classes, recursively assigning an
138          * index to each, implicitly skipping the ones that have
139          * already been assigned by the time this (top-level)
140          * iteration reaches them.
141          */
142         for (Type type : classDefs.keySet()) {
143             idx = orderItems0(type, idx, sz - idx);
144         }
145     }
146
147     /**
148      * Helper for {@link #orderItems}, which recursively assigns indices
149      * to classes.
150      *
151      * @param type {@code null-ok;} type ref to assign, if any
152      * @param idx {@code >= 0;} the next index to assign
153      * @param maxDepth maximum recursion depth; if negative, this will
154      * throw an exception indicating class definition circularity
155      * @return {@code >= 0;} the next index to assign
156      */
157     private int orderItems0(Type type, int idx, int maxDepth) {
158         ClassDefItem c = classDefs.get(type);
159
160         if ((c == null) || (c.hasIndex())) {
161             return idx;
162         }
163
164         if (maxDepth < 0) {
165             throw new RuntimeException("class circularity with " + type);
166         }
167
168         maxDepth--;
169
170         CstType superclassCst = c.getSuperclass();
171         if (superclassCst != null) {
172             Type superclass = superclassCst.getClassType();
173             idx = orderItems0(superclass, idx, maxDepth);
174         }
175
176         TypeList interfaces = c.getInterfaces();
177         int sz = interfaces.size();
178         for (int i = 0; i < sz; i++) {
179             idx = orderItems0(interfaces.getType(i), idx, maxDepth);
180         }
181
182         c.setIndex(idx);
183         orderedDefs.add(c);
184         return idx + 1;
185     }
186 }