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.type.StdTypeList; 20import com.android.dx.rop.type.Type; 21import com.android.dx.rop.type.TypeList; 22import com.android.dx.util.AnnotatedOutput; 23import com.android.dx.util.Hex; 24 25/** 26 * Representation of a list of class references. 27 */ 28public final class TypeListItem extends OffsettedItem { 29 /** alignment requirement */ 30 private static final int ALIGNMENT = 4; 31 32 /** element size in bytes */ 33 private static final int ELEMENT_SIZE = 2; 34 35 /** header size in bytes */ 36 private static final int HEADER_SIZE = 4; 37 38 /** {@code non-null;} the actual list */ 39 private final TypeList list; 40 41 /** 42 * Constructs an instance. 43 * 44 * @param list {@code non-null;} the actual list 45 */ 46 public TypeListItem(TypeList list) { 47 super(ALIGNMENT, (list.size() * ELEMENT_SIZE) + HEADER_SIZE); 48 49 this.list = list; 50 } 51 52 /** {@inheritDoc} */ 53 @Override 54 public int hashCode() { 55 return StdTypeList.hashContents(list); 56 } 57 58 /** {@inheritDoc} */ 59 @Override 60 public ItemType itemType() { 61 return ItemType.TYPE_TYPE_LIST; 62 } 63 64 /** {@inheritDoc} */ 65 public void addContents(DexFile file) { 66 TypeIdsSection typeIds = file.getTypeIds(); 67 int sz = list.size(); 68 69 for (int i = 0; i < sz; i++) { 70 typeIds.intern(list.getType(i)); 71 } 72 } 73 74 /** {@inheritDoc} */ 75 @Override 76 public String toHuman() { 77 throw new RuntimeException("unsupported"); 78 } 79 80 /** 81 * Gets the underlying list. 82 * 83 * @return {@code non-null;} the list 84 */ 85 public TypeList getList() { 86 return list; 87 } 88 89 /** {@inheritDoc} */ 90 @Override 91 protected void writeTo0(DexFile file, AnnotatedOutput out) { 92 TypeIdsSection typeIds = file.getTypeIds(); 93 int sz = list.size(); 94 95 if (out.annotates()) { 96 out.annotate(0, offsetString() + " type_list"); 97 out.annotate(HEADER_SIZE, " size: " + Hex.u4(sz)); 98 for (int i = 0; i < sz; i++) { 99 Type one = list.getType(i); 100 int idx = typeIds.indexOf(one); 101 out.annotate(ELEMENT_SIZE, 102 " " + Hex.u2(idx) + " // " + one.toHuman()); 103 } 104 } 105 106 out.writeInt(sz); 107 108 for (int i = 0; i < sz; i++) { 109 out.writeShort(typeIds.indexOf(list.getType(i))); 110 } 111 } 112 113 /** {@inheritDoc} */ 114 @Override 115 protected int compareTo0(OffsettedItem other) { 116 TypeList thisList = this.list; 117 TypeList otherList = ((TypeListItem) other).list; 118 119 return StdTypeList.compareContents(thisList, otherList); 120 } 121} 122