Skip to content

Commit 5b7167a

Browse files
committed
GH-1494 - Improve inspectability of JavaPackage(s) and ApplicationModule for type inclusion.
1 parent 2e6b63c commit 5b7167a

File tree

3 files changed

+113
-4
lines changed

3 files changed

+113
-4
lines changed

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,23 @@ public boolean contains(Class<?> type) {
347347
return classes.contains(type);
348348
}
349349

350+
/**
351+
* Returns whether the module could contain the type independent of the actual types backing the current instance. The
352+
* mismatch usually comes from the distinction between production and test code. A module set up from the former would
353+
* not contain a test type within its package space. This method in contrast would acknowledge that the test type
354+
* logically belongs to the module, too.
355+
*
356+
* @param type must not be {@literal null}.
357+
* @see JavaPackage#couldContain(Class)
358+
* @since 1.4.6, 2.0.1
359+
*/
360+
public boolean couldContain(Class<?> type) {
361+
362+
Assert.notNull(type, "Class<?> must not be null!");
363+
364+
return contains(type) || !exclusions.couldContain(type) && basePackage.couldContain(type);
365+
}
366+
350367
/**
351368
* Returns the {@link JavaClass} for the given candidate simple or fully-qualified type name.
352369
*

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

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,19 @@ public Classes that(DescribedPredicate<? super JavaClass> predicate) {
240240
return classes.that(predicate);
241241
}
242242

243+
/**
244+
* Return whether the {@link JavaPackage} contains the given type.
245+
*
246+
* @param type must not be {@literal null}.
247+
* @since 1.4.6, 2.0.1
248+
*/
249+
public boolean contains(Class<?> type) {
250+
251+
Assert.notNull(type, "Type must not be null!");
252+
253+
return classes.contains(type);
254+
}
255+
243256
/**
244257
* Return whether the {@link JavaPackage} contains the given type.
245258
*
@@ -264,6 +277,38 @@ public boolean contains(String typeName) {
264277
return classes.contains(typeName);
265278
}
266279

280+
/**
281+
* Returns whether the packages could contain the type independent of the actual types backing the current instance.
282+
* The mismatch usually comes from the distinction between production and test code. A {@link JavaPackage} set up from
283+
* the former would not contain a test type within the same package. This method in contrast would acknowledge that
284+
* the test type logically belongs to the package.
285+
*
286+
* @param type must not be {@literal null}.
287+
* @since 1.4.6, 2.0.1
288+
*/
289+
public boolean couldContain(Class<?> type) {
290+
291+
Assert.notNull(type, "Type must not be null!");
292+
293+
return name.contains(PackageName.ofType(type.getName()));
294+
}
295+
296+
/**
297+
* Returns whether the packages could contain the type independent of the actual types backing the current instance.
298+
* The mismatch usually comes from the distinction between production and test code. A {@link JavaPackage} set up from
299+
* the former would not contain a test type within the same package. This method in contrast would acknowledge that
300+
* the test type logically belongs to the package.
301+
*
302+
* @param type must not be {@literal null}.
303+
* @since 1.4.6, 2.0.1
304+
*/
305+
public boolean couldContain(String type) {
306+
307+
Assert.notNull(type, "Type must not be null!");
308+
309+
return name.contains(PackageName.ofType(type));
310+
}
311+
267312
/**
268313
* Returns a {@link Stream} of all {@link JavaClass}es contained in the {@link JavaPackage}.
269314
*

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

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@
2828
* A collection of {@link JavaPackage}s.
2929
*
3030
* @author Oliver Drotbohm
31-
* @since 1.3
31+
* @since 1.3, public since 1.4.6, 2.0.1
3232
*/
33-
class JavaPackages implements Iterable<JavaPackage> {
33+
public class JavaPackages implements Iterable<JavaPackage> {
3434

3535
public static JavaPackages NONE = new JavaPackages(Collections.emptyList());
3636

@@ -41,7 +41,7 @@ class JavaPackages implements Iterable<JavaPackage> {
4141
*
4242
* @param packages must not be {@literal null}.
4343
*/
44-
JavaPackages(Collection<JavaPackage> packages) {
44+
public JavaPackages(Collection<JavaPackage> packages) {
4545

4646
Assert.notNull(packages, "Packages must not be null!");
4747

@@ -75,7 +75,7 @@ JavaPackages flatten() {
7575
*
7676
* @return will never be {@literal null}.
7777
*/
78-
Stream<JavaPackage> stream() {
78+
public Stream<JavaPackage> stream() {
7979
return packages.stream();
8080
}
8181

@@ -95,6 +95,53 @@ JavaPackages getSubPackagesOf(JavaPackage pkg) {
9595
.toList());
9696
}
9797

98+
public boolean contains(JavaPackage pkg) {
99+
return packages.contains(pkg);
100+
}
101+
102+
/**
103+
* Returns whether any of the packages contains the given type.
104+
*
105+
* @param type must not be {@literal null}.
106+
* @since 1.4.6, 2.0.1
107+
*/
108+
boolean contains(Class<?> type) {
109+
110+
Assert.notNull(type, "Type must not be null!");
111+
112+
return stream().anyMatch(it -> it.contains(type));
113+
}
114+
115+
/**
116+
* Returns whether any of the the packages could contain the type independent of the actual types backing the current
117+
* instance.
118+
*
119+
* @param type must not be {@literal null}.
120+
* @see JavaPackage#couldContain(Class)
121+
* @since 1.4.6, 2.0.1
122+
*/
123+
public boolean couldContain(Class<?> type) {
124+
125+
Assert.notNull(type, "Type must not be null!");
126+
127+
return stream().anyMatch(it -> it.couldContain(type));
128+
}
129+
130+
/**
131+
* Returns whether any of the the packages could contain the type independent of the actual types backing the current
132+
* instance.
133+
*
134+
* @param type must not be {@literal null}.
135+
* @see JavaPackage#couldContain(String)
136+
* @since 1.4.6, 2.0.1
137+
*/
138+
public boolean couldContain(String type) {
139+
140+
Assert.notNull(type, "Type must not be null!");
141+
142+
return stream().anyMatch(it -> it.couldContain(type));
143+
}
144+
98145
/*
99146
* (non-Javadoc)
100147
* @see java.lang.Iterable#iterator()

0 commit comments

Comments
 (0)