OSDN Git Service

e653661564e9baed1656a686cabe88cfb36dee98
[android-x86/dalvik.git] / dx / src / com / android / dx / io / MethodId.java
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.dx.io;
18
19 import com.android.dx.util.Unsigned;
20
21 public final class MethodId implements Comparable<MethodId> {
22     private final DexBuffer buffer;
23     private final int declaringClassIndex;
24     private final int protoIndex;
25     private final int nameIndex;
26
27     public MethodId(DexBuffer buffer, int declaringClassIndex, int protoIndex, int nameIndex) {
28         this.buffer = buffer;
29         this.declaringClassIndex = declaringClassIndex;
30         this.protoIndex = protoIndex;
31         this.nameIndex = nameIndex;
32     }
33
34     public int getDeclaringClassIndex() {
35         return declaringClassIndex;
36     }
37
38     public int getProtoIndex() {
39         return protoIndex;
40     }
41
42     public int getNameIndex() {
43         return nameIndex;
44     }
45
46     public int compareTo(MethodId other) {
47         if (declaringClassIndex != other.declaringClassIndex) {
48             return Unsigned.compare(declaringClassIndex, other.declaringClassIndex);
49         }
50         if (nameIndex != other.nameIndex) {
51             return Unsigned.compare(nameIndex, other.nameIndex);
52         }
53         return Unsigned.compare(protoIndex, other.protoIndex);
54     }
55
56     public void writeTo(DexBuffer.Section out) {
57         out.writeUnsignedShort(declaringClassIndex);
58         out.writeUnsignedShort(protoIndex);
59         out.writeInt(nameIndex);
60     }
61
62     @Override public String toString() {
63         if (buffer == null) {
64             return declaringClassIndex + " " + protoIndex + " " + nameIndex;
65         }
66         return buffer.typeNames().get(declaringClassIndex)
67                 + "." + buffer.strings().get(nameIndex)
68                 + buffer.readTypeList(buffer.protoIds().get(protoIndex).getParametersOffset());
69     }
70 }