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

Introduce CompilationException::getDiagnosticCode #31532

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,42 @@

package org.springframework.core.test.tools;

import javax.tools.Diagnostic;

import org.springframework.lang.Nullable;

/**
* Exception thrown when code cannot compile.
*
* @author Phillip Webb
* @author Yanming Zhou
* @since 6.0
*/
@SuppressWarnings("serial")
public class CompilationException extends RuntimeException {

@Nullable
private final String diagnosticCode;

CompilationException(String errors, SourceFiles sourceFiles, ResourceFiles resourceFiles) {
CompilationException(@Nullable String diagnosticCode, String errors, SourceFiles sourceFiles, ResourceFiles resourceFiles) {
super(buildMessage(errors, sourceFiles, resourceFiles));
this.diagnosticCode = diagnosticCode;
}

CompilationException(String errors, SourceFiles sourceFiles, ResourceFiles resourceFiles) {
this(null, errors, sourceFiles, resourceFiles);
}

/**
* Return the diagnostic code reported by compiler.
* @return diagnostic code reported by compiler, might be null.
* @since 6.1
* @see Diagnostic#getCode()
*/
@Nullable
public String getDiagnosticCode() {
return this.diagnosticCode;
}

private static String buildMessage(String errors, SourceFiles sourceFiles,
ResourceFiles resourceFiles) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
* @author Phillip Webb
* @author Scott Frederick
* @author Stephane Nicoll
* @author Yanming Zhou
* @since 6.0
* @see #forSystem()
*/
Expand Down Expand Up @@ -336,7 +337,7 @@ private DynamicClassLoader compile() {
}
boolean result = task.call();
if (!result || errors.hasReportedErrors()) {
throw new CompilationException(errors.toString(), this.sourceFiles, this.resourceFiles);
throw new CompilationException(errors.getDiagnosticCode(), errors.toString(), this.sourceFiles, this.resourceFiles);
}
}
return new DynamicClassLoader(classLoaderToUse, this.classFiles, this.resourceFiles,
Expand Down Expand Up @@ -373,13 +374,17 @@ static class Errors implements DiagnosticListener<JavaFileObject> {

private final StringBuilder message = new StringBuilder();

@Nullable
private String diagnosticCode;

Errors(Locale locale) {
this.locale = locale;
}

@Override
public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
if (diagnostic.getKind() == Diagnostic.Kind.ERROR) {
this.diagnosticCode = diagnostic.getCode();
this.message.append('\n');
this.message.append(diagnostic.getMessage(this.locale));
if (diagnostic.getSource() != null) {
Expand All @@ -396,6 +401,11 @@ boolean hasReportedErrors() {
return !this.message.isEmpty();
}

@Nullable
public String getDiagnosticCode() {
return this.diagnosticCode;
}

@Override
public String toString() {
return this.message.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
* @author Andy Wilkinson
* @author Scott Frederick
* @author Stephane Nicoll
* @author Yanming Zhou
*/
class TestCompilerTests {

Expand Down Expand Up @@ -173,7 +174,7 @@ public static void main(String[] args) {
assertThatExceptionOfType(CompilationException.class).isThrownBy(
() -> TestCompiler.forSystem().failOnWarning().withLocale(Locale.ENGLISH)
.withSources(SourceFile.of(HELLO_DEPRECATED), main).compile(compiled -> {
})).withMessageContaining("warnings found and -Werror specified");
})).extracting(CompilationException::getDiagnosticCode).isEqualTo("compiler.err.warnings.and.werror");
}

@Test
Expand Down