提交 c3ce4860 authored 作者: Uncle-pan's avatar Uncle-pan

Bugfix - check javac compile() exit status instead of parsing it's

output text
上级 16459470
...@@ -339,12 +339,13 @@ public class SourceCompiler { ...@@ -339,12 +339,13 @@ public class SourceCompiler {
ArrayList<JavaFileObject> compilationUnits = new ArrayList<>(); ArrayList<JavaFileObject> compilationUnits = new ArrayList<>();
compilationUnits.add(new StringJavaFileObject(fullClassName, source)); compilationUnits.add(new StringJavaFileObject(fullClassName, source));
// cannot concurrently compile // cannot concurrently compile
final boolean ok;
synchronized (JAVA_COMPILER) { synchronized (JAVA_COMPILER) {
JAVA_COMPILER.getTask(writer, fileManager, null, null, ok = JAVA_COMPILER.getTask(writer, fileManager, null, null,
null, compilationUnits).call(); null, compilationUnits).call();
} }
String output = writer.toString(); String output = writer.toString();
handleSyntaxError(output); handleSyntaxError(output, (ok? 0: 1));
return fileManager.getClassLoader(null).loadClass(fullClassName); return fileManager.getClassLoader(null).loadClass(fullClassName);
} catch (ClassNotFoundException | IOException e) { } catch (ClassNotFoundException | IOException e) {
throw DbException.convert(e); throw DbException.convert(e);
...@@ -375,7 +376,7 @@ public class SourceCompiler { ...@@ -375,7 +376,7 @@ public class SourceCompiler {
copyInThread(p.getErrorStream(), buff); copyInThread(p.getErrorStream(), buff);
p.waitFor(); p.waitFor();
String output = new String(buff.toByteArray(), StandardCharsets.UTF_8); String output = new String(buff.toByteArray(), StandardCharsets.UTF_8);
handleSyntaxError(output); handleSyntaxError(output, p.exitValue());
return p.exitValue(); return p.exitValue();
} catch (Exception e) { } catch (Exception e) {
throw DbException.convert(e); throw DbException.convert(e);
...@@ -400,14 +401,17 @@ public class SourceCompiler { ...@@ -400,14 +401,17 @@ public class SourceCompiler {
Method compile; Method compile;
compile = JAVAC_SUN.getMethod("compile", String[].class); compile = JAVAC_SUN.getMethod("compile", String[].class);
Object javac = JAVAC_SUN.getDeclaredConstructor().newInstance(); Object javac = JAVAC_SUN.getDeclaredConstructor().newInstance();
compile.invoke(javac, (Object) new String[] { // Bugfix: Here we should check exit status value instead of parsing javac output text.
// Because of the output text is different in different locale environment.
// @since 2018-07-20 little-pan
final Integer status = (Integer)compile.invoke(javac, (Object) new String[] {
"-sourcepath", COMPILE_DIR, "-sourcepath", COMPILE_DIR,
// "-Xlint:unchecked", // "-Xlint:unchecked",
"-d", COMPILE_DIR, "-d", COMPILE_DIR,
"-encoding", "UTF-8", "-encoding", "UTF-8",
javaFile.getAbsolutePath() }); javaFile.getAbsolutePath() });
String output = new String(buff.toByteArray(), StandardCharsets.UTF_8); String output = new String(buff.toByteArray(), StandardCharsets.UTF_8);
handleSyntaxError(output); handleSyntaxError(output, status);
} catch (Exception e) { } catch (Exception e) {
throw DbException.convert(e); throw DbException.convert(e);
} finally { } finally {
...@@ -415,7 +419,10 @@ public class SourceCompiler { ...@@ -415,7 +419,10 @@ public class SourceCompiler {
} }
} }
private static void handleSyntaxError(String output) { private static void handleSyntaxError(String output, int exitStatus) {
if(0 == exitStatus){
return;
}
boolean syntaxError = false; boolean syntaxError = false;
final BufferedReader reader = new BufferedReader(new StringReader(output)); final BufferedReader reader = new BufferedReader(new StringReader(output));
try { try {
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论