-
Notifications
You must be signed in to change notification settings - Fork 226
Expand file tree
/
Copy pathCodegenUtils.java
More file actions
137 lines (116 loc) · 3.95 KB
/
CodegenUtils.java
File metadata and controls
137 lines (116 loc) · 3.95 KB
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
130
131
132
133
134
135
136
137
/*
* Initial code taken from Charlie Nutter's org.jruby.util.CodegenUtils.
*/
package org.python.util;
import java.util.Arrays;
public class CodegenUtils {
/**
* Creates a dotted class name from a path/package name
*/
public static String c(String p) {
return p.replace('/', '.');
}
/**
* Creates a class path name, from a Class.
*/
public static String p(Class n) {
return n.getName().replace('.','/');
}
/**
* Creates a class identifier of form Labc/abc;, from a Class.
*/
public static String ci(Class n) {
if (n.isArray()) {
n = n.getComponentType();
if (n.isPrimitive()) {
if (n == Byte.TYPE) {
return "[B";
} else if (n == Boolean.TYPE) {
return "[Z";
} else if (n == Short.TYPE) {
return "[S";
} else if (n == Character.TYPE) {
return "[C";
} else if (n == Integer.TYPE) {
return "[I";
} else if (n == Float.TYPE) {
return "[F";
} else if (n == Double.TYPE) {
return "[D";
} else if (n == Long.TYPE) {
return "[J";
} else {
throw new RuntimeException("Unrecognized type in compiler: " + n.getName());
}
} else {
return "[" + ci(n);
}
} else {
if (n.isPrimitive()) {
if (n == Byte.TYPE) {
return "B";
} else if (n == Boolean.TYPE) {
return "Z";
} else if (n == Short.TYPE) {
return "S";
} else if (n == Character.TYPE) {
return "C";
} else if (n == Integer.TYPE) {
return "I";
} else if (n == Float.TYPE) {
return "F";
} else if (n == Double.TYPE) {
return "D";
} else if (n == Long.TYPE) {
return "J";
} else if (n == Void.TYPE) {
return "V";
} else {
throw new RuntimeException("Unrecognized type in compiler: " + n.getName());
}
} else {
return "L" + p(n) + ";";
}
}
}
/**
* Create a method signature from the given param types and return values
*/
public static String sig(Class retval, Class... params) {
return sigParams(params) + ci(retval);
}
public static String sig(Class retval, String descriptor, Class... params) {
return sigParams(descriptor, params) + ci(retval);
}
public static String sigParams(Class... params) {
StringBuilder signature = new StringBuilder("(");
for (int i = 0; i < params.length; i++) {
signature.append(ci(params[i]));
}
signature.append(")");
return signature.toString();
}
public static String sigParams(String descriptor, Class... params) {
StringBuilder signature = new StringBuilder("(");
signature.append(descriptor);
for (int i = 0; i < params.length; i++) {
signature.append(ci(params[i]));
}
signature.append(")");
return signature.toString();
}
public static Class[] params(Class... classes) {
return classes;
}
public static Class[] params(Class cls, int times) {
Class[] classes = new Class[times];
Arrays.fill(classes, cls);
return classes;
}
public static Class[] params(Class cls1, Class clsFill, int times) {
Class[] classes = new Class[times + 1];
Arrays.fill(classes, clsFill);
classes[0] = cls1;
return classes;
}
}