-
-
Notifications
You must be signed in to change notification settings - Fork 161
Expand file tree
/
Copy pathUTCompiler.java
More file actions
60 lines (56 loc) · 1.86 KB
/
UTCompiler.java
File metadata and controls
60 lines (56 loc) · 1.86 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
package processing.mode.java;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import processing.app.Platform;
import processing.app.exec.ProcessHelper;
import processing.app.exec.ProcessResult;
/**
* Utility class for compiling single compilationUnits.
*
* @author Jonathan Feinberg <[email protected]>
*
*/
class UTCompiler {
private final String classpath;
UTCompiler(File... classpath) throws IOException {
final StringBuilder sb = new StringBuilder();
for (final File f : classpath) {
if (sb.length() > 0)
sb.append(File.pathSeparatorChar);
sb.append(f.getAbsolutePath());
}
this.classpath = sb.toString();
}
ProcessResult compile(final String name, final String program)
throws IOException {
final File tmpdir = File.createTempFile("utcompiler", ".tmp");
if (!tmpdir.delete())
throw new IOException("Cannot delete " + tmpdir);
if (!tmpdir.mkdir())
throw new IOException("Cannot create " + tmpdir);
final File javaFile = new File(tmpdir, name + ".java");
final FileWriter java = new FileWriter(javaFile);
try {
java.write(program);
} finally {
java.close();
}
try {
return new ProcessHelper("javac",
"-sourcepath", tmpdir.getAbsolutePath(),
"-cp", classpath,
"-nowarn",
"-d", tmpdir.getAbsolutePath(),
javaFile.getAbsolutePath()).execute();
} catch (InterruptedException e) {
throw new RuntimeException(e);
} finally {
for (final File f: tmpdir.listFiles())
if (!f.getName().startsWith("."))if (!f.delete())
throw new IOException("Can't delete " + f);
if (!tmpdir.delete())
throw new IOException("Can't delete " + tmpdir);
}
}
}