-
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
Open
sam-ross
wants to merge
2
commits into
develop
Choose a base branch
from
feature/CORE-57-surface-javac-diagnostics
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+251
−22
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
136 changes: 136 additions & 0 deletions
136
src/test/java/net/openhft/compiler/CachedCompilerModuleClassLoaderReproTest.java
|
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. Please rename this file to something meaningful |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should this be
loadedClasses.put(className, clazz = classLoader.loadClass(className));?