Skip to content

Commit

Permalink
Replace noFallback property with SuppressWarnings annotation
Browse files Browse the repository at this point in the history
  • Loading branch information
charphi committed Feb 5, 2024
1 parent cebe9a8 commit cb0bc8a
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Add mustache templating in loader and batch names [#320](https://github.com/nbbrd/java-service-util/issues/320)

### Changed

- Replace noFallback property with SuppressWarnings annotation [#321](https://github.com/nbbrd/java-service-util/issues/321)

## [1.8.0] - 2024-01-31

### Added
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,9 @@
* Specifies if fallback class is unexpected.
*
* @return true if fallback class is expected, false otherwise
* @deprecated Use {@link #SINGLE_FALLBACK_NOT_EXPECTED} instead
*/
@Deprecated
boolean noFallback() default false;

/**
Expand Down Expand Up @@ -227,4 +229,9 @@ public void accept(Iterable serviceLoader) {
((ServiceLoader) serviceLoader).reload();
}
}

/**
* Name to suppress single-fallback warning using @{@link SuppressWarnings}
*/
String SINGLE_FALLBACK_NOT_EXPECTED = "SingleFallbackNotExpected";
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,7 @@
import javax.lang.model.util.Types;
import javax.tools.Diagnostic;
import java.io.IOException;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import java.util.*;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

Expand Down Expand Up @@ -168,7 +165,7 @@ public boolean checkDefinition(LoadDefinition definition) {
Types types = env.getTypeUtils();
TypeElement service = env.asTypeElement(definition.getServiceType());

if (!checkFallback(definition.getQuantifier(), definition.getFallback(), definition.isNoFallback(), service, types)) {
if (!checkFallback(definition.getQuantifier(), definition.getFallback(), definition.isNoFallback() || isSingleFallbackNotExpected(service), service, types)) {
return false;
}
if (!checkWrapper(definition.getWrapper(), service, types)) {
Expand Down Expand Up @@ -347,4 +344,9 @@ private static boolean isValidPattern(String value) {
return false;
}
}

private static boolean isSingleFallbackNotExpected(TypeElement service) {
SuppressWarnings annotation = service.getAnnotation(SuppressWarnings.class);
return annotation != null && Arrays.asList(annotation.value()).contains(ServiceDefinition.SINGLE_FALLBACK_NOT_EXPECTED);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -775,6 +775,20 @@ public void testUnexpected() {
.singleElement()
.has(sourceFileNamed("definition", "TestFallbackUnexpectedLoader.java"));
}

@Test
public void testSuppressWarning() {
JavaFileObject file = forResource("definition/TestFallbackSuppressWarning.java");
Compilation compilation = compile(file);

assertThat(compilation)
.has(succeededWithoutWarnings());

assertThat(compilation)
.extracting(Compilation::generatedSourceFiles, JAVA_FILE_OBJECTS)
.singleElement()
.has(sourceFileNamed("definition", "TestFallbackSuppressWarningLoader.java"));
}
}

@Nested
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package definition;

import static nbbrd.service.Quantifier.*;
import nbbrd.service.ServiceDefinition;

@SuppressWarnings(ServiceDefinition.SINGLE_FALLBACK_NOT_EXPECTED)
@ServiceDefinition(quantifier = SINGLE)
interface TestFallbackSuppressWarning {

}

0 comments on commit cb0bc8a

Please sign in to comment.