-
Notifications
You must be signed in to change notification settings - Fork 226
Expand file tree
/
Copy pathExposeTask.java
More file actions
85 lines (75 loc) · 2.59 KB
/
ExposeTask.java
File metadata and controls
85 lines (75 loc) · 2.59 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 java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Set;
import org.apache.tools.ant.BuildException;
import org.objectweb.asm.ClassWriter;
import org.python.util.GlobMatchingTask;
public class ExposeTask extends GlobMatchingTask {
@Override
protected String getFrom() {
return "*.class";
}
@Override
protected String getTo() {
return "*.class";
}
@Override
public void process(Set<File> toExpose) throws BuildException {
if (toExpose.size() > 1) {
log("Exposing " + toExpose.size() + " classes");
} else if (toExpose.size() == 1) {
log("Exposing 1 class");
}
expose(toExpose);
}
private void expose(Set<File> toExpose) {
for (File f : toExpose) {
ExposedTypeProcessor etp;
try {
etp = new ExposedTypeProcessor(new FileInputStream(f));
} catch (IOException e) {
throw new BuildException("Unable to read '" + f + "' to expose it", e);
} catch (InvalidExposingException iee) {
throw new BuildException(iee.getMessage());
}
for (MethodExposer exposer : etp.getMethodExposers()) {
generate(exposer);
}
for (DescriptorExposer exposer : etp.getDescriptorExposers()) {
generate(exposer);
}
if (etp.getNewExposer() != null) {
generate(etp.getNewExposer());
}
generate(etp.getTypeExposer());
write(etp.getExposedClassName(), etp.getBytecode());
}
}
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...
}
}
}
}
}