1917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul/* 2917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * Copyright (C) 2007 The Android Open Source Project 3917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * 4917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * Licensed under the Apache License, Version 2.0 (the "License"); 5917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * you may not use this file except in compliance with the License. 6917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * You may obtain a copy of the License at 7917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * 8917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * http://www.apache.org/licenses/LICENSE-2.0 9917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * 10917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * Unless required by applicable law or agreed to in writing, software 11917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * distributed under the License is distributed on an "AS IS" BASIS, 12917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * See the License for the specific language governing permissions and 14917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * limitations under the License. 15917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul */ 16917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 17917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgulpackage com.android.dexgen.dex.file; 18917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 19917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgulimport com.android.dexgen.rop.cst.Constant; 20917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgulimport com.android.dexgen.util.AnnotatedOutput; 21917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 22917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgulimport java.util.Collection; 23917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 24917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul/** 25917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * A section of a {@code .dex} file which consists of a sequence of 26917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * {@link Item} objects. Each of the items must have the same size in 27917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * the output. 28917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul */ 29917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgulpublic abstract class UniformItemSection extends Section { 30917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul /** 31917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * Constructs an instance. The file offset is initially unknown. 32917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * 33917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * @param name {@code null-ok;} the name of this instance, for annotation 34917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * purposes 35917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * @param file {@code non-null;} file that this instance is part of 36917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * @param alignment {@code > 0;} alignment requirement for the final output; 37917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * must be a power of 2 38917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul */ 39917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul public UniformItemSection(String name, DexFile file, int alignment) { 40917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul super(name, file, alignment); 41917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul } 42917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 43917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul /** {@inheritDoc} */ 44917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul @Override 45917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul public final int writeSize() { 46917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul Collection<? extends Item> items = items(); 47917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul int sz = items.size(); 48917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 49917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul if (sz == 0) { 50917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul return 0; 51917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul } 52917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 53917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul // Since each item has to be the same size, we can pick any. 54917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul return sz * items.iterator().next().writeSize(); 55917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul } 56917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 57917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul /** 58917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * Gets the item corresponding to the given {@link Constant}. This 59917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * will throw an exception if the constant is not found, including 60917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * if this instance isn't the sort that maps constants to {@link 61917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * IndexedItem} instances. 62917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * 63917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * @param cst {@code non-null;} constant to look for 64917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * @return {@code non-null;} the corresponding item found in this instance 65917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul */ 66917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul public abstract IndexedItem get(Constant cst); 67917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 68917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul /** {@inheritDoc} */ 69917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul @Override 70917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul protected final void prepare0() { 71917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul DexFile file = getFile(); 72917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 73917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul orderItems(); 74917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 75917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul for (Item one : items()) { 76917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul one.addContents(file); 77917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul } 78917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul } 79917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 80917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul /** {@inheritDoc} */ 81917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul @Override 82917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul protected final void writeTo0(AnnotatedOutput out) { 83917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul DexFile file = getFile(); 84917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul int alignment = getAlignment(); 85917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 86917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul for (Item one : items()) { 87917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul one.writeTo(file, out); 88917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul out.alignTo(alignment); 89917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul } 90917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul } 91917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 92917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul /** {@inheritDoc} */ 93917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul @Override 94917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul public final int getAbsoluteItemOffset(Item item) { 95917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul /* 96917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * Since all items must be the same size, we can use the size 97917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * of the one we're given to calculate its offset. 98917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul */ 99917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul IndexedItem ii = (IndexedItem) item; 100917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul int relativeOffset = ii.getIndex() * ii.writeSize(); 101917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 102917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul return getAbsoluteOffset(relativeOffset); 103917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul } 104917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 105917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul /** 106917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * Alters or picks the order for items in this instance if desired, 107917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * so that subsequent calls to {@link #items} will yield a 108917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * so-ordered collection. If the items in this instance are indexed, 109917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * then this method should also assign indices. 110917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul */ 111917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul protected abstract void orderItems(); 112917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul} 113