Skip to content
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

Report compilation errors in exception #142

Open
wants to merge 2 commits into
base: ea
Choose a base branch
from

Conversation

Marcono1234
Copy link
Contributor

@Marcono1234 Marcono1234 commented Jul 21, 2024

This pull request is based on #141 for simplicity; but if you want I can try to base it on the ea branch instead.

Relates to #35

Currently when you call CachedCompiler#loadFromJava with code containing compilation errors, you just get a ClassNotFoundException without any details. And depending on whether you used a custom PrinterWriter you then either have to inspect its output or System.err console output to understand the actual case.
This can be rather confusing and makes troubleshooting more difficult.

This pull request here collects Error diagnostics and in case of a ClassNotFoundException includes those in the exception message.

Comment on lines +420 to +431
public void test_compilerErrorsButLoadingDifferentClass() throws Exception {
// quieten the compiler output
PrintWriter quietWriter = new PrintWriter(new StringWriter());

// TODO: Should this throw an exception due to the compilation error nonetheless to be less
// error-prone for users, even if loading class would succeed?
Class<?> testClass = compiler.loadFromJava(classLoader,
// Load other class which is unaffected by compilation error
String.class.getName(),
"clazz X {}", quietWriter);
assertSame(String.class, testClass);
}
Copy link
Contributor Author

@Marcono1234 Marcono1234 Jul 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Open question: This mirrors the existing behavior, but personally I think it would be cleaner to always have CachedCompiler throw an exception (ClassNotFoundException?) when compilation errors occur. Even if loading the class would succeed in the end, because that class was unaffected by the compilation errors.

@tgd tgd self-requested a review September 27, 2024 18:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant