-
Notifications
You must be signed in to change notification settings - Fork 147
CORE-57 - Surface javac diagnostics for runtime compile failures #180
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -172,6 +172,13 @@ | |
| @NotNull String javaCode, | ||
| final @NotNull PrintWriter writer, | ||
| MyJavaFileManager fileManager) { | ||
| return compileFromJavaResult(className, javaCode, writer, fileManager).classes; | ||
| } | ||
|
|
||
| private CompilationResult compileFromJavaResult(@NotNull String className, | ||
| @NotNull String javaCode, | ||
| final @NotNull PrintWriter writer, | ||
| MyJavaFileManager fileManager) { | ||
| validateClassName(className); | ||
| Iterable<? extends JavaFileObject> compilationUnits; | ||
| if (sourceDir != null) { | ||
|
|
@@ -186,12 +193,15 @@ | |
| javaFileObjects.put(className, new JavaSourceFromString(className, javaCode)); | ||
| compilationUnits = new ArrayList<>(javaFileObjects.values()); // To prevent CME from compiler code | ||
| } | ||
| StringBuilder diagnostics = new StringBuilder(); | ||
| // reuse the same file manager to allow caching of jar files | ||
| boolean ok = s_compiler.getTask(writer, fileManager, new DiagnosticListener<JavaFileObject>() { | ||
| @Override | ||
| public void report(Diagnostic<? extends JavaFileObject> diagnostic) { | ||
| if (diagnostic.getKind() == Diagnostic.Kind.ERROR) { | ||
| writer.println(diagnostic); | ||
| String message = diagnostic.toString(); | ||
| writer.println(message); | ||
| diagnostics.append(message).append(System.lineSeparator()); | ||
| } | ||
| } | ||
| }, options, null, compilationUnits).call(); | ||
|
|
@@ -202,11 +212,11 @@ | |
| javaFileObjects.remove(className); | ||
|
|
||
| // nothing to return due to compiler error | ||
| return Collections.emptyMap(); | ||
| return new CompilationResult(false, Collections.emptyMap(), diagnostics.toString()); | ||
| } else { | ||
| Map<String, byte[]> result = fileManager.getAllBuffers(); | ||
|
|
||
| return result; | ||
| return new CompilationResult(true, result, diagnostics.toString()); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -222,7 +232,7 @@ | |
| * @return the loaded class instance | ||
| * @throws ClassNotFoundException if definition fails | ||
| */ | ||
| public Class<?> loadFromJava(@NotNull ClassLoader classLoader, | ||
|
Check failure on line 235 in src/main/java/net/openhft/compiler/CachedCompiler.java
|
||
| @NotNull String className, | ||
| @NotNull String javaCode, | ||
| @Nullable PrintWriter writer) throws ClassNotFoundException { | ||
|
|
@@ -245,7 +255,16 @@ | |
| fileManager = getFileManager(standardJavaFileManager); | ||
| fileManagerMap.put(classLoader, fileManager); | ||
| } | ||
| final Map<String, byte[]> compiled = compileFromJava(className, javaCode, printWriter, fileManager); | ||
| final CompilationResult compilation = compileFromJavaResult(className, javaCode, printWriter, fileManager); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it worth having the compilation result examined from within the Also this way, changing of the method signature may not be required as it can still return the
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point @benbonavia, I’ve moved the compile-result checks out of loadFromJava(...) into a private compileFromJavaOrThrow(...) helper, so the caller method is simpler while keeping the existing package-private compileFromJava(...) behavior unchanged. I did not move the throwing directly into compileFromJavaResult(...) because that method is also used to preserve the existing compileFromJava(...) contract of returning Map<String, byte[]>, including an empty map on javac failure. I also kept CompilationResult rather than passing diagnostics in as an out-parameter. loadFromJava(...) needs the success flag as well as the diagnostics so it can distinguish “javac failed” from “javac succeeded but did not produce the requested className”. That distinction is the main CORE-57 fix, so I think keeping it explicit is clearer than inferring it from a map plus a mutable diagnostics argument. I’ve also since split the rest of loadFromJava(...) into smaller helpers for the Sonar complexity issue: cache lookup, file-manager lookup, class definition, class-file writing, and final loaded-class lookup are now separated while preserving the existing locking behavior. |
||
| if (!compilation.success) { | ||
| throw compilationFailedException(className, compilation.diagnostics); | ||
| } | ||
|
|
||
| final Map<String, byte[]> compiled = compilation.classes; | ||
| if (!compiled.containsKey(className)) { | ||
| throw missingCompiledClassException(className, compiled.keySet(), compilation.diagnostics); | ||
| } | ||
|
|
||
| for (Map.Entry<String, byte[]> entry : compiled.entrySet()) { | ||
| String className2 = entry.getKey(); | ||
| validateClassName(className2); | ||
|
|
@@ -275,7 +294,10 @@ | |
| } | ||
| } | ||
| synchronized (loadedClassesMap) { | ||
| loadedClasses.put(className, clazz = classLoader.loadClass(className)); | ||
| clazz = loadedClasses.get(className); | ||
| } | ||
| if (clazz == null) { | ||
| throw missingCompiledClassException(className, compiled.keySet(), compilation.diagnostics); | ||
| } | ||
| return clazz; | ||
| } | ||
|
|
@@ -327,6 +349,27 @@ | |
| return candidate.toFile(); | ||
| } | ||
|
|
||
| private static ClassNotFoundException compilationFailedException(String className, String diagnostics) { | ||
| String diagnosticText = diagnostics.trim(); | ||
| String message = "Compilation failed for " + className; | ||
| if (!diagnosticText.isEmpty()) { | ||
| message += System.lineSeparator() + diagnosticText; | ||
| } | ||
| return new ClassNotFoundException(message, new IllegalStateException(message)); | ||
| } | ||
|
|
||
| private static ClassNotFoundException missingCompiledClassException(String className, | ||
| Set<String> compiledClassNames, | ||
| String diagnostics) { | ||
| String diagnosticText = diagnostics.trim(); | ||
| String message = "Compilation did not produce requested class " + className | ||
| + ". Compiled classes: " + compiledClassNames; | ||
| if (!diagnosticText.isEmpty()) { | ||
| message += System.lineSeparator() + diagnosticText; | ||
| } | ||
| return new ClassNotFoundException(message, new IllegalStateException(message)); | ||
| } | ||
|
|
||
| private static PrintWriter createDefaultWriter() { | ||
| OutputStreamWriter writer = new OutputStreamWriter(System.err, StandardCharsets.UTF_8); | ||
| return new PrintWriter(writer, true) { | ||
|
|
@@ -336,4 +379,16 @@ | |
| } | ||
| }; | ||
| } | ||
|
|
||
| private static final class CompilationResult { | ||
| private final boolean success; | ||
| private final Map<String, byte[]> classes; | ||
| private final String diagnostics; | ||
|
|
||
| private CompilationResult(boolean success, Map<String, byte[]> classes, String diagnostics) { | ||
| this.success = success; | ||
| this.classes = classes; | ||
| this.diagnostics = diagnostics; | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| /* | ||
| * Copyright 2013-2025 chronicle.software; SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
| package net.openhft.compiler; | ||
|
|
||
| import org.junit.Test; | ||
|
|
||
| import javax.tools.JavaCompiler; | ||
| import javax.tools.StandardJavaFileManager; | ||
| import javax.tools.ToolProvider; | ||
| import java.io.PrintWriter; | ||
| import java.io.StringWriter; | ||
| import java.util.Map; | ||
|
|
||
| import static org.junit.Assert.*; | ||
|
|
||
| public class CachedCompilerModuleClassLoaderReproTest { | ||
|
|
||
| @Test | ||
| public void moduleLikeLoaderCanLoadClassAfterSuccessfulDefineClass() throws Exception { | ||
| ModuleLikeClassLoader loader = new ModuleLikeClassLoader(); | ||
| CachedCompiler compiler = new CachedCompiler(null, null); | ||
|
|
||
| Class<?> clazz = compiler.loadFromJava(loader, | ||
| "app.Generated", | ||
| "package app; public class Generated { public int value() { return 42; } }"); | ||
|
|
||
| assertEquals("app.Generated", clazz.getName()); | ||
| assertSame(clazz, loader.loadClass("app.Generated")); | ||
| assertEquals(42, clazz.getDeclaredMethod("value").invoke(clazz.getDeclaredConstructor().newInstance())); | ||
| } | ||
|
|
||
| @Test | ||
| public void compileFailureForLoaderOnlyDependencyReportsJavacDiagnostics() throws Exception { | ||
| ModuleLikeClassLoader loader = new ModuleLikeClassLoader(); | ||
| defineLoaderOnlyDependency(loader); | ||
|
|
||
| assertSame("Sanity check: the supplied class loader can see the dependency", | ||
| loader.loadClass("app.Dto"), | ||
| Class.forName("app.Dto", false, loader)); | ||
|
|
||
| CachedCompiler compiler = new CachedCompiler(null, null); | ||
| StringWriter diagnostics = new StringWriter(); | ||
|
|
||
| ClassNotFoundException thrown = assertThrows(ClassNotFoundException.class, | ||
| () -> compiler.loadFromJava(loader, | ||
| "app.GeneratedUsesDto", | ||
| "package app; public class GeneratedUsesDto { app.Dto dto; }", | ||
| new PrintWriter(diagnostics))); | ||
|
|
||
| assertTrue("Thrown exception should identify compilation failure: " + thrown.getMessage(), | ||
| thrown.getMessage().contains("Compilation failed for app.GeneratedUsesDto")); | ||
| assertTrue("Thrown exception should include javac missing-symbol diagnostics: " + thrown.getMessage(), | ||
| thrown.getMessage().contains("cannot find symbol")); | ||
| assertTrue("Thrown exception should include the dependency javac could not resolve: " + thrown.getMessage(), | ||
| thrown.getMessage().contains("Dto")); | ||
| assertNotNull("Thrown exception should carry a diagnostic cause", thrown.getCause()); | ||
| assertTrue("javac diagnostics should mention the dependency that the compiler could not resolve: " | ||
| + diagnostics, | ||
| diagnostics.toString().contains("Dto")); | ||
| assertNull("The generated class should not have been defined after javac failure", | ||
| loader.findLoaded("app.GeneratedUsesDto")); | ||
| } | ||
|
|
||
| @Test | ||
| public void successfulCompileWithoutRequestedClassReportsMissingOutput() { | ||
| ModuleLikeClassLoader loader = new ModuleLikeClassLoader(); | ||
| CachedCompiler compiler = new CachedCompiler(null, null); | ||
|
|
||
| ClassNotFoundException thrown = assertThrows(ClassNotFoundException.class, | ||
| () -> compiler.loadFromJava(loader, | ||
| "app.Expected", | ||
| "package app; class Different {}")); | ||
|
|
||
| assertTrue("Thrown exception should identify the missing requested class: " + thrown.getMessage(), | ||
| thrown.getMessage().contains("Compilation did not produce requested class app.Expected")); | ||
| assertTrue("Thrown exception should identify the class javac actually produced: " + thrown.getMessage(), | ||
| thrown.getMessage().contains("app.Different")); | ||
| assertNotNull("Thrown exception should carry a diagnostic cause", thrown.getCause()); | ||
| assertNull("The requested class should not have been defined", | ||
| loader.findLoaded("app.Expected")); | ||
| } | ||
|
|
||
| private static void defineLoaderOnlyDependency(ModuleLikeClassLoader loader) throws Exception { | ||
| JavaCompiler javac = ToolProvider.getSystemJavaCompiler(); | ||
| assertNotNull("System compiler required", javac); | ||
|
|
||
| try (StandardJavaFileManager standardManager = javac.getStandardFileManager(null, null, null)) { | ||
| CachedCompiler bytecodeCompiler = new CachedCompiler(null, null); | ||
| MyJavaFileManager fileManager = new MyJavaFileManager(standardManager); | ||
| Map<String, byte[]> classes = bytecodeCompiler.compileFromJava( | ||
| "app.Dto", | ||
| "package app; public class Dto {}", | ||
| fileManager); | ||
| byte[] dtoBytes = classes.get("app.Dto"); | ||
| assertNotNull(dtoBytes); | ||
| CompilerUtils.defineClass(loader, "app.Dto", dtoBytes); | ||
| } | ||
| } | ||
|
|
||
| private static final class ModuleLikeClassLoader extends ClassLoader { | ||
| private static final String APP_PREFIX = "app."; | ||
|
|
||
| ModuleLikeClassLoader() { | ||
| super(CachedCompilerModuleClassLoaderReproTest.class.getClassLoader()); | ||
| } | ||
|
|
||
| @Override | ||
| public Class<?> loadClass(String name) throws ClassNotFoundException { | ||
| return loadClass(name, false); | ||
| } | ||
|
|
||
| @Override | ||
| protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException { | ||
| if (!name.startsWith(APP_PREFIX)) { | ||
| return super.loadClass(name, resolve); | ||
| } | ||
| return findClass(name, resolve); | ||
| } | ||
|
|
||
| private Class<?> findClass(String name, boolean resolve) throws ClassNotFoundException { | ||
| Class<?> loaded = findLoadedClass(name); | ||
| if (loaded != null) { | ||
| if (resolve) { | ||
| resolveClass(loaded); | ||
| } | ||
| return loaded; | ||
| } | ||
| throw new ClassNotFoundException(name + " from [Module \"deployment.repro.war\" from Service Module Loader]"); | ||
| } | ||
|
|
||
| Class<?> findLoaded(String name) { | ||
| return findLoadedClass(name); | ||
| } | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.