• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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;
18 
19 import com.android.dx.rop.cst.CstMethodRef;
20 import com.android.dx.rop.cst.CstNat;
21 import com.android.dx.rop.cst.CstString;
22 import com.android.dx.rop.type.Prototype;
23 import java.util.List;
24 
25 /**
26  * Identifies a method or constructor.
27  *
28  * @param <D> the type declaring this field
29  * @param <R> the return type of this method
30  */
31 public final class MethodId<D, R> {
32     final TypeId<D> declaringType;
33     final TypeId<R> returnType;
34     final String name;
35     final TypeList parameters;
36 
37     /** cached converted state */
38     final CstNat nat;
39     final CstMethodRef constant;
40 
MethodId(TypeId<D> declaringType, TypeId<R> returnType, String name, TypeList parameters)41     MethodId(TypeId<D> declaringType, TypeId<R> returnType, String name, TypeList parameters) {
42         if (declaringType == null || returnType == null || name == null || parameters == null) {
43             throw new NullPointerException();
44         }
45         this.declaringType = declaringType;
46         this.returnType = returnType;
47         this.name = name;
48         this.parameters = parameters;
49         this.nat = new CstNat(new CstString(name), new CstString(descriptor(false)));
50         this.constant = new CstMethodRef(declaringType.constant, nat);
51     }
52 
getDeclaringType()53     public TypeId<D> getDeclaringType() {
54         return declaringType;
55     }
56 
getReturnType()57     public TypeId<R> getReturnType() {
58         return returnType;
59     }
60 
61     /**
62      * Returns true if this method is a constructor for its declaring class.
63      */
isConstructor()64     public boolean isConstructor() {
65         return name.equals("<init>");
66     }
67 
68     /**
69      * Returns the method's name. This is "&lt;init&gt;" if this is a constructor.
70      */
getName()71     public String getName() {
72         return name;
73     }
74 
getParameters()75     public List<TypeId<?>> getParameters() {
76         return parameters.asList();
77     }
78 
79     /**
80      * Returns a descriptor like "(Ljava/lang/Class;[I)Ljava/lang/Object;".
81      */
descriptor(boolean includeThis)82     String descriptor(boolean includeThis) {
83         StringBuilder result = new StringBuilder();
84         result.append("(");
85         if (includeThis) {
86             result.append(declaringType.name);
87         }
88         for (TypeId t : parameters.types) {
89             result.append(t.name);
90         }
91         result.append(")");
92         result.append(returnType.name);
93         return result.toString();
94     }
95 
prototype(boolean includeThis)96     Prototype prototype(boolean includeThis) {
97         return Prototype.intern(descriptor(includeThis));
98     }
99 
100     @Override
equals(Object o)101     public boolean equals(Object o) {
102         return o instanceof MethodId
103                 && ((MethodId<?, ?>) o).declaringType.equals(declaringType)
104                 && ((MethodId<?, ?>) o).name.equals(name)
105                 && ((MethodId<?, ?>) o).parameters.equals(parameters)
106                 && ((MethodId<?, ?>) o).returnType.equals(returnType);
107     }
108 
109     @Override
hashCode()110     public int hashCode() {
111         int result = 17;
112         result = 31 * result + declaringType.hashCode();
113         result = 31 * result + name.hashCode();
114         result = 31 * result + parameters.hashCode();
115         result = 31 * result + returnType.hashCode();
116         return result;
117     }
118 
119     @Override
toString()120     public String toString() {
121         return declaringType + "." + name + "(" + parameters + ")";
122     }
123 }
124