CORE-57 - Surface javac diagnostics for runtime compile failures#180
CORE-57 - Surface javac diagnostics for runtime compile failures#180
Conversation
|
KealanTolandChronicle
left a comment
There was a problem hiding this comment.
Overall looks good, just one small comment about thread safety string appending, approved!
| javaFileObjects.put(className, new JavaSourceFromString(className, javaCode)); | ||
| compilationUnits = new ArrayList<>(javaFileObjects.values()); // To prevent CME from compiler code | ||
| } | ||
| StringBuilder diagnostics = new StringBuilder(); |
There was a problem hiding this comment.
This StringBuilder is captured in the DiagnosticListener lambda below, but if javac calls report() on multiple threads, then StringBuilder.append() will not be thread safe. Might be better to use StringBuffer or else explicit synchronisation, unless we can guarantee this will always be single-threaded ?
There was a problem hiding this comment.
The compile is done in this thread from the call() method, so I don't think there foresee any concurrency issues
| fileManagerMap.put(classLoader, fileManager); | ||
| } | ||
| final Map<String, byte[]> compiled = compileFromJava(className, javaCode, printWriter, fileManager); | ||
| final CompilationResult compilation = compileFromJavaResult(className, javaCode, printWriter, fileManager); |
There was a problem hiding this comment.
Is it worth having the compilation result examined from within the compileFromJavaResult() method to reduce complexity in this method?
Also this way, changing of the method signature may not be required as it can still return the Map<String, byte[]> of the compiled classes if it is a success and the className exists in the map of compiled (as its provided to the compileFromJava() method. Then to allow for diagnostics to be used, you can pass in the StringBuilder on the call - this can determine whether the caller actually cares about diagnostics and add the detail if it's asked for
benbonavia
left a comment
There was a problem hiding this comment.
Please reduce the complexity as per the sonar analysis



CORE-57 - Surface javac diagnostics for runtime compile failures
Summary
Surfaces javac diagnostics directly when runtime compilation fails, instead of masking the real compile failure as a generated-class
ClassNotFoundException.Context
CachedCompiler.compileFromJava(...)returns an empty map when javac fails. Previously,CachedCompiler.loadFromJava(...)did not distinguish that failure from a successful compile with no classes to define, and still calledclassLoader.loadClass(className).With module-style classloaders, this can produce a misleading
ClassNotFoundExceptionfor the generated class name, hiding the actual javac diagnostics such as missing symbols or unresolved types.Changes
CompilationResultpath inCachedCompiler.compileFromJava(...)behavior.PrintWriter.loadFromJava(...)to throw diagnosticClassNotFoundExceptionimmediately when javac compilation fails.defineClass(...)instead of falling through toclassLoader.loadClass(...).Verification