1 /*
2  * Copyright (C) 2011 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.dex;
18 
19 import com.android.dex.util.Unsigned;
20 
21 public final class FieldId implements Comparable<FieldId> {
22     private final Dex dex;
23     private final int declaringClassIndex;
24     private final int typeIndex;
25     private final int nameIndex;
26 
FieldId(Dex dex, int declaringClassIndex, int typeIndex, int nameIndex)27     public FieldId(Dex dex, int declaringClassIndex, int typeIndex, int nameIndex) {
28         this.dex = dex;
29         this.declaringClassIndex = declaringClassIndex;
30         this.typeIndex = typeIndex;
31         this.nameIndex = nameIndex;
32     }
33 
getDeclaringClassIndex()34     public int getDeclaringClassIndex() {
35         return declaringClassIndex;
36     }
37 
getTypeIndex()38     public int getTypeIndex() {
39         return typeIndex;
40     }
41 
getNameIndex()42     public int getNameIndex() {
43         return nameIndex;
44     }
45 
46     @Override
compareTo(FieldId other)47     public int compareTo(FieldId other) {
48         if (declaringClassIndex != other.declaringClassIndex) {
49             return Unsigned.compare(declaringClassIndex, other.declaringClassIndex);
50         }
51         if (nameIndex != other.nameIndex) {
52             return Unsigned.compare(nameIndex, other.nameIndex);
53         }
54         return Unsigned.compare(typeIndex, other.typeIndex); // should always be 0
55     }
56 
writeTo(Dex.Section out)57     public void writeTo(Dex.Section out) {
58         out.writeUnsignedShort(declaringClassIndex);
59         out.writeUnsignedShort(typeIndex);
60         out.writeInt(nameIndex);
61     }
62 
63     @Override
toString()64     public String toString() {
65         if (dex == null) {
66             return declaringClassIndex + " " + typeIndex + " " + nameIndex;
67         }
68         return dex.typeNames().get(typeIndex) + "." + dex.strings().get(nameIndex);
69     }
70 }
71