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.rop.cst;
18
19import com.android.dx.rop.type.Type;
20
21/**
22 * Constants of type {@code CONSTANT_Fieldref_info}.
23 */
24public final class CstFieldRef extends CstMemberRef {
25    /**
26     * Returns an instance of this class that represents the static
27     * field which should hold the class corresponding to a given
28     * primitive type. For example, if given {@link Type#INT}, this
29     * method returns an instance corresponding to the field
30     * {@code java.lang.Integer.TYPE}.
31     *
32     * @param primitiveType {@code non-null;} the primitive type
33     * @return {@code non-null;} the corresponding static field
34     */
35    public static CstFieldRef forPrimitiveType(Type primitiveType) {
36        return new CstFieldRef(CstType.forBoxedPrimitiveType(primitiveType),
37                CstNat.PRIMITIVE_TYPE_NAT);
38    }
39
40    /**
41     * Constructs an instance.
42     *
43     * @param definingClass {@code non-null;} the type of the defining class
44     * @param nat {@code non-null;} the name-and-type
45     */
46    public CstFieldRef(CstType definingClass, CstNat nat) {
47        super(definingClass, nat);
48    }
49
50    /** {@inheritDoc} */
51    @Override
52    public String typeName() {
53        return "field";
54    }
55
56    /**
57     * Returns the type of this field.
58     *
59     * @return {@code non-null;} the field's type
60     */
61    public Type getType() {
62        return getNat().getFieldType();
63    }
64
65    /** {@inheritDoc} */
66    @Override
67    protected int compareTo0(Constant other) {
68        int cmp = super.compareTo0(other);
69
70        if (cmp != 0) {
71            return cmp;
72        }
73
74        CstFieldRef otherField = (CstFieldRef) other;
75        CstString thisDescriptor = getNat().getDescriptor();
76        CstString otherDescriptor = otherField.getNat().getDescriptor();
77        return thisDescriptor.compareTo(otherDescriptor);
78    }
79}
80