From e7f2966617022e09c2da3e535c8bf08d079ab9fd Mon Sep 17 00:00:00 2001 From: Oliver Drotbohm Date: Tue, 18 Jun 2024 13:57:31 +0200 Subject: [PATCH] GH-669 - Fix named interface detection in case of nested packages. If named interfaces are declared in nested packages, the name detection might accidentally pick up the names declared in child packages. We now deliberately only inspect the base package. --- .../modulith/core/NamedInterface.java | 5 ++- .../java/reproducers/gh650/Gh650Tests.java | 38 +++++++++++++++++++ .../first/persistence/SomeRepository.java | 21 ++++++++++ .../gh650/first/persistence/dto/SomeDto.java | 21 ++++++++++ .../first/persistence/dto/package-info.java | 2 + .../gh650/first/persistence/package-info.java | 2 + 6 files changed, 87 insertions(+), 2 deletions(-) create mode 100644 spring-modulith-core/src/test/java/reproducers/gh650/Gh650Tests.java create mode 100644 spring-modulith-core/src/test/java/reproducers/gh650/first/persistence/SomeRepository.java create mode 100644 spring-modulith-core/src/test/java/reproducers/gh650/first/persistence/dto/SomeDto.java create mode 100644 spring-modulith-core/src/test/java/reproducers/gh650/first/persistence/dto/package-info.java create mode 100644 spring-modulith-core/src/test/java/reproducers/gh650/first/persistence/package-info.java diff --git a/spring-modulith-core/src/main/java/org/springframework/modulith/core/NamedInterface.java b/spring-modulith-core/src/main/java/org/springframework/modulith/core/NamedInterface.java index 05b3b7f3..c084a4cd 100644 --- a/spring-modulith-core/src/main/java/org/springframework/modulith/core/NamedInterface.java +++ b/spring-modulith-core/src/main/java/org/springframework/modulith/core/NamedInterface.java @@ -69,12 +69,13 @@ public class NamedInterface implements Iterable { */ static List of(JavaPackage javaPackage) { - var names = javaPackage.getAnnotation(org.springframework.modulith.NamedInterface.class) // + var basePackage = javaPackage.toSingle(); + var names = basePackage.getAnnotation(org.springframework.modulith.NamedInterface.class) // .map(it -> getDefaultedNames(it, javaPackage.getName())) // .orElseThrow(() -> new IllegalArgumentException( String.format("Couldn't find NamedInterface annotation on package %s!", javaPackage))); - var classes = javaPackage.toSingle().getExposedClasses(); + var classes = basePackage.getExposedClasses(); return names.stream() . map(it -> new NamedInterface(it, classes)) // diff --git a/spring-modulith-core/src/test/java/reproducers/gh650/Gh650Tests.java b/spring-modulith-core/src/test/java/reproducers/gh650/Gh650Tests.java new file mode 100644 index 00000000..f43cd0e6 --- /dev/null +++ b/spring-modulith-core/src/test/java/reproducers/gh650/Gh650Tests.java @@ -0,0 +1,38 @@ +/* + * Copyright 2024 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package reproducers.gh650; + +import static org.assertj.core.api.Assertions.*; + +import org.junit.jupiter.api.Test; +import org.springframework.modulith.core.NamedInterface; +import org.springframework.modulith.core.TestUtils; + +/** + * @author Oliver Drotbohm + */ +public class Gh650Tests { + + @Test // GH-650 + void usesBasePackageNamedInterface() { + + var module = TestUtils.of("reproducers.gh650").getModuleByName("first").orElseThrow(); + + assertThat(module.getNamedInterfaces()) + .extracting(NamedInterface::getName) + .containsExactlyInAnyOrder("<>", "persistence", "persistence.dto"); + } +} diff --git a/spring-modulith-core/src/test/java/reproducers/gh650/first/persistence/SomeRepository.java b/spring-modulith-core/src/test/java/reproducers/gh650/first/persistence/SomeRepository.java new file mode 100644 index 00000000..a5248aeb --- /dev/null +++ b/spring-modulith-core/src/test/java/reproducers/gh650/first/persistence/SomeRepository.java @@ -0,0 +1,21 @@ +/* + * Copyright 2024 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package reproducers.gh650.first.persistence; + +/** + * @author Oliver Drotbohm + */ +public interface SomeRepository {} diff --git a/spring-modulith-core/src/test/java/reproducers/gh650/first/persistence/dto/SomeDto.java b/spring-modulith-core/src/test/java/reproducers/gh650/first/persistence/dto/SomeDto.java new file mode 100644 index 00000000..b782528a --- /dev/null +++ b/spring-modulith-core/src/test/java/reproducers/gh650/first/persistence/dto/SomeDto.java @@ -0,0 +1,21 @@ +/* + * Copyright 2024 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package reproducers.gh650.first.persistence.dto; + +/** + * @author Oliver Drotbohm + */ +public class SomeDto {} diff --git a/spring-modulith-core/src/test/java/reproducers/gh650/first/persistence/dto/package-info.java b/spring-modulith-core/src/test/java/reproducers/gh650/first/persistence/dto/package-info.java new file mode 100644 index 00000000..aacb7ff0 --- /dev/null +++ b/spring-modulith-core/src/test/java/reproducers/gh650/first/persistence/dto/package-info.java @@ -0,0 +1,2 @@ +@org.springframework.modulith.NamedInterface("persistence.dto") +package reproducers.gh650.first.persistence.dto; diff --git a/spring-modulith-core/src/test/java/reproducers/gh650/first/persistence/package-info.java b/spring-modulith-core/src/test/java/reproducers/gh650/first/persistence/package-info.java new file mode 100644 index 00000000..3c78bc13 --- /dev/null +++ b/spring-modulith-core/src/test/java/reproducers/gh650/first/persistence/package-info.java @@ -0,0 +1,2 @@ +@org.springframework.modulith.NamedInterface +package reproducers.gh650.first.persistence;