-
Notifications
You must be signed in to change notification settings - Fork 227
Expand file tree
/
Copy pathCodegenUtils.java
More file actions
255 lines (219 loc) · 7.99 KB
/
CodegenUtils.java
File metadata and controls
255 lines (219 loc) · 7.99 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/*
* CodegenUtils.java
*
* Created on January 31, 2007, 11:54 AM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package org.python.modules.jffi;
import org.objectweb.asm.AnnotationVisitor;
import org.objectweb.asm.Type;
import java.util.Arrays;
import java.util.Map;
/**
*
* @author headius
*/
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) + ";";
}
}
}
/**
* Creates a human-readable representation, from a Class.
*/
public static String human(Class n) {
return n.getCanonicalName();
}
public static String humanShort(Class n) {
return n.getSimpleName();
}
/**
* 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[] retvalParams) {
Class[] justParams = new Class[retvalParams.length - 1];
System.arraycopy(retvalParams, 1, justParams, 0, justParams.length);
return sigParams(justParams) + ci(retvalParams[0]);
}
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 String pretty(Class retval, Class... params) {
return prettyParams(params) + human(retval);
}
public static String prettyParams(Class... params) {
StringBuilder signature = new StringBuilder("(");
for (int i = 0; i < params.length; i++) {
signature.append(human(params[i]));
if (i < params.length - 1) signature.append(',');
}
signature.append(")");
return signature.toString();
}
public static String prettyShortParams(Class... params) {
StringBuilder signature = new StringBuilder("(");
for (int i = 0; i < params.length; i++) {
signature.append(humanShort(params[i]));
if (i < params.length - 1) signature.append(',');
}
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;
}
public static Class[] params(Class cls1, Class cls2, Class clsFill, int times) {
Class[] classes = new Class[times + 2];
Arrays.fill(classes, clsFill);
classes[0] = cls1;
classes[1] = cls2;
return classes;
}
public static String getAnnotatedBindingClassName(String javaMethodName, String typeName, boolean isStatic, int required, int optional, boolean multi, boolean framed) {
String commonClassSuffix;
if (multi) {
commonClassSuffix = (isStatic ? "$s$" : "$i$" ) + javaMethodName;
} else {
commonClassSuffix = (isStatic ? "$s$" : "$i$" ) + required + "$" + optional + "$" + javaMethodName;
}
return typeName + commonClassSuffix;
}
public static void visitAnnotationFields(AnnotationVisitor visitor, Map<String, Object> fields) {
for (Map.Entry<String, Object> fieldEntry : fields.entrySet()) {
Object value = fieldEntry.getValue();
if (value.getClass().isArray()) {
Object[] values = (Object[]) value;
AnnotationVisitor arrayV = visitor.visitArray(fieldEntry.getKey());
for (int i = 0; i < values.length; i++) {
arrayV.visit(null, values[i]);
}
arrayV.visitEnd();
} else if (value.getClass().isEnum()) {
visitor.visitEnum(fieldEntry.getKey(), ci(value.getClass()), value.toString());
} else if (value instanceof Class) {
visitor.visit(fieldEntry.getKey(), Type.getType((Class) value));
} else {
visitor.visit(fieldEntry.getKey(), value);
}
}
}
public static Class getBoxType(Class type) {
if (type == int.class) {
return Integer.class;
} else if (type == byte.class) {
return Byte.class;
} else if (type == short.class) {
return Short.class;
} else if (type == char.class) {
return Character.class;
} else if (type == long.class) {
return Long.class;
} else if (type == float.class) {
return Float.class;
} else if (type == double.class) {
return Double.class;
} else if (type == boolean.class) {
return Boolean.class;
} else {
throw new RuntimeException("Not a native type: " + type);
}
}
}