Skip to content

Commit cffb0a2

Browse files
committed
GH-587 - Re-establish proper ApplicationModules instance caching.
The previous change unfortunately altered the CacheKey type in a way that it does not result in equal instances for the same set of parameters, primarily because SingletonSupplier does not implement equals(…)/hashCode(). We now resort to include the ModulithMetadata source in the equals(…) / hashCode() calculation rather than the ModulithMetadata instance itself. This also avoids annotation processing during CacheKey instance creation.
1 parent b83119e commit cffb0a2

File tree

1 file changed

+28
-13
lines changed

1 file changed

+28
-13
lines changed

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

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -611,19 +611,22 @@ private Stream<ApplicationModule> allModules() {
611611
* @param key must not be {@literal null}.
612612
* @return will never be {@literal null}.
613613
*/
614-
private static ApplicationModules of(CacheKey key) {
614+
private static ApplicationModules of(CacheKey cacheKey) {
615615

616-
Assert.notNull(key, "Cache key must not be null!");
616+
Assert.notNull(cacheKey, "Cache key must not be null!");
617617

618-
var metadata = key.getMetadata();
619-
var modules = new ApplicationModules(metadata, key.getIgnored(),
620-
metadata.useFullyQualifiedModuleNames(), key.getOptions());
618+
return CACHE.computeIfAbsent(cacheKey, key -> {
621619

622-
var sharedModules = metadata.getSharedModuleNames() //
623-
.map(modules::getRequiredModule) //
624-
.collect(Collectors.toSet());
620+
var metadata = key.getMetadata();
621+
var modules = new ApplicationModules(metadata, key.getIgnored(),
622+
metadata.useFullyQualifiedModuleNames(), key.getOptions());
625623

626-
return modules.withSharedModules(sharedModules);
624+
var sharedModules = metadata.getSharedModuleNames() //
625+
.map(modules::getRequiredModule) //
626+
.collect(Collectors.toSet());
627+
628+
return modules.withSharedModules(sharedModules);
629+
});
627630
}
628631

629632
/**
@@ -667,21 +670,24 @@ private static class CacheKey {
667670

668671
private final DescribedPredicate<JavaClass> ignored;
669672
private final ImportOption options;
673+
private final Object metadataSource;
670674
private final Supplier<ModulithMetadata> metadata;
671675

672-
public CacheKey(DescribedPredicate<JavaClass> ignored, ImportOption options, Supplier<ModulithMetadata> metadata) {
676+
public CacheKey(DescribedPredicate<JavaClass> ignored, ImportOption options, Object metadataSource,
677+
Supplier<ModulithMetadata> metadata) {
673678

674679
this.ignored = ignored;
675680
this.options = options;
681+
this.metadataSource = metadataSource;
676682
this.metadata = SingletonSupplier.of(metadata);
677683
}
678684

679685
static CacheKey of(String pkg, DescribedPredicate<JavaClass> ignored, ImportOption options) {
680-
return new CacheKey(ignored, options, () -> ModulithMetadata.of(pkg));
686+
return new CacheKey(ignored, options, pkg, () -> ModulithMetadata.of(pkg));
681687
}
682688

683689
static CacheKey of(Class<?> type, DescribedPredicate<JavaClass> ignored, ImportOption options) {
684-
return new CacheKey(ignored, options, () -> ModulithMetadata.of(type));
690+
return new CacheKey(ignored, options, type, () -> ModulithMetadata.of(type));
685691
}
686692

687693
DescribedPredicate<JavaClass> getIgnored() {
@@ -713,7 +719,16 @@ public boolean equals(Object obj) {
713719

714720
return Objects.equals(this.ignored, that.ignored)
715721
&& Objects.equals(this.options, that.options)
716-
&& Objects.equals(this.metadata.get(), that.metadata.get());
722+
&& Objects.equals(this.metadataSource, that.metadataSource);
723+
}
724+
725+
/*
726+
* (non-Javadoc)
727+
* @see java.lang.Object#hashCode()
728+
*/
729+
@Override
730+
public int hashCode() {
731+
return Objects.hash(ignored, options, metadataSource);
717732
}
718733
}
719734

0 commit comments

Comments
 (0)