-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathModuleExposeTask.java
More file actions
85 lines (75 loc) · 2.53 KB
/
Copy pathModuleExposeTask.java
File metadata and controls
85 lines (75 loc) · 2.53 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
package org.python.expose.generate;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;
import org.objectweb.asm.ClassWriter;
import org.python.modules.Setup;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
/**
* Created by isaiah on 7/11/16.
*/
public class ModuleExposeTask extends Task {
public File getDestDir() {
return destDir;
}
public void setDestDir(File destDir) {
this.destDir = destDir;
}
protected File destDir;
@Override
public void execute() throws BuildException {
for (String mod : Setup.newbuiltinModules) {
InputStream in = getClass().getClassLoader()
.getResourceAsStream(className(mod).replace('.', '/') + ".class");
ExposedModuleProcessor ice = new ExposedModuleProcessor(in);
for(MethodExposer exposer : ice.getMethodExposers()) {
generate(exposer);
}
generate(ice.getModuleExposer());
}
}
private String className(String name) {
String classname;
String modname;
int colon = name.indexOf(':');
if (colon != -1) {
// name:fqclassname
modname = name.substring(0, colon).trim();
classname = name.substring(colon + 1, name.length()).trim();
if (classname.equals("null")) {
// name:null, i.e. remove it
classname = null;
}
} else {
modname = name.trim();
classname = "org.python.modules." + modname;
}
return classname;
}
private void generate(Exposer exposer) {
ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
exposer.generate(writer);
write(exposer.getClassName(), writer.toByteArray());
}
private void write(String destClass, byte[] newClassfile) {
File dest = new File(destDir, destClass.replace('.', '/') + ".class");
dest.getParentFile().mkdirs();// TODO - check for success
FileOutputStream out = null;
try {
out = new FileOutputStream(dest);
out.write(newClassfile);
} catch (IOException e) {
throw new BuildException("Unable to write to '" + dest + "'", e);
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
// Le sigh...
}
}
}
}
}