diff --git a/spring-modulith-core/src/main/java/org/springframework/modulith/core/ModulithMetadata.java b/spring-modulith-core/src/main/java/org/springframework/modulith/core/ModulithMetadata.java index 8739f597..ab67df27 100644 --- a/spring-modulith-core/src/main/java/org/springframework/modulith/core/ModulithMetadata.java +++ b/spring-modulith-core/src/main/java/org/springframework/modulith/core/ModulithMetadata.java @@ -17,7 +17,6 @@ package org.springframework.modulith.core; import java.util.List; import java.util.Optional; -import java.util.function.Supplier; import java.util.stream.Stream; import org.springframework.modulith.Modulith; @@ -33,26 +32,24 @@ import org.springframework.util.Assert; public interface ModulithMetadata { static final String ANNOTATION_MISSING = "Modules can only be retrieved from a root type, but %s is not annotated with either @%s, @%s or @%s!"; + static final String WITH_ANNOTATIONS = ANNOTATION_MISSING.formatted("%s", Modulith.class.getSimpleName(), + Modulithic.class.getSimpleName(), SpringTypes.AT_SPRING_BOOT_APPLICATION); /** * Creates a new {@link ModulithMetadata} for the given annotated type. Expects the type either be annotated with * {@link Modulith}, {@link Modulithic} or {@link org.springframework.boot.autoconfigure.SpringBootApplication}. * * @param annotated must not be {@literal null}. - * @return + * @return will never be {@literal null}. * @throws IllegalArgumentException in case none of the above mentioned annotations is present on the given type. */ public static ModulithMetadata of(Class annotated) { Assert.notNull(annotated, "Annotated type must not be null!"); - Supplier exception = () -> new IllegalArgumentException( - String.format(ANNOTATION_MISSING, annotated.getSimpleName(), Modulith.class.getSimpleName(), - Modulithic.class.getSimpleName(), SpringTypes.AT_SPRING_BOOT_APPLICATION)); - - Supplier withDefaults = () -> SpringBootModulithMetadata.of(annotated).orElseThrow(exception); - - return AnnotationModulithMetadata.of(annotated).orElseGet(withDefaults); + return AnnotationModulithMetadata.of(annotated) + .or(() -> SpringBootModulithMetadata.of(annotated)) + .orElseThrow(() -> new IllegalArgumentException(WITH_ANNOTATIONS.formatted(annotated.getSimpleName()))); } /** diff --git a/spring-modulith-core/src/test/java/org/springframework/modulith/core/ModulithMetadataUnitTest.java b/spring-modulith-core/src/test/java/org/springframework/modulith/core/ModulithMetadataUnitTest.java index ec97c270..4f9f8461 100644 --- a/spring-modulith-core/src/test/java/org/springframework/modulith/core/ModulithMetadataUnitTest.java +++ b/spring-modulith-core/src/test/java/org/springframework/modulith/core/ModulithMetadataUnitTest.java @@ -32,7 +32,7 @@ import org.springframework.modulith.Modulithic; class ModulithMetadataUnitTest { @Test - public void inspectsModulithAnnotation() throws Exception { + void inspectsModulithAnnotation() throws Exception { Stream.of(ModulithAnnotated.class, ModuliticAnnotated.class) // .map(ModulithMetadata::of) // @@ -46,7 +46,7 @@ class ModulithMetadataUnitTest { } @Test - public void usesDefaultsIfModulithAnnotationsAreMissing() { + void usesDefaultsIfModulithAnnotationsAreMissing() { ModulithMetadata metadata = ModulithMetadata.of(SpringBootApplicationAnnotated.class); @@ -57,10 +57,11 @@ class ModulithMetadataUnitTest { } @Test - public void rejectsTypeNotAnnotatedWithEitherModulithAnnotationOrSpringBootApplication() { + void rejectsTypeNotAnnotatedWithEitherModulithAnnotationOrSpringBootApplication() { assertThatExceptionOfType(IllegalArgumentException.class) // .isThrownBy(() -> ModulithMetadata.of(Unannotated.class)) // + .withMessageContaining(Unannotated.class.getSimpleName()) // .withMessageContaining(Modulith.class.getSimpleName()) // .withMessageContaining(Modulithic.class.getSimpleName()) // .withMessageContaining(SpringBootApplication.class.getSimpleName());