2 * Copyright (C) 2007 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.dx.dex.file;
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;
30 * Class definitions list section of a {@code .dex} file.
32 public final class ClassDefsSection extends UniformItemSection {
34 * {@code non-null;} map from type constants for classes to {@link
35 * ClassDefItem} instances that define those classes
37 private final TreeMap<Type, ClassDefItem> classDefs;
39 /** {@code null-ok;} ordered list of classes; set in {@link #orderItems} */
40 private ArrayList<ClassDefItem> orderedDefs;
43 * Constructs an instance. The file offset is initially unknown.
45 * @param file {@code non-null;} file that this instance is part of
47 public ClassDefsSection(DexFile file) {
48 super("class_defs", file, 4);
50 classDefs = new TreeMap<Type, ClassDefItem>();
56 public Collection<? extends Item> items() {
57 if (orderedDefs != null) {
61 return classDefs.values();
66 public IndexedItem get(Constant cst) {
68 throw new NullPointerException("cst == null");
73 Type type = ((CstType) cst).getClassType();
74 IndexedItem result = classDefs.get(type);
77 throw new IllegalArgumentException("not found");
84 * Writes the portion of the file header that refers to this instance.
86 * @param out {@code non-null;} where to write
88 public void writeHeaderPart(AnnotatedOutput out) {
91 int sz = classDefs.size();
92 int offset = (sz == 0) ? 0 : getFileOffset();
94 if (out.annotates()) {
95 out.annotate(4, "class_defs_size: " + Hex.u4(sz));
96 out.annotate(4, "class_defs_off: " + Hex.u4(offset));
100 out.writeInt(offset);
104 * Adds an element to this instance. It is illegal to attempt to add more
105 * than one class with the same name.
107 * @param clazz {@code non-null;} the class def to add
109 public void add(ClassDefItem clazz) {
113 type = clazz.getThisClass().getClassType();
114 } catch (NullPointerException ex) {
115 // Elucidate the exception.
116 throw new NullPointerException("clazz == null");
121 if (classDefs.get(type) != null) {
122 throw new IllegalArgumentException("already added: " + type);
125 classDefs.put(type, clazz);
130 protected void orderItems() {
131 int sz = classDefs.size();
134 orderedDefs = new ArrayList<ClassDefItem>(sz);
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.
142 for (Type type : classDefs.keySet()) {
143 idx = orderItems0(type, idx, sz - idx);
148 * Helper for {@link #orderItems}, which recursively assigns indices
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
157 private int orderItems0(Type type, int idx, int maxDepth) {
158 ClassDefItem c = classDefs.get(type);
160 if ((c == null) || (c.hasIndex())) {
165 throw new RuntimeException("class circularity with " + type);
170 CstType superclassCst = c.getSuperclass();
171 if (superclassCst != null) {
172 Type superclass = superclassCst.getClassType();
173 idx = orderItems0(superclass, idx, maxDepth);
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);