@@ -107,9 +107,40 @@ private static DescribedPredicate<CanBeAnnotated> getAtGenerated() {
107
107
108
108
private boolean verified ;
109
109
110
+ /**
111
+ * Creates a new {@link ApplicationModules} instance.
112
+ *
113
+ * @param metadata must not be {@literal null}.
114
+ * @param ignored must not be {@literal null}.
115
+ * @param useFullyQualifiedModuleNames can be {@literal null}.
116
+ * @param option must not be {@literal null}.
117
+ */
118
+ protected ApplicationModules (ModulithMetadata metadata ,
119
+ DescribedPredicate <JavaClass > ignored , boolean useFullyQualifiedModuleNames , ImportOption option ) {
120
+ this (metadata , metadata .getBasePackages (), ignored , useFullyQualifiedModuleNames , option );
121
+ }
122
+
123
+ /**
124
+ * Creates a new {@link ApplicationModules} instance.
125
+ *
126
+ * @param metadata must not be {@literal null}.
127
+ * @param packages must not be {@literal null}.
128
+ * @param ignored must not be {@literal null}.
129
+ * @param useFullyQualifiedModuleNames can be {@literal null}.
130
+ * @param option must not be {@literal null}.
131
+ * @deprecated since 1.2, for removal in 1.3. Use {@link ApplicationModules(ModulithMetadata, DescribedPredicate,
132
+ * boolean, ImportOption)} instead and set up {@link ModulithMetadata} to contain the packages you want to
133
+ * use.
134
+ */
135
+ @ Deprecated (forRemoval = true )
110
136
protected ApplicationModules (ModulithMetadata metadata , Collection <String > packages ,
111
137
DescribedPredicate <JavaClass > ignored , boolean useFullyQualifiedModuleNames , ImportOption option ) {
112
138
139
+ Assert .notNull (metadata , "ModulithMetadata must not be null!" );
140
+ Assert .notNull (packages , "Base packages must not be null!" );
141
+ Assert .notNull (ignored , "Ignores must not be null!" );
142
+ Assert .notNull (option , "ImportOptions must not be null!" );
143
+
113
144
this .metadata = metadata ;
114
145
this .allClasses = new ClassFileImporter () //
115
146
.withImportOption (option ) //
@@ -190,26 +221,33 @@ public static ApplicationModules of(Class<?> modulithType) {
190
221
}
191
222
192
223
/**
193
- * Creates a new {@link ApplicationModules} relative to the given modulith type, a
194
- * {@link ApplicationModuleDetectionStrategy} and a {@link DescribedPredicate} which types and packages to ignore.
195
- * Will inspect the {@link org.springframework.modulith.Modulith} and {@link org.springframework.modulith.Modulithic}
196
- * annotations on the class given for advanced customizations of the module setup.
224
+ * Creates a new {@link ApplicationModules} relative to the given modulith type, and a {@link DescribedPredicate}
225
+ * which types and packages to ignore. Will inspect the {@link org.springframework.modulith.Modulith} and
226
+ * {@link org.springframework.modulith.Modulithic} annotations on the class given for advanced customizations of the
227
+ * module setup.
197
228
*
198
229
* @param modulithType must not be {@literal null}.
199
230
* @param ignored must not be {@literal null}.
200
231
* @return will never be {@literal null}.
201
232
*/
202
233
public static ApplicationModules of (Class <?> modulithType , DescribedPredicate <JavaClass > ignored ) {
203
234
204
- CacheKey key = new TypeKey (modulithType , ignored );
235
+ Assert .notNull (modulithType , "Modulith root type must not be null!" );
236
+ Assert .notNull (ignored , "Predicate to describe ignored types must not be null!" );
205
237
206
- return CACHE .computeIfAbsent (key , it -> {
207
-
208
- Assert .notNull (modulithType , "Modulith root type must not be null!" );
209
- Assert .notNull (ignored , "Predicate to describe ignored types must not be null!" );
238
+ return of (CacheKey .of (modulithType , ignored , IMPORT_OPTION ));
239
+ }
210
240
211
- return of (key );
212
- });
241
+ /**
242
+ * Creates a new {@link ApplicationModules} instance for the given type and {@link ImportOption}.
243
+ *
244
+ * @param modulithType must not be {@literal null}.
245
+ * @param options must not be {@literal null}.
246
+ * @return will never be {@literal null}.
247
+ * @since 1.2
248
+ */
249
+ public static ApplicationModules of (Class <?> modulithType , ImportOption options ) {
250
+ return of (CacheKey .of (modulithType , alwaysFalse (), options ));
213
251
}
214
252
215
253
/**
@@ -231,15 +269,22 @@ public static ApplicationModules of(String javaPackage) {
231
269
*/
232
270
public static ApplicationModules of (String javaPackage , DescribedPredicate <JavaClass > ignored ) {
233
271
234
- CacheKey key = new PackageKey (javaPackage , ignored );
235
-
236
- return CACHE .computeIfAbsent (key , it -> {
272
+ Assert .hasText (javaPackage , "Base package must not be null or empty!" );
273
+ Assert .notNull (ignored , "Predicate to describe ignored types must not be null!" );
237
274
238
- Assert . hasText (javaPackage , "Base package must not be null or empty!" );
239
- Assert . notNull ( ignored , "Predicate to describe ignored types must not be null!" );
275
+ return of ( CacheKey . of (javaPackage , ignored , IMPORT_OPTION ) );
276
+ }
240
277
241
- return of (key );
242
- });
278
+ /**
279
+ * Creates a new {@link ApplicationModules} instance for the given package and {@link ImportOption}.
280
+ *
281
+ * @param javaPackage must not be {@literal null}.
282
+ * @param options must not be {@literal null}.
283
+ * @return will never be {@literal null}.
284
+ * @since 1.2
285
+ */
286
+ public static ApplicationModules of (String javaPackage , ImportOption options ) {
287
+ return of (CacheKey .of (javaPackage , alwaysFalse (), options ));
243
288
}
244
289
245
290
/**
@@ -571,13 +616,8 @@ private static ApplicationModules of(CacheKey key) {
571
616
Assert .notNull (key , "Cache key must not be null!" );
572
617
573
618
var metadata = key .getMetadata ();
574
-
575
- var basePackages = new HashSet <String >();
576
- basePackages .add (key .getBasePackage ());
577
- basePackages .addAll (metadata .getAdditionalPackages ());
578
-
579
- var modules = new ApplicationModules (metadata , basePackages , key .getIgnored (),
580
- metadata .useFullyQualifiedModuleNames (), IMPORT_OPTION );
619
+ var modules = new ApplicationModules (metadata , key .getIgnored (),
620
+ metadata .useFullyQualifiedModuleNames (), key .getOptions ());
581
621
582
622
var sharedModules = metadata .getSharedModuleNames () //
583
623
.map (modules ::getRequiredModule ) //
@@ -623,130 +663,37 @@ public static DescribedPredicate<JavaClass> withoutModule(String name) {
623
663
}
624
664
}
625
665
626
- private static interface CacheKey {
666
+ private static class CacheKey {
627
667
628
- String getBasePackage ();
629
-
630
- DescribedPredicate <JavaClass > getIgnored ();
631
-
632
- ModulithMetadata getMetadata ();
633
- }
634
-
635
- private static final class TypeKey implements CacheKey {
636
-
637
- private final Class <?> type ;
638
668
private final DescribedPredicate <JavaClass > ignored ;
669
+ private final ImportOption options ;
670
+ private final Supplier <ModulithMetadata > metadata ;
639
671
640
- /**
641
- * Creates a new {@link TypeKey} for the given type and {@link DescribedPredicate} of ignored {@link JavaClass}es.
642
- *
643
- * @param type must not be {@literal null}.
644
- * @param ignored must not be {@literal null}.
645
- */
646
- TypeKey (Class <?> type , DescribedPredicate <JavaClass > ignored ) {
672
+ public CacheKey (DescribedPredicate <JavaClass > ignored , ImportOption options , Supplier <ModulithMetadata > metadata ) {
647
673
648
- this .type = type ;
649
674
this .ignored = ignored ;
675
+ this .options = options ;
676
+ this .metadata = SingletonSupplier .of (metadata );
650
677
}
651
678
652
- /*
653
- * (non-Javadoc)
654
- * @see org.springframework.modulith.model.Modules.CacheKey#getBasePackage()
655
- */
656
- @ Override
657
- public String getBasePackage () {
658
- return type .getPackage ().getName ();
679
+ static CacheKey of (String pkg , DescribedPredicate <JavaClass > ignored , ImportOption options ) {
680
+ return new CacheKey (ignored , options , () -> ModulithMetadata .of (pkg ));
659
681
}
660
682
661
- /*
662
- * (non-Javadoc)
663
- * @see org.springframework.modulith.model.Modules.CacheKey#getMetadata()
664
- */
665
- @ Override
666
- public ModulithMetadata getMetadata () {
667
- return ModulithMetadata .of (type );
683
+ static CacheKey of (Class <?> type , DescribedPredicate <JavaClass > ignored , ImportOption options ) {
684
+ return new CacheKey (ignored , options , () -> ModulithMetadata .of (type ));
668
685
}
669
686
670
- /*
671
- * (non-Javadoc)
672
- * @see org.springframework.modulith.model.ApplicationModules.CacheKey#getIgnored()
673
- */
674
- @ Override
675
- public DescribedPredicate <JavaClass > getIgnored () {
687
+ DescribedPredicate <JavaClass > getIgnored () {
676
688
return ignored ;
677
689
}
678
690
679
- /*
680
- * (non-Javadoc)
681
- * @see java.lang.Object#equals(java.lang.Object)
682
- */
683
- @ Override
684
- public boolean equals (Object obj ) {
685
-
686
- if (this == obj ) {
687
- return true ;
688
- }
689
-
690
- if (!(obj instanceof TypeKey other )) {
691
- return false ;
692
- }
693
-
694
- return Objects .equals (this .type , other .type ) //
695
- && Objects .equals (this .ignored , other .ignored );
696
- }
697
-
698
- /*
699
- * (non-Javadoc)
700
- * @see java.lang.Object#hashCode()
701
- */
702
- @ Override
703
- public int hashCode () {
704
- return Objects .hash (type , ignored );
705
- }
706
- }
707
-
708
- private static final class PackageKey implements CacheKey {
709
-
710
- private final String basePackage ;
711
- private final DescribedPredicate <JavaClass > ignored ;
712
-
713
- /**
714
- * Creates a new {@link PackageKey} for the given base package and {@link DescribedPredicate} of ignored
715
- * {@link JavaClass}es.
716
- *
717
- * @param basePackage must not be {@literal null}.
718
- * @param ignored must not be {@literal null}.
719
- */
720
- PackageKey (String basePackage , DescribedPredicate <JavaClass > ignored ) {
721
-
722
- this .basePackage = basePackage ;
723
- this .ignored = ignored ;
691
+ ModulithMetadata getMetadata () {
692
+ return metadata .get ();
724
693
}
725
694
726
- /*
727
- * (non-Javadoc)
728
- * @see org.springframework.modulith.model.ApplicationModules.CacheKey#getBasePackage()
729
- */
730
- @ Override
731
- public String getBasePackage () {
732
- return basePackage ;
733
- }
734
-
735
- /*
736
- * (non-Javadoc)
737
- * @see org.springframework.modulith.model.ApplicationModules.CacheKey#getIgnored()
738
- */
739
- public DescribedPredicate <JavaClass > getIgnored () {
740
- return ignored ;
741
- }
742
-
743
- /*
744
- * (non-Javadoc)
745
- * @see org.springframework.modulith.model.Modules.CacheKey#getMetadata()
746
- */
747
- @ Override
748
- public ModulithMetadata getMetadata () {
749
- return ModulithMetadata .of (basePackage );
695
+ ImportOption getOptions () {
696
+ return options ;
750
697
}
751
698
752
699
/*
@@ -756,25 +703,17 @@ public ModulithMetadata getMetadata() {
756
703
@ Override
757
704
public boolean equals (Object obj ) {
758
705
759
- if (this == obj ) {
706
+ if (obj == this ) {
760
707
return true ;
761
708
}
762
709
763
- if (!(obj instanceof PackageKey that )) {
710
+ if (!(obj instanceof CacheKey that )) {
764
711
return false ;
765
712
}
766
713
767
- return Objects .equals (this .basePackage , that .basePackage ) //
768
- && Objects .equals (this .ignored , that .ignored );
769
- }
770
-
771
- /*
772
- * (non-Javadoc)
773
- * @see java.lang.Object#hashCode()
774
- */
775
- @ Override
776
- public int hashCode () {
777
- return Objects .hash (basePackage , ignored );
714
+ return Objects .equals (this .ignored , that .ignored )
715
+ && Objects .equals (this .options , that .options )
716
+ && Objects .equals (this .metadata .get (), that .metadata .get ());
778
717
}
779
718
}
780
719
0 commit comments