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.util.AnnotatedOutput;
21import java.util.Collection;
22
23/**
24 * A section of a {@code .dex} file which consists of a sequence of
25 * {@link Item} objects. Each of the items must have the same size in
26 * the output.
27 */
28public abstract class UniformItemSection extends Section {
29    /**
30     * Constructs an instance. The file offset is initially unknown.
31     *
32     * @param name {@code null-ok;} the name of this instance, for annotation
33     * purposes
34     * @param file {@code non-null;} file that this instance is part of
35     * @param alignment {@code > 0;} alignment requirement for the final output;
36     * must be a power of 2
37     */
38    public UniformItemSection(String name, DexFile file, int alignment) {
39        super(name, file, alignment);
40    }
41
42    /** {@inheritDoc} */
43    @Override
44    public final int writeSize() {
45        Collection<? extends Item> items = items();
46        int sz = items.size();
47
48        if (sz == 0) {
49            return 0;
50        }
51
52        // Since each item has to be the same size, we can pick any.
53        return sz * items.iterator().next().writeSize();
54    }
55
56    /**
57     * Gets the item corresponding to the given {@link Constant}. This
58     * will throw an exception if the constant is not found, including
59     * if this instance isn't the sort that maps constants to {@link
60     * IndexedItem} instances.
61     *
62     * @param cst {@code non-null;} constant to look for
63     * @return {@code non-null;} the corresponding item found in this instance
64     */
65    public abstract IndexedItem get(Constant cst);
66
67    /** {@inheritDoc} */
68    @Override
69    protected final void prepare0() {
70        DexFile file = getFile();
71
72        orderItems();
73
74        for (Item one : items()) {
75            one.addContents(file);
76        }
77    }
78
79    /** {@inheritDoc} */
80    @Override
81    protected final void writeTo0(AnnotatedOutput out) {
82        DexFile file = getFile();
83        int alignment = getAlignment();
84
85        for (Item one : items()) {
86            one.writeTo(file, out);
87            out.alignTo(alignment);
88        }
89    }
90
91    /** {@inheritDoc} */
92    @Override
93    public final int getAbsoluteItemOffset(Item item) {
94        /*
95         * Since all items must be the same size, we can use the size
96         * of the one we're given to calculate its offset.
97         */
98        IndexedItem ii = (IndexedItem) item;
99        int relativeOffset = ii.getIndex() * ii.writeSize();
100
101        return getAbsoluteOffset(relativeOffset);
102    }
103
104    /**
105     * Alters or picks the order for items in this instance if desired,
106     * so that subsequent calls to {@link #items} will yield a
107     * so-ordered collection. If the items in this instance are indexed,
108     * then this method should also assign indices.
109     */
110    protected abstract void orderItems();
111}
112