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