Home | History | Annotate | Download | only in cst
      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 
     17 package com.android.dx.rop.cst;
     18 
     19 import com.android.dx.rop.type.Type;
     20 
     21 /**
     22  * Constants of type {@code CONSTANT_Fieldref_info}.
     23  */
     24 public 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     @Override
     62     public Type getType() {
     63         return getNat().getFieldType();
     64     }
     65 
     66     /** {@inheritDoc} */
     67     @Override
     68     protected int compareTo0(Constant other) {
     69         int cmp = super.compareTo0(other);
     70 
     71         if (cmp != 0) {
     72             return cmp;
     73         }
     74 
     75         CstFieldRef otherField = (CstFieldRef) other;
     76         CstString thisDescriptor = getNat().getDescriptor();
     77         CstString otherDescriptor = otherField.getNat().getDescriptor();
     78         return thisDescriptor.compareTo(otherDescriptor);
     79     }
     80 }
     81