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