1/* 2 * Copyright (C) 2008 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.type.Prototype; 21import com.android.dx.util.AnnotatedOutput; 22import com.android.dx.util.Hex; 23import java.util.Collection; 24import java.util.TreeMap; 25 26/** 27 * Proto (method prototype) identifiers list section of a 28 * {@code .dex} file. 29 */ 30public final class ProtoIdsSection extends UniformItemSection { 31 /** 32 * {@code non-null;} map from method prototypes to {@link ProtoIdItem} instances 33 */ 34 private final TreeMap<Prototype, ProtoIdItem> protoIds; 35 36 /** 37 * Constructs an instance. The file offset is initially unknown. 38 * 39 * @param file {@code non-null;} file that this instance is part of 40 */ 41 public ProtoIdsSection(DexFile file) { 42 super("proto_ids", file, 4); 43 44 protoIds = new TreeMap<Prototype, ProtoIdItem>(); 45 } 46 47 /** {@inheritDoc} */ 48 @Override 49 public Collection<? extends Item> items() { 50 return protoIds.values(); 51 } 52 53 /** {@inheritDoc} */ 54 @Override 55 public IndexedItem get(Constant cst) { 56 throw new UnsupportedOperationException("unsupported"); 57 } 58 59 /** 60 * Writes the portion of the file header that refers to this instance. 61 * 62 * @param out {@code non-null;} where to write 63 */ 64 public void writeHeaderPart(AnnotatedOutput out) { 65 throwIfNotPrepared(); 66 67 int sz = protoIds.size(); 68 int offset = (sz == 0) ? 0 : getFileOffset(); 69 70 if (sz > 65536) { 71 throw new UnsupportedOperationException("too many proto ids"); 72 } 73 74 if (out.annotates()) { 75 out.annotate(4, "proto_ids_size: " + Hex.u4(sz)); 76 out.annotate(4, "proto_ids_off: " + Hex.u4(offset)); 77 } 78 79 out.writeInt(sz); 80 out.writeInt(offset); 81 } 82 83 /** 84 * Interns an element into this instance. 85 * 86 * @param prototype {@code non-null;} the prototype to intern 87 * @return {@code non-null;} the interned reference 88 */ 89 public ProtoIdItem intern(Prototype prototype) { 90 if (prototype == null) { 91 throw new NullPointerException("prototype == null"); 92 } 93 94 throwIfPrepared(); 95 96 ProtoIdItem result = protoIds.get(prototype); 97 98 if (result == null) { 99 result = new ProtoIdItem(prototype); 100 protoIds.put(prototype, result); 101 } 102 103 return result; 104 } 105 106 /** 107 * Gets the index of the given prototype, which must have 108 * been added to this instance. 109 * 110 * @param prototype {@code non-null;} the prototype to look up 111 * @return {@code >= 0;} the reference's index 112 */ 113 public int indexOf(Prototype prototype) { 114 if (prototype == null) { 115 throw new NullPointerException("prototype == null"); 116 } 117 118 throwIfNotPrepared(); 119 120 ProtoIdItem item = protoIds.get(prototype); 121 122 if (item == null) { 123 throw new IllegalArgumentException("not found"); 124 } 125 126 return item.getIndex(); 127 } 128 129 /** {@inheritDoc} */ 130 @Override 131 protected void orderItems() { 132 int idx = 0; 133 134 for (Object i : items()) { 135 ((ProtoIdItem) i).setIndex(idx); 136 idx++; 137 } 138 } 139} 140