001/*
002 * Copyright (C) 2009 The Android Open Source Project
003 * Copyright (C) 2015-2017 Keepsafe Software
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018package com.android.dexdeps;
019
020import java.util.Arrays;
021
022public class MethodRef implements HasDeclaringClass {
023    private String mDeclClass, mReturnType, mMethodName;
024    private String[] mArgTypes;
025
026    /**
027     * Initializes a new field reference.
028     */
029    public MethodRef(String declClass, String[] argTypes, String returnType,
030            String methodName) {
031        mDeclClass = declClass;
032        mArgTypes = argTypes;
033        mReturnType = returnType;
034        mMethodName = methodName;
035    }
036
037    /**
038     * Gets the name of the method's declaring class.
039     */
040    @Override
041    public String getDeclClassName() {
042        return mDeclClass;
043    }
044
045    /**
046     * Gets the method's descriptor.
047     */
048    public String getDescriptor() {
049        return descriptorFromProtoArray(mArgTypes, mReturnType);
050    }
051
052    /**
053     * Gets the method's name.
054     */
055    public String getName() {
056        return mMethodName;
057    }
058
059    /**
060     * Gets an array of method argument types.
061     */
062    public String[] getArgumentTypeNames() {
063        return mArgTypes;
064    }
065
066    /**
067     * Gets the method's return type.  Examples: "Ljava/lang/String;", "[I".
068     */
069    public String getReturnTypeName() {
070        return mReturnType;
071    }
072
073    /**
074     * Returns the method descriptor, given the argument and return type
075     * prototype strings.
076     */
077    private static String descriptorFromProtoArray(String[] protos,
078            String returnType) {
079        StringBuilder builder = new StringBuilder();
080
081        builder.append("(");
082        for (int i = 0; i < protos.length; i++) {
083            builder.append(protos[i]);
084        }
085
086        builder.append(")");
087        builder.append(returnType);
088
089        return builder.toString();
090    }
091
092    /*
093     * BEGIN MODIFICATION
094     */
095
096    @Override public boolean equals(Object o) {
097        if (!(o instanceof MethodRef)) {
098            return false;
099        }
100        MethodRef other = (MethodRef) o;
101        return other.mDeclClass.equals(mDeclClass) &&
102            other.mReturnType.equals(mReturnType) &&
103            other.mMethodName.equals(mMethodName) &&
104            Arrays.equals(other.mArgTypes, mArgTypes);
105    }
106
107    @Override public int hashCode() {
108        return mDeclClass.hashCode() ^ mReturnType.hashCode() ^
109            mMethodName.hashCode() ^ Arrays.hashCode(mArgTypes);
110    }
111
112    /*
113     * END MODIFICATION
114     */
115}