15
15
*/
16
16
package org .springframework .modulith .core ;
17
17
18
- import java .util .Arrays ;
19
18
import java .util .Iterator ;
20
19
import java .util .List ;
21
20
22
21
import org .springframework .util .Assert ;
23
22
24
- import com .tngtech .archunit .base .DescribedPredicate ;
25
23
import com .tngtech .archunit .core .domain .JavaClass ;
26
24
import com .tngtech .archunit .core .domain .JavaClass .Predicates ;
27
- import com .tngtech .archunit .core .domain .JavaModifier ;
28
- import com .tngtech .archunit .core .domain .properties .HasModifiers ;
29
25
30
26
/**
31
27
* A named interface into an {@link ApplicationModule}. This can either be a package, explicitly annotated with
36
32
* @author Oliver Drotbohm
37
33
* @see org.springframework.modulith.ApplicationModule#allowedDependencies()
38
34
*/
39
- public abstract class NamedInterface implements Iterable <JavaClass > {
35
+ public class NamedInterface implements Iterable <JavaClass > {
40
36
41
- private static final String UNNAMED_NAME = "<<UNNAMED>>" ;
42
- private static final String PACKAGE_INFO_NAME = "package-info" ;
37
+ static final String UNNAMED_NAME = "<<UNNAMED>>" ;
43
38
44
- protected final String name ;
39
+ private final String name ;
40
+ private final Classes classes ;
45
41
46
42
/**
47
43
* Creates a new {@link NamedInterface} with the given name.
48
44
*
49
45
* @param name must not be {@literal null} or empty.
50
46
*/
51
- protected NamedInterface (String name ) {
47
+ private NamedInterface (String name , Classes classes ) {
52
48
53
49
Assert .hasText (name , "Name must not be null or empty!" );
54
50
55
51
this .name = name ;
52
+ this .classes = classes ;
56
53
}
57
54
58
55
/**
@@ -61,15 +58,17 @@ protected NamedInterface(String name) {
61
58
* @param javaPackage must not be {@literal null}.
62
59
* @return will never be {@literal null}.
63
60
*/
64
- public static List <NamedInterface > of (JavaPackage javaPackage ) {
61
+ static List <NamedInterface > of (JavaPackage javaPackage ) {
65
62
66
- String [] name = javaPackage .getAnnotation (org .springframework .modulith .NamedInterface .class ) //
67
- .map (it -> it . name ( )) //
63
+ var names = javaPackage .getAnnotation (org .springframework .modulith .NamedInterface .class ) //
64
+ .map (it -> getDefaultedNames ( it , javaPackage . getName () )) //
68
65
.orElseThrow (() -> new IllegalArgumentException (
69
66
String .format ("Couldn't find NamedInterface annotation on package %s!" , javaPackage )));
70
67
71
- return Arrays .stream (name ) //
72
- .<NamedInterface > map (it -> new PackageBasedNamedInterface (it , javaPackage )) //
68
+ var classes = javaPackage .toSingle ().getExposedClasses ();
69
+
70
+ return names .stream ()
71
+ .<NamedInterface > map (it -> new NamedInterface (it , classes )) //
73
72
.toList ();
74
73
}
75
74
@@ -81,8 +80,8 @@ public static List<NamedInterface> of(JavaPackage javaPackage) {
81
80
* @param basePackage must not be {@literal null}.
82
81
* @return will never be {@literal null}.
83
82
*/
84
- public static TypeBasedNamedInterface of (String name , Classes classes , JavaPackage basePackage ) {
85
- return new TypeBasedNamedInterface (name , classes , basePackage );
83
+ static NamedInterface of (String name , Classes classes ) {
84
+ return new NamedInterface (name , classes );
86
85
}
87
86
88
87
/**
@@ -92,7 +91,7 @@ public static TypeBasedNamedInterface of(String name, Classes classes, JavaPacka
92
91
* @return will never be {@literal null}.
93
92
*/
94
93
static NamedInterface unnamed (JavaPackage javaPackage ) {
95
- return new PackageBasedNamedInterface (UNNAMED_NAME , javaPackage );
94
+ return new NamedInterface (UNNAMED_NAME , javaPackage . toSingle (). getExposedClasses () );
96
95
}
97
96
98
97
/**
@@ -120,7 +119,7 @@ public boolean contains(JavaClass type) {
120
119
121
120
Assert .notNull (type , "JavaClass must not be null!" );
122
121
123
- return getClasses () .contains (type );
122
+ return classes .contains (type );
124
123
}
125
124
126
125
/**
@@ -132,7 +131,7 @@ public boolean contains(Class<?> type) {
132
131
133
132
Assert .notNull (type , "Type must not be null!" );
134
133
135
- return !getClasses () .that (Predicates .equivalentTo (type )).isEmpty ();
134
+ return !classes .that (Predicates .equivalentTo (type )).isEmpty ();
136
135
}
137
136
138
137
/**
@@ -144,7 +143,7 @@ boolean hasSameNameAs(NamedInterface other) {
144
143
145
144
Assert .notNull (other , "NamedInterface must not be null!" );
146
145
147
- return this . name .equals (other .name );
146
+ return name .equals (other .name );
148
147
}
149
148
150
149
/*
@@ -153,128 +152,49 @@ boolean hasSameNameAs(NamedInterface other) {
153
152
*/
154
153
@ Override
155
154
public Iterator <JavaClass > iterator () {
156
- return getClasses () .iterator ();
155
+ return classes .iterator ();
157
156
}
158
157
159
158
/**
160
- * Returns all {@link Classes} making up this {@link NamedInterface }.
159
+ * Merges the current {@link NamedInterface} with the given {@link TypeBasedNamedInterface }.
161
160
*
161
+ * @param other must not be {@literal null}.
162
162
* @return will never be {@literal null}.
163
163
*/
164
- protected abstract Classes getClasses ();
164
+ NamedInterface merge (NamedInterface other ) {
165
+
166
+ Assert .isTrue (this .name .equals (other .name ),
167
+ () -> "Named interfaces name must be equal to %s but was %s" .formatted (name , other .name ));
168
+
169
+ return new NamedInterface (name , classes .and (other .classes ));
170
+ }
171
+
172
+ /*
173
+ * (non-Javadoc)
174
+ * @see java.lang.Object#toString()
175
+ */
176
+ @ Override
177
+ public String toString () {
178
+ return "NamedInterface: name=%s, types=%s" .formatted (name , classes );
179
+ }
165
180
166
181
/**
167
- * Merges the current {@link NamedInterface} with the given {@link TypeBasedNamedInterface}.
182
+ * Returns the names declared in the given {@link org.springframework.modulith.NamedInterface} annotation or defaults
183
+ * to the local name of the given package if none declared.
168
184
*
169
- * @param other must not be {@literal null}.
185
+ * @param annotation must not be {@literal null}.
186
+ * @param packageName must not be {@literal null}.
170
187
* @return will never be {@literal null}.
171
188
*/
172
- public abstract NamedInterface merge (TypeBasedNamedInterface other );
173
-
174
- private static class PackageBasedNamedInterface extends NamedInterface {
175
-
176
- private final Classes classes ;
177
- private final JavaPackage javaPackage ;
178
-
179
- public PackageBasedNamedInterface (String name , JavaPackage pkg ) {
180
-
181
- super (name );
182
-
183
- Assert .notNull (pkg , "Package must not be null!" );
184
- Assert .hasText (name , "Package name must not be null or empty!" );
185
-
186
- this .classes = pkg .toSingle ().getClasses () //
187
- .that (HasModifiers .Predicates .modifier (JavaModifier .PUBLIC )) //
188
- .that (DescribedPredicate .not (JavaClass .Predicates .simpleName (PACKAGE_INFO_NAME )));
189
-
190
- this .javaPackage = pkg ;
191
- }
192
-
193
- private PackageBasedNamedInterface (String name , Classes classes , JavaPackage pkg ) {
194
-
195
- super (name );
196
- this .classes = classes ;
197
- this .javaPackage = pkg ;
198
- }
199
-
200
- /*
201
- * (non-Javadoc)
202
- * @see org.springframework.modulith.model.NamedInterface#getClasses()
203
- */
204
- @ Override
205
- public Classes getClasses () {
206
- return classes ;
207
- }
208
-
209
- /*
210
- * (non-Javadoc)
211
- * @see org.springframework.modulith.model.NamedInterface#merge(org.springframework.modulith.model.NamedInterface.TypeBasedNamedInterface)
212
- */
213
- @ Override
214
- public NamedInterface merge (TypeBasedNamedInterface other ) {
215
- return new PackageBasedNamedInterface (name , classes .and (other .classes ), javaPackage );
216
- }
217
-
218
- /*
219
- * (non-Javadoc)
220
- * @see org.springframework.modulith.model.NamedInterface#toString()
221
- */
222
- @ Override
223
- public String toString () {
224
- return String .format ("%s - Public types residing in %s:\n %s\n " , name , javaPackage .getName (),
225
- classes .format (javaPackage .getName ()));
226
- }
227
- }
189
+ static List <String > getDefaultedNames (org .springframework .modulith .NamedInterface annotation , String packageName ) {
190
+
191
+ Assert .notNull (annotation , "NamedInterface must not be null!" );
192
+ Assert .hasText (packageName , "Package name must not be null or empty!" );
193
+
194
+ var declaredNames = annotation .name ();
228
195
229
- public static class TypeBasedNamedInterface extends NamedInterface {
230
-
231
- private final Classes classes ;
232
- private final JavaPackage pkg ;
233
-
234
- /**
235
- * Creates a new {@link TypeBasedNamedInterface} with the given name, {@link Classes} and {@link JavaPackage}.
236
- *
237
- * @param name must not be {@literal null} or empty.
238
- * @param types must not be {@literal null}.
239
- * @param pkg must not be {@literal null}.
240
- */
241
- public TypeBasedNamedInterface (String name , Classes types , JavaPackage pkg ) {
242
-
243
- super (name );
244
-
245
- Assert .notNull (types , "Classes must not be null!" );
246
- Assert .notNull (pkg , "JavaPackage must not be null!" );
247
-
248
- this .classes = types ;
249
- this .pkg = pkg ;
250
- }
251
-
252
- /*
253
- * (non-Javadoc)
254
- * @see org.springframework.modulith.model.NamedInterface#getClasses()
255
- */
256
- @ Override
257
- public Classes getClasses () {
258
- return classes ;
259
- }
260
-
261
- /*
262
- * (non-Javadoc)
263
- * @see org.springframework.modulith.model.NamedInterface#merge(org.springframework.modulith.model.NamedInterface.TypeBasedNamedInterface)
264
- */
265
- @ Override
266
- public NamedInterface merge (TypeBasedNamedInterface other ) {
267
- return new TypeBasedNamedInterface (name , classes .and (other .classes ), pkg );
268
- }
269
-
270
- /*
271
- * (non-Javadoc)
272
- * @see org.springframework.modulith.model.NamedInterface#toString()
273
- */
274
- @ Override
275
- public String toString () {
276
- return String .format ("%s - Types underneath base package %s:\n %s\n " , name , pkg .getName (),
277
- classes .format (pkg .getName ()));
278
- }
196
+ return declaredNames .length == 0
197
+ ? List .of (packageName .substring (packageName .lastIndexOf ('.' ) + 1 ))
198
+ : List .of (declaredNames );
279
199
}
280
200
}
0 commit comments