-
Notifications
You must be signed in to change notification settings - Fork 226
Expand file tree
/
Copy pathClassFile.java
More file actions
272 lines (238 loc) · 9.23 KB
/
ClassFile.java
File metadata and controls
272 lines (238 loc) · 9.23 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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
// Copyright (c) Corporation for National Research Initiatives
package org.python.compiler;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.File;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.objectweb.asm.AnnotationVisitor;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.FieldVisitor;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.Type;
import org.python.core.imp;
import org.python.compiler.ProxyCodeHelpers.AnnotationDescr;
public class ClassFile
{
ClassWriter cw;
int access;
long mtime;
public String name;
String superclass;
String sfilename;
String[] interfaces;
List<MethodVisitor> methodVisitors;
List<FieldVisitor> fieldVisitors;
List<AnnotationVisitor> annotationVisitors;
public static String fixName(String n) {
if (n.indexOf('.') == -1)
return n;
char[] c = n.toCharArray();
for(int i=0; i<c.length; i++) {
if (c[i] == '.') c[i] = '/';
}
return new String(c);
}
public static void visitAnnotations(AnnotationVisitor av, Map<String, Object> fields) {
for (Entry<String, Object>field: fields.entrySet()) {
visitAnnotation(av, field.getKey(), field.getValue());
}
}
// See org.objectweb.asm.AnnotationVisitor for details
// TODO Support annotation annotations and annotation array annotations
public static void visitAnnotation(AnnotationVisitor av, String fieldName, Object fieldValue) {
Class<?> fieldValueClass = fieldValue.getClass();
if (fieldValue instanceof Class) {
av.visit(fieldName, Type.getType((Class<?>)fieldValue));
} else if (fieldValueClass.isEnum()) {
av.visitEnum(fieldName, ProxyCodeHelpers.mapType(fieldValueClass), fieldValue.toString());
} else if (fieldValue instanceof List) {
AnnotationVisitor arrayVisitor = av.visitArray(fieldName);
List<Object> fieldList = (List<Object>)fieldValue;
for (Object arrayField: fieldList) {
visitAnnotation(arrayVisitor, null, arrayField);
}
arrayVisitor.visitEnd();
} else {
av.visit(fieldName, fieldValue);
}
}
public ClassFile(String name) {
this(name, "java/lang/Object", Opcodes.ACC_SYNCHRONIZED | Opcodes.ACC_PUBLIC,
org.python.core.imp.NO_MTIME);
}
public ClassFile(String name, String superclass, int access) {
this(name, superclass, access, org.python.core.imp.NO_MTIME);
}
public ClassFile(String name, String superclass, int access, long mtime) {
this.name = fixName(name);
this.superclass = fixName(superclass);
this.interfaces = new String[0];
this.access = access;
this.mtime = mtime;
cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
methodVisitors = Collections.synchronizedList(new ArrayList<MethodVisitor>());
fieldVisitors = Collections.synchronizedList(new ArrayList<FieldVisitor>());
annotationVisitors = Collections.synchronizedList(new ArrayList<AnnotationVisitor>());
}
public void setSource(String name) {
sfilename = name;
}
public void addInterface(String name)
throws IOException
{
String[] new_interfaces = new String[interfaces.length+1];
System.arraycopy(interfaces, 0, new_interfaces, 0, interfaces.length);
new_interfaces[interfaces.length] = name;
interfaces = new_interfaces;
}
public Code addMethod(String name, String type, int access)
throws IOException
{
MethodVisitor mv = cw.visitMethod(access, name, type, null, null);
Code pmv = new Code(mv, type, access);
methodVisitors.add(pmv);
return pmv;
}
public Code addMethod(String name, String type, int access, String[] exceptions)
throws IOException
{
MethodVisitor mv = cw.visitMethod(access, name, type, null, exceptions);
Code pmv = new Code(mv, type, access);
methodVisitors.add(pmv);
return pmv;
}
public Code addMethod(String name, String type, int access, String[] exceptions,
AnnotationDescr[]methodAnnotationDescrs, AnnotationDescr[][] parameterAnnotationDescrs)
throws IOException
{
MethodVisitor mv = cw.visitMethod(access, name, type, null, exceptions);
// method annotations
for (AnnotationDescr ad: methodAnnotationDescrs) {
AnnotationVisitor av = mv.visitAnnotation(ad.getName(), true);
if (ad.hasFields()) {
visitAnnotations(av, ad.getFields());
}
av.visitEnd();
}
// parameter annotations
for (int i = 0; i < parameterAnnotationDescrs.length; i++) {
for (AnnotationDescr ad: parameterAnnotationDescrs[i]) {
AnnotationVisitor av = mv.visitParameterAnnotation(i, ad.getName(), true);
if (ad.hasFields()) {
visitAnnotations(av, ad.getFields());
}
av.visitEnd();
}
}
Code pmv = new Code(mv, type, access);
methodVisitors.add(pmv);
return pmv;
}
public void addFinalStringLiteral(String name, String value)
throws IOException
{
FieldVisitor fv = cw.visitField(Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL +
Opcodes.ACC_STATIC, name, ClassConstants.$str, null, value);
fieldVisitors.add(fv);
}
public void addClassAnnotation(AnnotationDescr annotationDescr) {
AnnotationVisitor av = cw.visitAnnotation(annotationDescr.getName(), true);
if (annotationDescr.hasFields()) {
visitAnnotations(av, annotationDescr.getFields());
}
annotationVisitors.add(av);
}
public void addField(String name, String type, int access)
throws IOException
{
addField(name, type, access, null);
}
public void addField(String name, String type, int access, AnnotationDescr[] annotationDescrs)
throws IOException
{
FieldVisitor fv = cw.visitField(access, name, type, null, null);
if (annotationDescrs != null) {
for (AnnotationDescr ad: annotationDescrs) {
AnnotationVisitor av = fv.visitAnnotation(ad.getName(), true);
if (ad.hasFields()) {
visitAnnotations(av, ad.getFields());
}
av.visitEnd();
}
}
fieldVisitors.add(fv);
}
public void endFields()
throws IOException
{
for (FieldVisitor fv : fieldVisitors) {
fv.visitEnd();
}
}
public void endMethods()
throws IOException
{
for (int i=0; i<methodVisitors.size(); i++) {
MethodVisitor mv = methodVisitors.get(i);
mv.visitMaxs(0,0);
mv.visitEnd();
}
}
public void endClassAnnotations() {
for (AnnotationVisitor av: annotationVisitors) {
av.visitEnd();
}
}
public void write(OutputStream stream)
throws IOException
{
String sfilenameShort = sfilename;
if (sfilename != null) {
try {
Path pth = new File("dist/Lib").toPath().normalize().toAbsolutePath();
Path pth2 = new File(sfilename).toPath().normalize().toAbsolutePath();
sfilenameShort = pth.relativize(pth2).toString();
if (sfilenameShort.startsWith("..")) {
// prefer absolute path in this case
sfilenameShort = sfilename;
}
if (File.separatorChar != '/') {
// Make the path uniform on all platforms. We use POSIX- and URL-notation here.
sfilenameShort = sfilenameShort.replace(File.separatorChar, '/');
}
} catch (Exception fe) {}
}
cw.visit(Opcodes.V1_6, Opcodes.ACC_PUBLIC + Opcodes.ACC_SUPER, this.name, null, this.superclass, interfaces);
AnnotationVisitor av = cw.visitAnnotation("Lorg/python/compiler/APIVersion;", true);
// XXX: should imp.java really house this value or should imp.java point into
// org.python.compiler?
av.visit("value", Integer.valueOf(imp.getAPIVersion()));
av.visitEnd();
av = cw.visitAnnotation("Lorg/python/compiler/MTime;", true);
av.visit("value", Long.valueOf(mtime));
av.visitEnd();
if (sfilenameShort != null) {
av = cw.visitAnnotation("Lorg/python/compiler/Filename;", true);
av.visit("value", sfilenameShort);
av.visitEnd();
cw.visitSource(sfilenameShort, null);
}
endClassAnnotations();
endFields();
endMethods();
byte[] ba = cw.toByteArray();
//fos = io.FileOutputStream("%s.class" % self.name)
ByteArrayOutputStream baos = new ByteArrayOutputStream(ba.length);
baos.write(ba, 0, ba.length);
baos.writeTo(stream);
//debug(baos);
baos.close();
}
}