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.dex.SizeOf;
20import com.android.dx.rop.cst.CstType;
21import com.android.dx.rop.cst.CstString;
22import com.android.dx.util.AnnotatedOutput;
23import com.android.dx.util.Hex;
24
25/**
26 * Representation of a type reference inside a Dalvik file.
27 */
28public final class TypeIdItem extends IdItem {
29    /**
30     * Constructs an instance.
31     *
32     * @param type {@code non-null;} the constant for the type
33     */
34    public TypeIdItem(CstType type) {
35        super(type);
36    }
37
38    /** {@inheritDoc} */
39    @Override
40    public ItemType itemType() {
41        return ItemType.TYPE_TYPE_ID_ITEM;
42    }
43
44    /** {@inheritDoc} */
45    @Override
46    public int writeSize() {
47        return SizeOf.TYPE_ID_ITEM;
48    }
49
50    /** {@inheritDoc} */
51    @Override
52    public void addContents(DexFile file) {
53        file.getStringIds().intern(getDefiningClass().getDescriptor());
54    }
55
56    /** {@inheritDoc} */
57    @Override
58    public void writeTo(DexFile file, AnnotatedOutput out) {
59        CstType type = getDefiningClass();
60        CstString descriptor = type.getDescriptor();
61        int idx = file.getStringIds().indexOf(descriptor);
62
63        if (out.annotates()) {
64            out.annotate(0, indexString() + ' ' + descriptor.toHuman());
65            out.annotate(4, "  descriptor_idx: " + Hex.u4(idx));
66        }
67
68        out.writeInt(idx);
69    }
70}
71