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.rop.cst.CstType; 21917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgulimport com.android.dexgen.rop.type.Type; 22917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgulimport com.android.dexgen.rop.type.TypeList; 23917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgulimport com.android.dexgen.util.AnnotatedOutput; 24917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgulimport com.android.dexgen.util.Hex; 25917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 26917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgulimport java.util.ArrayList; 27917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgulimport java.util.Collection; 28917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgulimport java.util.TreeMap; 29917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 30917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul/** 31917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * Class definitions list section of a {@code .dex} file. 32917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul */ 33917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgulpublic final class ClassDefsSection extends UniformItemSection { 34917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul /** 35917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * {@code non-null;} map from type constants for classes to {@link 36917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * ClassDefItem} instances that define those classes 37917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul */ 38917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul private final TreeMap<Type, ClassDefItem> classDefs; 39917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 40917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul /** {@code null-ok;} ordered list of classes; set in {@link #orderItems} */ 41917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul private ArrayList<ClassDefItem> orderedDefs; 42917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 43917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul /** 44917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * Constructs an instance. The file offset is initially unknown. 45917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * 46917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * @param file {@code non-null;} file that this instance is part of 47917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul */ 48917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul public ClassDefsSection(DexFile file) { 49917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul super("class_defs", file, 4); 50917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 51917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul classDefs = new TreeMap<Type, ClassDefItem>(); 52917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul orderedDefs = null; 53917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul } 54917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 55917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul /** {@inheritDoc} */ 56917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul @Override 57917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul public Collection<? extends Item> items() { 58917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul if (orderedDefs != null) { 59917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul return orderedDefs; 60917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul } 61917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 62917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul return classDefs.values(); 63917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul } 64917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 65917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul /** {@inheritDoc} */ 66917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul @Override 67917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul public IndexedItem get(Constant cst) { 68917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul if (cst == null) { 69917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul throw new NullPointerException("cst == null"); 70917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul } 71917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 72917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul throwIfNotPrepared(); 73917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 74917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul Type type = ((CstType) cst).getClassType(); 75917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul IndexedItem result = classDefs.get(type); 76917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 77917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul if (result == null) { 78917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul throw new IllegalArgumentException("not found"); 79917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul } 80917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 81917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul return result; 82917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul } 83917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 84917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul /** 85917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * Writes the portion of the file header that refers to this instance. 86917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * 87917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * @param out {@code non-null;} where to write 88917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul */ 89917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul public void writeHeaderPart(AnnotatedOutput out) { 90917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul throwIfNotPrepared(); 91917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 92917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul int sz = classDefs.size(); 93917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul int offset = (sz == 0) ? 0 : getFileOffset(); 94917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 95917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul if (out.annotates()) { 96917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul out.annotate(4, "class_defs_size: " + Hex.u4(sz)); 97917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul out.annotate(4, "class_defs_off: " + Hex.u4(offset)); 98917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul } 99917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 100917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul out.writeInt(sz); 101917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul out.writeInt(offset); 102917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul } 103917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 104917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul /** 105917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * Adds an element to this instance. It is illegal to attempt to add more 106917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * than one class with the same name. 107917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * 108917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * @param clazz {@code non-null;} the class def to add 109917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul */ 110917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul public void add(ClassDefItem clazz) { 111917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul Type type; 112917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 113917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul try { 114917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul type = clazz.getThisClass().getClassType(); 115917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul } catch (NullPointerException ex) { 116917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul // Elucidate the exception. 117917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul throw new NullPointerException("clazz == null"); 118917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul } 119917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 120917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul throwIfPrepared(); 121917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 122917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul if (classDefs.get(type) != null) { 123917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul throw new IllegalArgumentException("already added: " + type); 124917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul } 125917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 126917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul classDefs.put(type, clazz); 127917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul } 128917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 129917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul /** {@inheritDoc} */ 130917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul @Override 131917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul protected void orderItems() { 132917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul int sz = classDefs.size(); 133917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul int idx = 0; 134917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 135917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul orderedDefs = new ArrayList<ClassDefItem>(sz); 136917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 137917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul /* 138917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * Iterate over all the classes, recursively assigning an 139917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * index to each, implicitly skipping the ones that have 140917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * already been assigned by the time this (top-level) 141917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * iteration reaches them. 142917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul */ 143917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul for (Type type : classDefs.keySet()) { 144917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul idx = orderItems0(type, idx, sz - idx); 145917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul } 146917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul } 147917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 148917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul /** 149917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * Helper for {@link #orderItems}, which recursively assigns indices 150917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * to classes. 151917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * 152917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * @param type {@code null-ok;} type ref to assign, if any 153917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * @param idx {@code >= 0;} the next index to assign 154917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * @param maxDepth maximum recursion depth; if negative, this will 155917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * throw an exception indicating class definition circularity 156917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * @return {@code >= 0;} the next index to assign 157917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul */ 158917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul private int orderItems0(Type type, int idx, int maxDepth) { 159917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul ClassDefItem c = classDefs.get(type); 160917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 161917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul if ((c == null) || (c.hasIndex())) { 162917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul return idx; 163917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul } 164917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 165917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul if (maxDepth < 0) { 166917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul throw new RuntimeException("class circularity with " + type); 167917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul } 168917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 169917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul maxDepth--; 170917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 171917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul CstType superclassCst = c.getSuperclass(); 172917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul if (superclassCst != null) { 173917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul Type superclass = superclassCst.getClassType(); 174917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul idx = orderItems0(superclass, idx, maxDepth); 175917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul } 176917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 177917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul TypeList interfaces = c.getInterfaces(); 178917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul int sz = interfaces.size(); 179917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul for (int i = 0; i < sz; i++) { 180917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul idx = orderItems0(interfaces.getType(i), idx, maxDepth); 181917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul } 182917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 183917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul c.setIndex(idx); 184917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul orderedDefs.add(c); 185917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul return idx + 1; 186917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul } 187917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul} 188