Skip to content

Commit 2cd51c8

Browse files
committed
GH-765 - Prevent ApplicationModuleInformation from picking up info from nested packages.
1 parent 41bd816 commit 2cd51c8

File tree

5 files changed

+70
-10
lines changed

5 files changed

+70
-10
lines changed

spring-modulith-core/src/main/java/org/springframework/modulith/core/ApplicationModuleInformation.java

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626
import org.jmolecules.ddd.annotation.Module;
2727
import org.springframework.modulith.ApplicationModule;
2828
import org.springframework.modulith.ApplicationModule.Type;
29+
import org.springframework.modulith.core.Types.JMoleculesTypes;
2930
import org.springframework.util.Assert;
30-
import org.springframework.util.ClassUtils;
3131
import org.springframework.util.StringUtils;
3232

3333
import com.tngtech.archunit.core.domain.JavaClass;
@@ -48,15 +48,11 @@ interface ApplicationModuleInformation {
4848
*/
4949
public static ApplicationModuleInformation of(JavaPackage javaPackage) {
5050

51-
var lookup = AnnotationLookup.of(javaPackage, __ -> true);
51+
var lookup = AnnotationLookup.of(javaPackage.toSingle(), __ -> true);
5252

53-
if (ClassUtils.isPresent("org.jmolecules.ddd.annotation.Module",
54-
ApplicationModuleInformation.class.getClassLoader())
55-
&& JMoleculesModule.supports(lookup)) {
56-
return new JMoleculesModule(lookup);
57-
}
58-
59-
return new SpringModulithModule(lookup);
53+
return JMoleculesTypes.isPresent() && JMoleculesModule.supports(lookup)
54+
? new JMoleculesModule(lookup)
55+
: new SpringModulithModule(lookup);
6056
}
6157

6258
/**

spring-modulith-core/src/main/java/org/springframework/modulith/core/Types.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,14 @@ static class JMoleculesTypes {
5151
private static final String ARCHUNIT_RULES = BASE_PACKAGE + ".archunit.JMoleculesDddRules";
5252
private static final String MODULE = ANNOTATION_PACKAGE + ".Module";
5353

54+
private static final boolean PRESENT = ClassUtils.isPresent(AT_ENTITY, JMoleculesTypes.class.getClassLoader());
55+
5456
static final String AT_DOMAIN_EVENT_HANDLER = BASE_PACKAGE + ".event.annotation.DomainEventHandler";
5557
static final String AT_DOMAIN_EVENT = BASE_PACKAGE + ".event.annotation.DomainEvent";
5658
static final String DOMAIN_EVENT = BASE_PACKAGE + ".event.types.DomainEvent";
5759

5860
public static boolean isPresent() {
59-
return ClassUtils.isPresent(AT_ENTITY, JMoleculesTypes.class.getClassLoader());
61+
return PRESENT;
6062
}
6163

6264
@Nullable
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright 2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.modulith.core;
17+
18+
import static org.assertj.core.api.Assertions.*;
19+
20+
import reproducers.gh764.Entry;
21+
22+
import org.junit.jupiter.api.Test;
23+
24+
/**
25+
* Unit tests for {@link ApplicationModuleInformation}.
26+
*
27+
* @author Oliver Drotbohm
28+
*/
29+
class ApplicationModuleInformationUnitTests {
30+
31+
@Test // GH-764
32+
void doesNotConsiderAnnotationOnNestedPackageInfos() {
33+
34+
var pkg = TestUtils.getPackage(Entry.class);
35+
var info = ApplicationModuleInformation.of(pkg);
36+
37+
assertThat(info.getDisplayName()).isEmpty();
38+
}
39+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* Copyright 2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package reproducers.gh764;
17+
18+
/**
19+
* @author Oliver Drotbohm
20+
*/
21+
public class Entry {}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
@org.springframework.modulith.ApplicationModule(displayName = "Nested")
2+
package reproducers.gh764.nested;

0 commit comments

Comments
 (0)