+ try(Compilation c = new Compilation()) {
+ split(c);
+ for(Path cp : classpath)
+ c.classpath(cp);
+ if(!c.compile())
+ throw(new CompilationException(file, c.output()));
+ ClassLoader parent = Compiler.class.getClassLoader();
+ if(!classpath.isEmpty() || !include.isEmpty()) {
+ Collection<ClassLoader> libs = new ArrayList<>();
+ for(Path cp : classpath)
+ libs.add(libloader(cp));
+ for(Module mod : include)
+ libs.add(mod.code);
+ parent = new LibClassLoader(parent, libs);
+ }
+ code = new BufferedClassLoader(parent, c.classes());
+ }
+ }
+
+ public void split(Compilation c) throws IOException {
+ StringBuilder head = new StringBuilder();
+ BufferedWriter cur = null;
+ try(BufferedReader fp = Files.newBufferedReader(file)) {
+ for(String ln = fp.readLine(); ln != null; ln = fp.readLine()) {
+ Matcher m = libpat.matcher(ln);
+ if(m.find()) {
+ Path lib = findlib(m.group(1));
+ if(lib == null)
+ throw(new CompilationException(file, Arrays.asList("no such library: " + m.group(1))));
+ classpath.add(lib);
+ }
+ m = incpat.matcher(ln);
+ if(m.find()) {
+ String nm = m.group(1);
+ File f= null;
+ if(f == null) {
+ Path p = file.resolveSibling(nm);
+ if(Files.isRegularFile(p))
+ f = file(p);
+ }
+ if(f == null) {
+ Path p = cwd.resolve(nm);
+ if(Files.isRegularFile(p))
+ f = file(p);
+ }
+ if(f == null)
+ throw(new CompilationException(file, Arrays.asList("no such file to include: " + nm)));
+ f.update();
+ c.addlib(f.mod().code.contents);
+ include.add(f.mod());
+ }
+ m = classpat.matcher(ln);
+ if(m.find()) {
+ String clnm = m.group(4);
+ Path sp = c.srcdir.resolve(clnm + ".java");
+ c.add(sp);
+ if(cur != null)
+ cur.close();
+ cur = Files.newBufferedWriter(sp);
+ cur.append(head);
+ }
+ if(cur != null) {
+ cur.append(ln); cur.append('\n');
+ } else {
+ head.append(ln); head.append('\n');
+ }
+ }
+ } finally {
+ if(cur != null)
+ cur.close();
+ }
+ }
+ }
+
+ public class File {
+ public final Path name;
+ private FileTime mtime = null;
+ private Module mod = null;
+
+ private File(Path name) {
+ this.name = name;