-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
MethodDescription.java
129 lines (111 loc) · 4.27 KB
/
MethodDescription.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package me.nallar.tickthreading.mappings;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import me.nallar.tickthreading.Log;
import me.nallar.tickthreading.util.ListUtil;
public class MethodDescription {
public final String clazz;
public final ClassDescription classDescription;
public final String returnType;
public final String parameters;
public final String name;
private Integer cachedHashCode = null;
public MethodDescription(String clazz, String name, String returnType, String parameters) {
this.clazz = clazz;
this.classDescription = new ClassDescription(clazz);
this.returnType = returnType;
this.parameters = parameters;
this.name = name;
}
public MethodDescription(Method method) {
this(method.getDeclaringClass().getCanonicalName(), method.getName(), getJVMName(method.getReturnType()), getParameterList(method));
}
public MethodDescription(String clazz, String name, String MCPDescription) {
//MCP style - (Lxv;IIILanw;Ljava/util/List;Llq;)V
this(clazz, name, MCPDescription.substring(MCPDescription.lastIndexOf(')') + 1), MCPDescription.substring(1, MCPDescription.indexOf(')')));
}
public String getShortName() {
return clazz + "/" + name;
}
public String getMCPName() {
return "(" + parameters + ")" + returnType;
}
@Override
public String toString() {
return name + getMCPName();
}
@Override
public int hashCode() {
if (cachedHashCode != null) {
return cachedHashCode;
}
int hashCode = returnType.hashCode();
hashCode = 31 * hashCode + parameters.hashCode();
hashCode = 31 * hashCode + name.hashCode();
hashCode = 31 * hashCode + clazz.hashCode();
return (cachedHashCode = hashCode);
}
@Override
public boolean equals(Object other) {
return this == other || (other instanceof MethodDescription && other.hashCode() == this.hashCode()) || (other instanceof Method && new MethodDescription((Method) other).equals(this));
}
public boolean similar(Object other) {
return this == other || (other instanceof MethodDescription && ((MethodDescription) other).getShortName() == this.getShortName()) || (other instanceof Method && new MethodDescription((Method) other).similar(this));
}
private static String getJVMName(Class<?> clazz) {
if (clazz.isPrimitive()) {
if (clazz.equals(Boolean.TYPE)) {
return "Z";
} else if (clazz.equals(Short.TYPE)) {
return "S";
} else if (clazz.equals(Long.TYPE)) {
return "J";
} else if (clazz.equals(Integer.TYPE)) {
return "I";
} else if (clazz.equals(Float.TYPE)) {
return "F";
} else if (clazz.equals(Double.TYPE)) {
return "D";
} else if (clazz.equals(Character.TYPE)) {
return "C";
}
}
return "L" + clazz.getCanonicalName() + ";";
}
private static String getParameterList(Method method) {
List<Class<?>> parameterClasses = new ArrayList<Class<?>>(Arrays.asList(method.getParameterTypes()));
StringBuilder parameters = new StringBuilder();
for (Class<?> clazz : parameterClasses) {
parameters.append(getJVMName(clazz));
}
return parameters.toString();
}
public static MethodDescription fromString(String clazz, String methodString) {
try {
String methodName = methodString.substring(0, methodString.indexOf('('));
return new MethodDescription(clazz, methodName, methodString.substring(methodString.indexOf('(')));
} catch (Exception ignored) {
}
return new MethodDescription(clazz, methodString, "", "");
}
public static List<MethodDescription> fromListString(String clazz, String methodList) {
ArrayList<MethodDescription> methodDescriptions = new ArrayList<MethodDescription>();
for (String methodString : ListUtil.split(methodList)) {
methodDescriptions.add(fromString(clazz, methodString));
}
return methodDescriptions;
}
public static String toListString(List<MethodDescription> methodDescriptions) {
if (methodDescriptions.size() == 0) {
return "";
}
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(methodDescriptions.remove(0));
for (MethodDescription methodDescription : methodDescriptions) {
stringBuilder.append(',').append(methodDescription);
}
return stringBuilder.toString();
}
}