5 import java.util.regex.*;
6 import java.nio.file.*;
7 import java.nio.file.attribute.*;
10 public class Compiler {
11 private final Map<Path, File> files = new HashMap<>();
13 public static class ClassOutput {
14 public final String name;
15 public final ByteArrayOutputStream buf = new ByteArrayOutputStream();
17 public ClassOutput(String name) {
22 public static class CompilationException extends RuntimeException {
23 public final Path file;
24 private final List<? extends Object> messages;
26 public CompilationException(Path file, List<? extends Object> messages) {
28 this.messages = messages;
31 public String getMessage() {
32 return(file + ": compilation failed");
35 public String messages() {
36 StringBuilder buf = new StringBuilder();
37 for(Object msg : messages)
38 buf.append(msg.toString() + "\n");
39 return(buf.toString());
42 public void printStackTrace(PrintStream out) {
43 out.print(messages());
44 super.printStackTrace(out);
48 public static class Compilation implements AutoCloseable {
49 private final Path dir, srcdir, outdir;
50 private final List<Path> infiles = new ArrayList<>();
51 private List<String> output = null;
53 public Compilation() throws IOException {
54 dir = Files.createTempDirectory("javac");
55 srcdir = dir.resolve("src");
56 outdir = dir.resolve("out");
57 Files.createDirectory(srcdir);
58 Files.createDirectory(outdir);
61 public void add(Path p) {
65 public boolean compile() throws IOException {
66 List<String> args = new ArrayList<>();
69 args.add(outdir.toString());
71 args.add(p.toString());
72 ProcessBuilder cmd = new ProcessBuilder(args);
73 cmd.redirectErrorStream(true);
74 cmd.redirectOutput(ProcessBuilder.Redirect.PIPE);
75 Process proc = cmd.start();
76 List<String> output = new ArrayList<>();
77 BufferedReader fp = new BufferedReader(new InputStreamReader(proc.getInputStream(), Utils.UTF8));
79 String ln = fp.readLine();
86 status = proc.waitFor();
87 } catch(InterruptedException e) {
88 Thread.currentThread().interrupt();
89 throw(new IOException("compilation interrupted"));
95 public List<String> output() {
97 throw(new IllegalStateException());
101 public Collection<ClassOutput> classes() throws IOException {
102 Collection<ClassOutput> ret = new ArrayList<>();
103 for(Path p : (Iterable<Path>)Files.walk(outdir)::iterator) {
104 Path rel = outdir.relativize(p);
105 String fn = rel.getName(rel.getNameCount() - 1).toString();
106 if(!Files.isRegularFile(p) || !fn.endsWith(".class"))
108 StringBuilder clnm = new StringBuilder();
109 for(int i = 0; i < rel.getNameCount() - 1; i++) {
110 clnm.append(rel.getName(i));
113 clnm.append(fn.substring(0, fn.length() - 6));
114 ClassOutput cls = new ClassOutput(clnm.toString());
115 Files.copy(p, cls.buf);
121 private static void remove(Path p) throws IOException {
122 if(Files.isDirectory(p)) {
123 for(Path ent : (Iterable<Path>)Files.list(p)::iterator)
129 public void close() throws IOException {
134 public static class BufferedClassLoader extends ClassLoader {
135 public final Map<String, byte[]> contents;
137 public BufferedClassLoader(Collection<ClassOutput> contents) {
138 this.contents = new HashMap<>();
139 for(ClassOutput clc : contents)
140 this.contents.put(clc.name, clc.buf.toByteArray());
143 public Class<?> findClass(String name) throws ClassNotFoundException {
144 byte[] c = contents.get(name);
146 throw(new ClassNotFoundException(name));
147 return(defineClass(name, c, 0, c.length));
151 public static class Module {
152 public final Path file;
153 public final ClassLoader code;
155 public Module(Path file) throws IOException {
157 try(Compilation c = new Compilation()) {
160 throw(new CompilationException(file, c.output()));
161 code = new BufferedClassLoader(c.classes());
165 private static final Pattern classpat = Pattern.compile("^((public|abstract)\\s+)*(class|interface)\\s+(\\S+)");
166 public void split(Compilation c) throws IOException {
167 StringBuilder head = new StringBuilder();
168 BufferedWriter cur = null;
169 try(BufferedReader fp = Files.newBufferedReader(file)) {
170 for(String ln = fp.readLine(); ln != null; ln = fp.readLine()) {
171 Matcher m = classpat.matcher(ln);
173 String clnm = m.group(4);
174 Path sp = c.srcdir.resolve(clnm + ".java");
178 cur = Files.newBufferedWriter(sp);
182 cur.append(ln); cur.append('\n');
184 head.append(ln); head.append('\n');
194 public static class File {
195 public final Path name;
196 private FileTime mtime = null;
197 private Module mod = null;
199 private File(Path name) {
203 public void update() throws IOException {
205 FileTime mtime = Files.getLastModifiedTime(name);
206 if((this.mtime == null) || (this.mtime.compareTo(mtime) < 0)) {
207 mod = new Module(name);
213 public Module mod() {
215 throw(new RuntimeException("file has not yet been updated"));
220 public File file(Path name) {
221 synchronized(files) {
222 File ret = files.get(name);
224 files.put(name, ret = new File(name));
229 private static Compiler global = null;
230 public static Compiler get() {
232 synchronized(Compiler.class) {
234 global = new Compiler();