1 /*
2  * Copyright (C) 2008 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  * Constant type to represent a reference to a particular constant
23  * value of an enumerated type.
24  */
25 public final class CstEnumRef extends CstMemberRef {
26     /** {@code null-ok;} the corresponding field ref, lazily initialized */
27     private CstFieldRef fieldRef;
28 
29     /**
30      * Constructs an instance.
31      *
32      * @param nat {@code non-null;} the name-and-type; the defining class is derived
33      * from this
34      */
CstEnumRef(CstNat nat)35     public CstEnumRef(CstNat nat) {
36         super(new CstType(nat.getFieldType()), nat);
37 
38         fieldRef = null;
39     }
40 
41     /** {@inheritDoc} */
42     @Override
typeName()43     public String typeName() {
44         return "enum";
45     }
46 
47     /**
48      * {@inheritDoc}
49      *
50      * <b>Note:</b> This returns the enumerated type.
51      */
52     @Override
getType()53     public Type getType() {
54         return getDefiningClass().getClassType();
55     }
56 
57     /**
58      * Get a {@link CstFieldRef} that corresponds with this instance.
59      *
60      * @return {@code non-null;} the corresponding field reference
61      */
getFieldRef()62     public CstFieldRef getFieldRef() {
63         if (fieldRef == null) {
64             fieldRef = new CstFieldRef(getDefiningClass(), getNat());
65         }
66 
67         return fieldRef;
68     }
69 }
70