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.util.AnnotatedOutput; 20917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgulimport com.android.dexgen.util.Hex; 21917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 22917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgulimport java.util.HashMap; 23917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgulimport java.util.List; 24917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 25917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul/** 26917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * Class that represents a contiguous list of uniform items. Each 27917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * item in the list, in particular, must have the same write size and 28917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * alignment. 29917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * 30917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * <p>This class inherits its alignment from its items, bumped up to 31917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * {@code 4} if the items have a looser alignment requirement. If 32917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * it is more than {@code 4}, then there will be a gap after the 33917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * output list size (which is four bytes) and before the first item.</p> 34917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * 35917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * @param <T> type of element contained in an instance 36917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul */ 37917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgulpublic final class UniformListItem<T extends OffsettedItem> 38917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul extends OffsettedItem { 39917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul /** the size of the list header */ 40917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul private static final int HEADER_SIZE = 4; 41917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 42917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul /** {@code non-null;} the item type */ 43917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul private final ItemType itemType; 44917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 45917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul /** {@code non-null;} the contents */ 46917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul private final List<T> items; 47917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 48917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul /** 49917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * Constructs an instance. It is illegal to modify the given list once 50917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * it is used to construct an instance of this class. 51917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * 52917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * @param itemType {@code non-null;} the type of the item 53917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * @param items {@code non-null and non-empty;} list of items to represent 54917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul */ 55917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul public UniformListItem(ItemType itemType, List<T> items) { 56917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul super(getAlignment(items), writeSize(items)); 57917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 58917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul if (itemType == null) { 59917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul throw new NullPointerException("itemType == null"); 60917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul } 61917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 62917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul this.items = items; 63917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul this.itemType = itemType; 64917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul } 65917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 66917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul /** 67917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * Helper for {@link #UniformListItem}, which returns the alignment 68917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * requirement implied by the given list. See the header comment for 69917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * more details. 70917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * 71917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * @param items {@code non-null;} list of items being represented 72917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * @return {@code >= 4;} the alignment requirement 73917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul */ 74917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul private static int getAlignment(List<? extends OffsettedItem> items) { 75917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul try { 76917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul // Since they all must have the same alignment, any one will do. 77917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul return Math.max(HEADER_SIZE, items.get(0).getAlignment()); 78917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul } catch (IndexOutOfBoundsException ex) { 79917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul // Translate the exception. 80917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul throw new IllegalArgumentException("items.size() == 0"); 81917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul } catch (NullPointerException ex) { 82917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul // Translate the exception. 83917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul throw new NullPointerException("items == null"); 84917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul } 85917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul } 86917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 87917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul /** 88917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * Calculates the write size for the given list. 89917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * 90917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * @param items {@code non-null;} the list in question 91917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * @return {@code >= 0;} the write size 92917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul */ 93917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul private static int writeSize(List<? extends OffsettedItem> items) { 94917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul /* 95917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * This class assumes all included items are the same size, 96917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * an assumption which is verified in place0(). 97917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul */ 98917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul OffsettedItem first = items.get(0); 99917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul return (items.size() * first.writeSize()) + getAlignment(items); 100917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul } 101917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 102917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul /** {@inheritDoc} */ 103917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul @Override 104917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul public ItemType itemType() { 105917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul return itemType; 106917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul } 107917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 108917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul /** {@inheritDoc} */ 109917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul @Override 110917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul public String toString() { 111917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul StringBuffer sb = new StringBuffer(100); 112917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 113917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul sb.append(getClass().getName()); 114917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul sb.append(items); 115917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 116917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul return sb.toString(); 117917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul } 118917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 119917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul /** {@inheritDoc} */ 120917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul @Override 121917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul public void addContents(DexFile file) { 122917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul for (OffsettedItem i : items) { 123917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul i.addContents(file); 124917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul } 125917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul } 126917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 127917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul /** {@inheritDoc} */ 128917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul @Override 129917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul public final String toHuman() { 130917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul StringBuffer sb = new StringBuffer(100); 131917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul boolean first = true; 132917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 133917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul sb.append("{"); 134917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 135917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul for (OffsettedItem i : items) { 136917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul if (first) { 137917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul first = false; 138917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul } else { 139917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul sb.append(", "); 140917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul } 141917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul sb.append(i.toHuman()); 142917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul } 143917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 144917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul sb.append("}"); 145917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul return sb.toString(); 146917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul } 147917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 148917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul /** 149917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * Gets the underlying list of items. 150917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * 151917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * @return {@code non-null;} the list 152917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul */ 153917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul public final List<T> getItems() { 154917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul return items; 155917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul } 156917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 157917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul /** {@inheritDoc} */ 158917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul @Override 159917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul protected void place0(Section addedTo, int offset) { 160917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul offset += headerSize(); 161917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 162917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul boolean first = true; 163917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul int theSize = -1; 164917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul int theAlignment = -1; 165917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 166917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul for (OffsettedItem i : items) { 167917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul int size = i.writeSize(); 168917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul if (first) { 169917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul theSize = size; 170917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul theAlignment = i.getAlignment(); 171917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul first = false; 172917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul } else { 173917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul if (size != theSize) { 174917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul throw new UnsupportedOperationException( 175917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul "item size mismatch"); 176917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul } 177917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul if (i.getAlignment() != theAlignment) { 178917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul throw new UnsupportedOperationException( 179917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul "item alignment mismatch"); 180917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul } 181917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul } 182917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 183917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul offset = i.place(addedTo, offset) + size; 184917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul } 185917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul } 186917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 187917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul /** {@inheritDoc} */ 188917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul @Override 189917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul protected void writeTo0(DexFile file, AnnotatedOutput out) { 190917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul int size = items.size(); 191917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 192917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul if (out.annotates()) { 193917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul out.annotate(0, offsetString() + " " + typeName()); 194917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul out.annotate(4, " size: " + Hex.u4(size)); 195917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul } 196917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 197917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul out.writeInt(size); 198917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 199917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul for (OffsettedItem i : items) { 200917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul i.writeTo(file, out); 201917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul } 202917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul } 203917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 204917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul /** 205917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * Get the size of the header of this list. 206917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * 207917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * @return {@code >= 0;} the header size 208917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul */ 209917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul private int headerSize() { 210917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul /* 211917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * Because of how this instance was set up, this is the same 212917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * as the alignment. 213917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul */ 214917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul return getAlignment(); 215917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul } 216917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul} 217