Skip to content

Commit d0ffcdd

Browse files
committed
GH-157 - Improve aggregate lookup.
We now directly collect all aggregate types instead of looking up entities first.
1 parent 6e16cba commit d0ffcdd

File tree

3 files changed

+52
-11
lines changed

3 files changed

+52
-11
lines changed

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

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public class ApplicationModule {
7272
private final boolean useFullyQualifiedModuleNames;
7373

7474
private final Supplier<Classes> springBeans;
75-
private final Supplier<Classes> entities;
75+
private final Supplier<Classes> aggregateRoots;
7676
private final Supplier<List<JavaClass>> valueTypes;
7777
private final Supplier<List<EventType>> publishedEvents;
7878

@@ -90,7 +90,7 @@ public class ApplicationModule {
9090
this.useFullyQualifiedModuleNames = useFullyQualifiedModuleNames;
9191

9292
this.springBeans = Suppliers.memoize(() -> filterSpringBeans(basePackage));
93-
this.entities = Suppliers.memoize(() -> findEntities(basePackage));
93+
this.aggregateRoots = Suppliers.memoize(() -> findAggregateRoots(basePackage));
9494
this.valueTypes = Suppliers
9595
.memoize(() -> findArchitecturallyEvidentType(ArchitecturallyEvidentType::isValueObject));
9696
this.publishedEvents = Suppliers.memoize(() -> findPublishedEvents());
@@ -195,10 +195,7 @@ public List<JavaClass> getValueTypes() {
195195
*/
196196
public List<JavaClass> getAggregateRoots() {
197197

198-
return entities.get().stream() //
199-
.map(it -> ArchitecturallyEvidentType.of(it, getSpringBeansInternal())) //
200-
.filter(ArchitecturallyEvidentType::isAggregateRoot) //
201-
.map(ArchitecturallyEvidentType::getType) //
198+
return aggregateRoots.get().stream() //
202199
.flatMap(this::resolveModuleSuperTypes) //
203200
.distinct() //
204201
.toList();
@@ -433,7 +430,7 @@ public boolean equals(Object obj) {
433430
}
434431

435432
return Objects.equals(this.basePackage, that.basePackage) //
436-
&& Objects.equals(this.entities, that.entities) //
433+
&& Objects.equals(this.aggregateRoots, that.aggregateRoots) //
437434
&& Objects.equals(this.information, that.information) //
438435
&& Objects.equals(this.namedInterfaces, that.namedInterfaces) //
439436
&& Objects.equals(this.publishedEvents, that.publishedEvents) //
@@ -448,7 +445,7 @@ public boolean equals(Object obj) {
448445
*/
449446
@Override
450447
public int hashCode() {
451-
return Objects.hash(basePackage, entities, information, namedInterfaces, publishedEvents, springBeans,
448+
return Objects.hash(basePackage, aggregateRoots, information, namedInterfaces, publishedEvents, springBeans,
452449
useFullyQualifiedModuleNames, valueTypes);
453450
}
454451

@@ -532,12 +529,13 @@ private boolean isDependencyToOtherModule(JavaClass dependency, ApplicationModul
532529
return modules.contains(dependency) && !contains(dependency);
533530
}
534531

535-
private Classes findEntities(JavaPackage source) {
532+
private Classes findAggregateRoots(JavaPackage source) {
536533

537534
return source.stream() //
538535
.map(it -> ArchitecturallyEvidentType.of(it, getSpringBeansInternal()))
539-
.filter(ArchitecturallyEvidentType::isEntity) //
540-
.map(ArchitecturallyEvidentType::getType).collect(Classes.toClasses());
536+
.filter(ArchitecturallyEvidentType::isAggregateRoot) //
537+
.map(ArchitecturallyEvidentType::getType) //
538+
.collect(Classes.toClasses());
541539
}
542540

543541
private static Classes filterSpringBeans(JavaPackage source) {
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright 2023 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 com.acme.withatbean;
17+
18+
import org.jmolecules.ddd.types.AggregateRoot;
19+
import org.jmolecules.ddd.types.Identifier;
20+
21+
import com.acme.withatbean.SampleAggregate.SampleIdentifier;
22+
23+
/**
24+
* @author Oliver Drotbohm
25+
*/
26+
public class SampleAggregate implements AggregateRoot<SampleAggregate, SampleIdentifier> {
27+
28+
@Override
29+
public SampleIdentifier getId() {
30+
return null;
31+
}
32+
33+
record SampleIdentifier() implements Identifier {}
34+
}

spring-modulith-core/src/test/java/org/springframework/modulith/core/ModuleUnitTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.junit.jupiter.api.TestInstance;
2626
import org.junit.jupiter.api.TestInstance.Lifecycle;
2727

28+
import com.acme.withatbean.SampleAggregate;
2829
import com.acme.withatbean.TestEvents.JMoleculesAnnotated;
2930
import com.acme.withatbean.TestEvents.JMoleculesImplementing;
3031
import com.tngtech.archunit.core.domain.JavaClass;
@@ -74,4 +75,12 @@ void discoversPublishedEvents() {
7475
void usesCapitalizedNameAsDisplayNameByDefault() {
7576
assertThat(module.getDisplayName()).isEqualTo("Withatbean");
7677
}
78+
79+
@Test // GH-157
80+
void detectsAggregates() {
81+
82+
assertThat(module.getAggregateRoots())
83+
.<Class<?>> extracting(JavaClass::reflect)
84+
.containsExactly(SampleAggregate.class);
85+
}
7786
}

0 commit comments

Comments
 (0)