1
2
3
4
5 package mockit.internal.util;
6
7 import edu.umd.cs.findbugs.annotations.NonNull;
8
9 import mockit.asm.types.ArrayType;
10 import mockit.asm.types.JavaType;
11 import mockit.asm.types.PrimitiveType;
12
13 public final class TypeDescriptor {
14 private static final Class<?>[] NO_PARAMETERS = {};
15
16 private TypeDescriptor() {
17 }
18
19 @NonNull
20 public static Class<?>[] getParameterTypes(@NonNull String methodDesc) {
21 JavaType[] paramTypes = JavaType.getArgumentTypes(methodDesc);
22
23 if (paramTypes.length == 0) {
24 return NO_PARAMETERS;
25 }
26
27 Class<?>[] paramClasses = new Class<?>[paramTypes.length];
28
29 for (int i = 0; i < paramTypes.length; i++) {
30 paramClasses[i] = getClassForType(paramTypes[i]);
31 }
32
33 return paramClasses;
34 }
35
36 @NonNull
37 public static Class<?> getReturnType(@NonNull String methodSignature) {
38 String methodDesc = methodDescriptionWithoutTypeArguments(methodSignature);
39 JavaType returnType = JavaType.getReturnType(methodDesc);
40 return getClassForType(returnType);
41 }
42
43 @NonNull
44 private static String methodDescriptionWithoutTypeArguments(@NonNull String methodSignature) {
45 while (true) {
46 int p = methodSignature.indexOf('<');
47
48 if (p < 0) {
49 return methodSignature;
50 }
51
52 String firstPart = methodSignature.substring(0, p);
53 int q = methodSignature.indexOf('>', p) + 1;
54
55 if (methodSignature.charAt(q) == '.') {
56 methodSignature = firstPart + '$' + methodSignature.substring(q + 1);
57 } else {
58 methodSignature = firstPart + methodSignature.substring(q);
59 }
60 }
61 }
62
63 @NonNull
64 public static Class<?> getClassForType(@NonNull JavaType type) {
65 if (type instanceof PrimitiveType) {
66 return ((PrimitiveType) type).getType();
67 }
68
69 String className;
70
71 if (type instanceof ArrayType) {
72 className = type.getDescriptor().replace('/', '.');
73 } else {
74 className = type.getClassName();
75 }
76
77 return ClassLoad.loadClass(className);
78 }
79 }