From 14f771ca13c5e4653a08574fc5b673b14817b723 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Tue, 21 Jan 2025 02:51:06 +0000 Subject: [PATCH] Fix the compiler task status reporting for annotation processor (#17063) Signed-off-by: Andriy Redko (cherry picked from commit 6e3d710ec9480c42073d9aabb30c404e606176d0) Signed-off-by: github-actions[bot] --- .../ApiAnnotationProcessorTests.java | 211 +++++++++++------- .../annotation/processor/CompilerSupport.java | 40 ++-- 2 files changed, 159 insertions(+), 92 deletions(-) diff --git a/libs/common/src/test/java/org/opensearch/common/annotation/processor/ApiAnnotationProcessorTests.java b/libs/common/src/test/java/org/opensearch/common/annotation/processor/ApiAnnotationProcessorTests.java index 716dcc3b9015f..944b29c139160 100644 --- a/libs/common/src/test/java/org/opensearch/common/annotation/processor/ApiAnnotationProcessorTests.java +++ b/libs/common/src/test/java/org/opensearch/common/annotation/processor/ApiAnnotationProcessorTests.java @@ -8,8 +8,11 @@ package org.opensearch.common.annotation.processor; +import org.opensearch.common.SuppressForbidden; import org.opensearch.common.annotation.InternalApi; import org.opensearch.test.OpenSearchTestCase; +import org.junit.Rule; +import org.junit.rules.TemporaryFolder; import javax.tools.Diagnostic; @@ -20,10 +23,14 @@ import static org.hamcrest.Matchers.hasSize; import static org.hamcrest.Matchers.instanceOf; +@SuppressForbidden(reason = "TemporaryFolder does not support Path-based APIs") @SuppressWarnings("deprecation") public class ApiAnnotationProcessorTests extends OpenSearchTestCase implements CompilerSupport { + @Rule + public final TemporaryFolder folder = new TemporaryFolder(); + public void testPublicApiMethodArgumentNotAnnotated() { - final CompilerResult result = compile("PublicApiMethodArgumentNotAnnotated.java", "NotAnnotated.java"); + final CompilerResult result = compile(folder.getRoot().toPath(), "PublicApiMethodArgumentNotAnnotated.java", "NotAnnotated.java"); assertThat(result, instanceOf(Failure.class)); final Failure failure = (Failure) result; @@ -44,7 +51,11 @@ public void testPublicApiMethodArgumentNotAnnotated() { } public void testPublicApiMethodArgumentNotAnnotatedGenerics() { - final CompilerResult result = compile("PublicApiMethodArgumentNotAnnotatedGenerics.java", "NotAnnotated.java"); + final CompilerResult result = compile( + folder.getRoot().toPath(), + "PublicApiMethodArgumentNotAnnotatedGenerics.java", + "NotAnnotated.java" + ); assertThat(result, instanceOf(Failure.class)); final Failure failure = (Failure) result; @@ -65,7 +76,11 @@ public void testPublicApiMethodArgumentNotAnnotatedGenerics() { } public void testPublicApiMethodThrowsNotAnnotated() { - final CompilerResult result = compile("PublicApiMethodThrowsNotAnnotated.java", "PublicApiAnnotated.java"); + final CompilerResult result = compile( + folder.getRoot().toPath(), + "PublicApiMethodThrowsNotAnnotated.java", + "PublicApiAnnotated.java" + ); assertThat(result, instanceOf(Failure.class)); final Failure failure = (Failure) result; @@ -86,7 +101,7 @@ public void testPublicApiMethodThrowsNotAnnotated() { } public void testPublicApiMethodArgumentNotAnnotatedPackagePrivate() { - final CompilerResult result = compile("PublicApiMethodArgumentNotAnnotatedPackagePrivate.java"); + final CompilerResult result = compile(folder.getRoot().toPath(), "PublicApiMethodArgumentNotAnnotatedPackagePrivate.java"); assertThat(result, instanceOf(Failure.class)); final Failure failure = (Failure) result; @@ -120,7 +135,7 @@ public void testPublicApiMethodArgumentNotAnnotatedPackagePrivate() { } public void testPublicApiMethodArgumentAnnotatedPackagePrivate() { - final CompilerResult result = compile("PublicApiMethodArgumentAnnotatedPackagePrivate.java"); + final CompilerResult result = compile(folder.getRoot().toPath(), "PublicApiMethodArgumentAnnotatedPackagePrivate.java"); assertThat(result, instanceOf(Failure.class)); final Failure failure = (Failure) result; @@ -141,7 +156,7 @@ public void testPublicApiMethodArgumentAnnotatedPackagePrivate() { } public void testPublicApiWithInternalApiMethod() { - final CompilerResult result = compile("PublicApiWithInternalApiMethod.java"); + final CompilerResult result = compile(folder.getRoot().toPath(), "PublicApiWithInternalApiMethod.java"); assertThat(result, instanceOf(Failure.class)); final Failure failure = (Failure) result; @@ -164,40 +179,48 @@ public void testPublicApiWithInternalApiMethod() { * The constructor arguments have relaxed semantics at the moment: those could be not annotated or be annotated as {@link InternalApi} */ public void testPublicApiConstructorArgumentNotAnnotated() { - final CompilerResult result = compile("PublicApiConstructorArgumentNotAnnotated.java", "NotAnnotated.java"); - assertThat(result, instanceOf(Failure.class)); + final CompilerResult result = compile( + folder.getRoot().toPath(), + "PublicApiConstructorArgumentNotAnnotated.java", + "NotAnnotated.java" + ); + assertThat(result, instanceOf(Success.class)); - final Failure failure = (Failure) result; - assertThat(failure.diagnotics(), hasSize(2)); + final Success success = (Success) result; + assertThat(success.diagnotics(), hasSize(2)); - assertThat(failure.diagnotics(), not(hasItem(matching(Diagnostic.Kind.ERROR)))); + assertThat(success.diagnotics(), not(hasItem(matching(Diagnostic.Kind.ERROR)))); } /** * The constructor arguments have relaxed semantics at the moment: those could be not annotated or be annotated as {@link InternalApi} */ public void testPublicApiConstructorArgumentAnnotatedInternalApi() { - final CompilerResult result = compile("PublicApiConstructorArgumentAnnotatedInternalApi.java", "InternalApiAnnotated.java"); - assertThat(result, instanceOf(Failure.class)); + final CompilerResult result = compile( + folder.getRoot().toPath(), + "PublicApiConstructorArgumentAnnotatedInternalApi.java", + "InternalApiAnnotated.java" + ); + assertThat(result, instanceOf(Success.class)); - final Failure failure = (Failure) result; - assertThat(failure.diagnotics(), hasSize(2)); + final Success success = (Success) result; + assertThat(success.diagnotics(), hasSize(2)); - assertThat(failure.diagnotics(), not(hasItem(matching(Diagnostic.Kind.ERROR)))); + assertThat(success.diagnotics(), not(hasItem(matching(Diagnostic.Kind.ERROR)))); } public void testPublicApiWithExperimentalApiMethod() { - final CompilerResult result = compile("PublicApiWithExperimentalApiMethod.java"); - assertThat(result, instanceOf(Failure.class)); + final CompilerResult result = compile(folder.getRoot().toPath(), "PublicApiWithExperimentalApiMethod.java"); + assertThat(result, instanceOf(Success.class)); - final Failure failure = (Failure) result; - assertThat(failure.diagnotics(), hasSize(2)); + final Success success = (Success) result; + assertThat(success.diagnotics(), hasSize(2)); - assertThat(failure.diagnotics(), not(hasItem(matching(Diagnostic.Kind.ERROR)))); + assertThat(success.diagnotics(), not(hasItem(matching(Diagnostic.Kind.ERROR)))); } public void testPublicApiMethodReturnNotAnnotated() { - final CompilerResult result = compile("PublicApiMethodReturnNotAnnotated.java", "NotAnnotated.java"); + final CompilerResult result = compile(folder.getRoot().toPath(), "PublicApiMethodReturnNotAnnotated.java", "NotAnnotated.java"); assertThat(result, instanceOf(Failure.class)); final Failure failure = (Failure) result; @@ -218,7 +241,11 @@ public void testPublicApiMethodReturnNotAnnotated() { } public void testPublicApiMethodReturnNotAnnotatedGenerics() { - final CompilerResult result = compile("PublicApiMethodReturnNotAnnotatedGenerics.java", "NotAnnotated.java"); + final CompilerResult result = compile( + folder.getRoot().toPath(), + "PublicApiMethodReturnNotAnnotatedGenerics.java", + "NotAnnotated.java" + ); assertThat(result, instanceOf(Failure.class)); final Failure failure = (Failure) result; @@ -239,7 +266,11 @@ public void testPublicApiMethodReturnNotAnnotatedGenerics() { } public void testPublicApiMethodReturnNotAnnotatedArray() { - final CompilerResult result = compile("PublicApiMethodReturnNotAnnotatedArray.java", "NotAnnotated.java"); + final CompilerResult result = compile( + folder.getRoot().toPath(), + "PublicApiMethodReturnNotAnnotatedArray.java", + "NotAnnotated.java" + ); assertThat(result, instanceOf(Failure.class)); final Failure failure = (Failure) result; @@ -260,7 +291,11 @@ public void testPublicApiMethodReturnNotAnnotatedArray() { } public void testPublicApiMethodReturnNotAnnotatedBoundedGenerics() { - final CompilerResult result = compile("PublicApiMethodReturnNotAnnotatedBoundedGenerics.java", "NotAnnotated.java"); + final CompilerResult result = compile( + folder.getRoot().toPath(), + "PublicApiMethodReturnNotAnnotatedBoundedGenerics.java", + "NotAnnotated.java" + ); assertThat(result, instanceOf(Failure.class)); final Failure failure = (Failure) result; @@ -282,6 +317,7 @@ public void testPublicApiMethodReturnNotAnnotatedBoundedGenerics() { public void testPublicApiMethodReturnNotAnnotatedAnnotation() { final CompilerResult result = compile( + folder.getRoot().toPath(), "PublicApiMethodReturnNotAnnotatedAnnotation.java", "PublicApiAnnotated.java", "NotAnnotatedAnnotation.java" @@ -306,57 +342,57 @@ public void testPublicApiMethodReturnNotAnnotatedAnnotation() { } public void testPublicApiMethodReturnNotAnnotatedWildcardGenerics() { - final CompilerResult result = compile("PublicApiMethodReturnNotAnnotatedWildcardGenerics.java"); - assertThat(result, instanceOf(Failure.class)); + final CompilerResult result = compile(folder.getRoot().toPath(), "PublicApiMethodReturnNotAnnotatedWildcardGenerics.java"); + assertThat(result, instanceOf(Success.class)); - final Failure failure = (Failure) result; - assertThat(failure.diagnotics(), hasSize(2)); + final Success success = (Success) result; + assertThat(success.diagnotics(), hasSize(2)); - assertThat(failure.diagnotics(), not(hasItem(matching(Diagnostic.Kind.ERROR)))); + assertThat(success.diagnotics(), not(hasItem(matching(Diagnostic.Kind.ERROR)))); } public void testPublicApiWithPackagePrivateMethod() { - final CompilerResult result = compile("PublicApiWithPackagePrivateMethod.java", "NotAnnotated.java"); - assertThat(result, instanceOf(Failure.class)); + final CompilerResult result = compile(folder.getRoot().toPath(), "PublicApiWithPackagePrivateMethod.java", "NotAnnotated.java"); + assertThat(result, instanceOf(Success.class)); - final Failure failure = (Failure) result; - assertThat(failure.diagnotics(), hasSize(2)); + final Success success = (Success) result; + assertThat(success.diagnotics(), hasSize(2)); - assertThat(failure.diagnotics(), not(hasItem(matching(Diagnostic.Kind.ERROR)))); + assertThat(success.diagnotics(), not(hasItem(matching(Diagnostic.Kind.ERROR)))); } public void testPublicApiMethodReturnSelf() { - final CompilerResult result = compile("PublicApiMethodReturnSelf.java"); - assertThat(result, instanceOf(Failure.class)); + final CompilerResult result = compile(folder.getRoot().toPath(), "PublicApiMethodReturnSelf.java"); + assertThat(result, instanceOf(Success.class)); - final Failure failure = (Failure) result; - assertThat(failure.diagnotics(), hasSize(2)); + final Success success = (Success) result; + assertThat(success.diagnotics(), hasSize(2)); - assertThat(failure.diagnotics(), not(hasItem(matching(Diagnostic.Kind.ERROR)))); + assertThat(success.diagnotics(), not(hasItem(matching(Diagnostic.Kind.ERROR)))); } public void testExperimentalApiMethodReturnSelf() { - final CompilerResult result = compile("ExperimentalApiMethodReturnSelf.java"); - assertThat(result, instanceOf(Failure.class)); + final CompilerResult result = compile(folder.getRoot().toPath(), "ExperimentalApiMethodReturnSelf.java"); + assertThat(result, instanceOf(Success.class)); - final Failure failure = (Failure) result; - assertThat(failure.diagnotics(), hasSize(2)); + final Success success = (Success) result; + assertThat(success.diagnotics(), hasSize(2)); - assertThat(failure.diagnotics(), not(hasItem(matching(Diagnostic.Kind.ERROR)))); + assertThat(success.diagnotics(), not(hasItem(matching(Diagnostic.Kind.ERROR)))); } public void testDeprecatedApiMethodReturnSelf() { - final CompilerResult result = compile("DeprecatedApiMethodReturnSelf.java"); - assertThat(result, instanceOf(Failure.class)); + final CompilerResult result = compile(folder.getRoot().toPath(), "DeprecatedApiMethodReturnSelf.java"); + assertThat(result, instanceOf(Success.class)); - final Failure failure = (Failure) result; - assertThat(failure.diagnotics(), hasSize(2)); + final Success success = (Success) result; + assertThat(success.diagnotics(), hasSize(2)); - assertThat(failure.diagnotics(), not(hasItem(matching(Diagnostic.Kind.ERROR)))); + assertThat(success.diagnotics(), not(hasItem(matching(Diagnostic.Kind.ERROR)))); } public void testPublicApiPackagePrivate() { - final CompilerResult result = compile("PublicApiPackagePrivate.java"); + final CompilerResult result = compile(folder.getRoot().toPath(), "PublicApiPackagePrivate.java"); assertThat(result, instanceOf(Failure.class)); final Failure failure = (Failure) result; @@ -376,7 +412,11 @@ public void testPublicApiPackagePrivate() { } public void testPublicApiMethodGenericsArgumentNotAnnotated() { - final CompilerResult result = compile("PublicApiMethodGenericsArgumentNotAnnotated.java", "NotAnnotated.java"); + final CompilerResult result = compile( + folder.getRoot().toPath(), + "PublicApiMethodGenericsArgumentNotAnnotated.java", + "NotAnnotated.java" + ); assertThat(result, instanceOf(Failure.class)); final Failure failure = (Failure) result; @@ -397,27 +437,35 @@ public void testPublicApiMethodGenericsArgumentNotAnnotated() { } public void testPublicApiMethodReturnAnnotatedArray() { - final CompilerResult result = compile("PublicApiMethodReturnAnnotatedArray.java", "PublicApiAnnotated.java"); - assertThat(result, instanceOf(Failure.class)); + final CompilerResult result = compile( + folder.getRoot().toPath(), + "PublicApiMethodReturnAnnotatedArray.java", + "PublicApiAnnotated.java" + ); + assertThat(result, instanceOf(Success.class)); - final Failure failure = (Failure) result; - assertThat(failure.diagnotics(), hasSize(2)); + final Success success = (Success) result; + assertThat(success.diagnotics(), hasSize(2)); - assertThat(failure.diagnotics(), not(hasItem(matching(Diagnostic.Kind.ERROR)))); + assertThat(success.diagnotics(), not(hasItem(matching(Diagnostic.Kind.ERROR)))); } public void testPublicApiMethodGenericsArgumentAnnotated() { - final CompilerResult result = compile("PublicApiMethodGenericsArgumentAnnotated.java", "PublicApiAnnotated.java"); - assertThat(result, instanceOf(Failure.class)); + final CompilerResult result = compile( + folder.getRoot().toPath(), + "PublicApiMethodGenericsArgumentAnnotated.java", + "PublicApiAnnotated.java" + ); + assertThat(result, instanceOf(Success.class)); - final Failure failure = (Failure) result; - assertThat(failure.diagnotics(), hasSize(2)); + final Success success = (Success) result; + assertThat(success.diagnotics(), hasSize(2)); - assertThat(failure.diagnotics(), not(hasItem(matching(Diagnostic.Kind.ERROR)))); + assertThat(success.diagnotics(), not(hasItem(matching(Diagnostic.Kind.ERROR)))); } public void testPublicApiAnnotatedNotOpensearch() { - final CompilerResult result = compileWithPackage("org.acme", "PublicApiAnnotated.java"); + final CompilerResult result = compileWithPackage(folder.getRoot().toPath(), "org.acme", "PublicApiAnnotated.java"); assertThat(result, instanceOf(Failure.class)); final Failure failure = (Failure) result; @@ -438,6 +486,7 @@ public void testPublicApiAnnotatedNotOpensearch() { public void testPublicApiMethodReturnAnnotatedGenerics() { final CompilerResult result = compile( + folder.getRoot().toPath(), "PublicApiMethodReturnAnnotatedGenerics.java", "PublicApiAnnotated.java", "NotAnnotatedAnnotation.java" @@ -465,30 +514,34 @@ public void testPublicApiMethodReturnAnnotatedGenerics() { * The type could expose protected inner types which are still considered to be a public API when used */ public void testPublicApiWithProtectedInterface() { - final CompilerResult result = compile("PublicApiWithProtectedInterface.java"); - assertThat(result, instanceOf(Failure.class)); + final CompilerResult result = compile(folder.getRoot().toPath(), "PublicApiWithProtectedInterface.java"); + assertThat(result, instanceOf(Success.class)); - final Failure failure = (Failure) result; - assertThat(failure.diagnotics(), hasSize(2)); + final Success success = (Success) result; + assertThat(success.diagnotics(), hasSize(2)); - assertThat(failure.diagnotics(), not(hasItem(matching(Diagnostic.Kind.ERROR)))); + assertThat(success.diagnotics(), not(hasItem(matching(Diagnostic.Kind.ERROR)))); } /** * The constructor arguments have relaxed semantics at the moment: those could be not annotated or be annotated as {@link InternalApi} */ public void testPublicApiConstructorAnnotatedInternalApi() { - final CompilerResult result = compile("PublicApiConstructorAnnotatedInternalApi.java", "NotAnnotated.java"); - assertThat(result, instanceOf(Failure.class)); + final CompilerResult result = compile( + folder.getRoot().toPath(), + "PublicApiConstructorAnnotatedInternalApi.java", + "NotAnnotated.java" + ); + assertThat(result, instanceOf(Success.class)); - final Failure failure = (Failure) result; - assertThat(failure.diagnotics(), hasSize(2)); + final Success success = (Success) result; + assertThat(success.diagnotics(), hasSize(2)); - assertThat(failure.diagnotics(), not(hasItem(matching(Diagnostic.Kind.ERROR)))); + assertThat(success.diagnotics(), not(hasItem(matching(Diagnostic.Kind.ERROR)))); } public void testPublicApiUnparseableVersion() { - final CompilerResult result = compile("PublicApiAnnotatedUnparseable.java"); + final CompilerResult result = compile(folder.getRoot().toPath(), "PublicApiAnnotatedUnparseable.java"); assertThat(result, instanceOf(Failure.class)); final Failure failure = (Failure) result; @@ -508,13 +561,13 @@ public void testPublicApiUnparseableVersion() { } public void testPublicApiWithDeprecatedApiMethod() { - final CompilerResult result = compile("PublicApiWithDeprecatedApiMethod.java"); - assertThat(result, instanceOf(Failure.class)); + final CompilerResult result = compile(folder.getRoot().toPath(), "PublicApiWithDeprecatedApiMethod.java"); + assertThat(result, instanceOf(Success.class)); - final Failure failure = (Failure) result; - assertThat(failure.diagnotics(), hasSize(2)); + final Success success = (Success) result; + assertThat(success.diagnotics(), hasSize(2)); - assertThat(failure.diagnotics(), not(hasItem(matching(Diagnostic.Kind.ERROR)))); + assertThat(success.diagnotics(), not(hasItem(matching(Diagnostic.Kind.ERROR)))); } } diff --git a/libs/common/src/test/java/org/opensearch/common/annotation/processor/CompilerSupport.java b/libs/common/src/test/java/org/opensearch/common/annotation/processor/CompilerSupport.java index c8fdb3333a714..e6bde87ec9348 100644 --- a/libs/common/src/test/java/org/opensearch/common/annotation/processor/CompilerSupport.java +++ b/libs/common/src/test/java/org/opensearch/common/annotation/processor/CompilerSupport.java @@ -29,6 +29,7 @@ import java.net.URI; import java.net.URL; import java.nio.charset.StandardCharsets; +import java.nio.file.Path; import java.security.AccessController; import java.security.PrivilegedAction; import java.util.Arrays; @@ -39,12 +40,12 @@ import java.util.stream.Stream; interface CompilerSupport { - default CompilerResult compile(String name, String... names) { - return compileWithPackage(ApiAnnotationProcessorTests.class.getPackageName(), name, names); + default CompilerResult compile(Path outputDirectory, String name, String... names) { + return compileWithPackage(outputDirectory, ApiAnnotationProcessorTests.class.getPackageName(), name, names); } @SuppressWarnings("removal") - default CompilerResult compileWithPackage(String pck, String name, String... names) { + default CompilerResult compileWithPackage(Path outputDirectory, String pck, String name, String... names) { final JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); final DiagnosticCollector collector = new DiagnosticCollector<>(); @@ -54,11 +55,18 @@ default CompilerResult compileWithPackage(String pck, String name, String... nam .map(f -> asSource(pck, f)) .collect(Collectors.toList()); - final CompilationTask task = compiler.getTask(out, fileManager, collector, null, null, files); + final CompilationTask task = compiler.getTask( + out, + fileManager, + collector, + List.of("-d", outputDirectory.toString()), + null, + files + ); task.setProcessors(Collections.singleton(new ApiAnnotationProcessor())); if (AccessController.doPrivileged((PrivilegedAction) () -> task.call())) { - return new Success(); + return new Success(collector.getDiagnostics()); } else { return new Failure(collector.getDiagnostics()); } @@ -81,16 +89,10 @@ public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOExcept }; } - class CompilerResult {} - - class Success extends CompilerResult { - - } - - class Failure extends CompilerResult { + class CompilerResult { private final List> diagnotics; - Failure(List> diagnotics) { + CompilerResult(List> diagnotics) { this.diagnotics = diagnotics; } @@ -99,6 +101,18 @@ List> diagnotics() { } } + class Success extends CompilerResult { + Success(List> diagnotics) { + super(diagnotics); + } + } + + class Failure extends CompilerResult { + Failure(List> diagnotics) { + super(diagnotics); + } + } + class HasDiagnostic extends TypeSafeMatcher> { private final Diagnostic.Kind kind; private final Matcher matcher;