/
MethodKey.java
86 lines (80 loc) · 2.64 KB
/
MethodKey.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*******************************************************************************
* Copyright (c) 2008, 2010 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.pde.api.tools.internal.model;
/**
* A key for a method - name & signature based.
*
* @since 1.1
*/
public class MethodKey {
private String fSelector;
private String fSig;
private String fTypename;
private boolean fConsiderTypename = false;
/**
* Constructs a new method key
*
* @param typename the name (fully qualified or otherwise) of the type the
* method is from
* @param name method name the name of the method
* @param sig method signature the signature of the method or
* <code>null</code>
* @param considertypename if the given type name should be used when
* computing equality and hash codes
*/
public MethodKey(String typename, String name, String sig, boolean considertypename) {
fTypename = typename;
fSelector = name;
fSig = sig;
fConsiderTypename = considertypename;
}
@Override
public boolean equals(Object obj) {
if (obj instanceof MethodKey key) {
return fSelector.equals(key.fSelector) && signaturesEqual(fSig, key.fSig) && (fConsiderTypename ? fTypename.equals(key.fTypename) : true);
}
return false;
}
@Override
public int hashCode() {
return (fConsiderTypename ? fTypename.hashCode() : 0) + fSelector.hashCode() + (fSig == null ? 0 : fSig.hashCode());
}
/**
* Returns if the given signatures are equal. Signatures are considered
* equal iff:
* <ul>
* <li>both are equal</li>
* <li>both are <code>null</code></li>
* </ul>
*
* @param sig1
* @param sig2
* @return <code>true</code> if the signatures are equal <code>false</code>
* otherwise
*/
boolean signaturesEqual(String sig1, String sig2) {
if (sig1 != null) {
return sig1.equals(sig2);
}
return sig2 == null;
}
@Override
public String toString() {
StringBuilder buf = new StringBuilder();
buf.append("Method Key: [enclosing type - ").append(fTypename).append("] "); //$NON-NLS-1$ //$NON-NLS-2$
buf.append("[method name - ").append(fSelector).append("] "); //$NON-NLS-1$ //$NON-NLS-2$
buf.append("[signature - ").append(fSig).append("] "); //$NON-NLS-1$ //$NON-NLS-2$
return buf.toString();
}
}