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