16 StringWriter outWriter =
new StringWriter();
17 StringWriter errWriter =
new StringWriter();
18 PrintWriter out =
new PrintWriter(outWriter);
19 PrintWriter err =
new PrintWriter(errWriter);
23 if (sourceCode.length != className.length) {
24 throw new RuntimeException(
"Impossible length mismatch in CompilerService");
27 if (!srcdir.exists()) srcdir.mkdirs();
28 if (!bindir.exists()) bindir.mkdirs();
30 for (
int i = 0; i < className.length; i++) {
32 if (className[i].contains(
".")) {
33 File packagePath =
new File(srcdir, className[i].replace(
'.',
'/')).getParentFile();
34 if (!packagePath.exists()) packagePath.mkdirs();
35 sourceFile =
new File(packagePath, className[i].substring(className[i].lastIndexOf(
'.') + 1) +
".java");
37 sourceFile =
new File(srcdir, className[i] +
".java");
40 byte[] newBytes = sourceCode[i].getBytes();
41 boolean shouldWrite =
true;
42 if (sourceFile.exists()) {
44 byte[] oldBytes = Files.readAllBytes(sourceFile.toPath());
45 if (java.util.Arrays.equals(newBytes, oldBytes)) {
48 }
catch (IOException e) {}
52 Files.write(sourceFile.toPath(), newBytes);
57 List<String> args =
new ArrayList<>();
59 args.add(
"-proc:none");
61 args.add(System.getProperty(
"java.class.path"));
63 args.add(bindir.getAbsolutePath());
64 args.add(srcdir.getAbsolutePath());
67 res.success = Main.compile(args.toArray(
new String[0]), out, err,
null);
71 res.output = outWriter.toString() +
"\n" + errWriter.toString();
75 Pattern errorPattern = Pattern.compile(
"^\\d+\\.\\s+ERROR\\s+in\\s+(.*?)\\s+\\(at\\s+line\\s+(\\d+)\\)");
76 BufferedReader reader =
new BufferedReader(
new StringReader(errWriter.toString()));
78 while ((line = reader.readLine()) !=
null) {
79 Matcher m = errorPattern.matcher(line);
81 String fullPath = m.group(1);
82 String fileName =
new File(fullPath).getName();
83 int lineNum = Integer.parseInt(m.group(2));
88 String messageLine = reader.readLine();
89 if (messageLine !=
null) {
96 }
catch (IOException e) {
98 res.output = e.getMessage();