-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathDescriptionUtil.java
68 lines (57 loc) · 1.91 KB
/
DescriptionUtil.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
package me.earth.headlessmc.lwjgl.util;
import lombok.experimental.UtilityClass;
import lombok.val;
import org.objectweb.asm.Type;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
/**
* Basically {@link Type#getDescriptor(Class)} since that class might not be
* available at Runtime.
*/
@UtilityClass
public class DescriptionUtil {
private static final Map<Class<?>, String> PRIMITIVES = new HashMap<>();
static {
PRIMITIVES.put(boolean.class, "Z");
PRIMITIVES.put(byte.class, "B");
PRIMITIVES.put(short.class, "S");
PRIMITIVES.put(int.class, "I");
PRIMITIVES.put(long.class, "J");
PRIMITIVES.put(float.class, "F");
PRIMITIVES.put(double.class, "D");
PRIMITIVES.put(char.class, "C");
PRIMITIVES.put(void.class, "V");
}
public static String getDesc(Method method) {
val desc = new StringBuilder(method.getName()).append("(");
for (Class<?> parameter : method.getParameterTypes()) {
putDesc(parameter, desc);
}
putDesc(method.getReturnType(), desc.append(")"));
return desc.toString();
}
public static String getDesc(Class<?> type) {
val result = new StringBuilder();
putDesc(type, result);
return result.toString();
}
private static void putDesc(Class<?> type, StringBuilder builder) {
while (type.isArray()) {
builder.append("[");
type = type.getComponentType();
}
val primitive = PRIMITIVES.get(type);
if (primitive == null) {
builder.append("L");
String name = type.getName();
for (int i = 0; i < name.length(); i++) {
char ch = name.charAt(i);
builder.append(ch == '.' ? '/' : ch);
}
builder.append(";");
} else {
builder.append(primitive);
}
}
}