GH-183 - Improvements in named interface declarations.

Type based named interfaces on types declared in a module's API package still caused the type to be included in the unnamed interface. This is now fixed by explicitly removing named interface types from the unnamed interface.

We now also detect API package types assigned to a named interface without an explicit name as the package name defaulting doesn't work in this case.

Furthermore, named interfaces are now sorted alphabetically to make the unnamed one always appear first.
This commit is contained in:
Oliver Drotbohm
2023-04-23 23:21:00 +02:00
parent 0e6c41b57c
commit d9f298c96a
7 changed files with 140 additions and 22 deletions

View File

@@ -19,6 +19,7 @@ import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
@@ -79,6 +80,22 @@ public class FormatableType {
return CACHE.computeIfAbsent(type.getName(), FormatableType::new);
}
/**
* Formats the given {@link JavaClass}es by rendering a comma-separated list with the abbreviated class names.
*
* @param types must not be {@literal null}.
* @return will never be {@literal null}.
*/
public static String format(Iterable<JavaClass> types) {
Assert.notNull(types, "Types must not be null!");
return StreamSupport.stream(types.spliterator(), false)
.map(FormatableType::of)
.map(FormatableType::getAbbreviatedFullName)
.collect(Collectors.joining(", "));
}
/**
* Creates a new {@link FormatableType} for the given fully-qualified type name.
*

View File

@@ -15,13 +15,19 @@
*/
package org.springframework.modulith.core;
import static com.tngtech.archunit.base.DescribedPredicate.*;
import static org.springframework.modulith.core.Types.*;
import java.util.Iterator;
import java.util.List;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.util.Assert;
import com.tngtech.archunit.base.DescribedPredicate;
import com.tngtech.archunit.core.domain.JavaClass;
import com.tngtech.archunit.core.domain.JavaClass.Predicates;
import com.tngtech.archunit.core.domain.properties.CanBeAnnotated;
/**
* A named interface into an {@link ApplicationModule}. This can either be a package, explicitly annotated with
@@ -35,6 +41,8 @@ import com.tngtech.archunit.core.domain.JavaClass.Predicates;
public class NamedInterface implements Iterable<JavaClass> {
static final String UNNAMED_NAME = "<<UNNAMED>>";
private static final DescribedPredicate<CanBeAnnotated> ANNOTATED_NAMED_INTERFACE = //
isAnnotatedWith(org.springframework.modulith.NamedInterface.class);
private final String name;
private final Classes classes;
@@ -90,7 +98,21 @@ public class NamedInterface implements Iterable<JavaClass> {
* @return will never be {@literal null}.
*/
static NamedInterface unnamed(JavaPackage javaPackage) {
return new NamedInterface(UNNAMED_NAME, javaPackage.toSingle().getExposedClasses());
var basePackageClasses = javaPackage.toSingle().getExposedClasses();
// Types that declare the annotation but no explicit name
var withDefaultedNamedInterface = basePackageClasses.stream()
.filter(ANNOTATED_NAMED_INTERFACE)
.filter(NamedInterface::withDefaultedNamedInterface)
.toList();
// Illegal in the base package
Assert.state(withDefaultedNamedInterface.isEmpty(),
() -> "Cannot use named interface defaulting for type(s) %s located in base package!"
.formatted(FormatableType.format(withDefaultedNamedInterface)));
return new NamedInterface(UNNAMED_NAME, basePackageClasses.that(not(ANNOTATED_NAMED_INTERFACE)));
}
/**
@@ -174,7 +196,9 @@ public class NamedInterface implements Iterable<JavaClass> {
*/
@Override
public String toString() {
return "NamedInterface: name=%s, types=%s".formatted(name, classes);
return "NamedInterface: name=%s, types=[%s]" //
.formatted(name, classes.isEmpty() ? "" : " " + FormatableType.format(classes) + " ");
}
/**
@@ -196,4 +220,19 @@ public class NamedInterface implements Iterable<JavaClass> {
? List.of(packageName.substring(packageName.lastIndexOf('.') + 1))
: List.of(declaredNames);
}
/**
* Returns whether the given {@link JavaClass} is annotated with {@link org.springframework.modulith.NamedInterface}
* but does not declare a name explicitly.
*
* @param type must not be {@literal null}.
* @return
*/
private static boolean withDefaultedNamedInterface(JavaClass type) {
var annotation = AnnotatedElementUtils.getMergedAnnotation(type.reflect(),
org.springframework.modulith.NamedInterface.class);
return annotation != null && annotation.name().length == 0;
}
}

View File

@@ -17,6 +17,7 @@ package org.springframework.modulith.core;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;
@@ -49,7 +50,9 @@ public class NamedInterfaces implements Iterable<NamedInterface> {
Assert.notNull(namedInterfaces, "Named interfaces must not be null!");
this.namedInterfaces = namedInterfaces;
this.namedInterfaces = namedInterfaces.stream()
.sorted(Comparator.comparing(NamedInterface::getName))
.toList();
}
/**
@@ -60,9 +63,9 @@ public class NamedInterfaces implements Iterable<NamedInterface> {
*/
static NamedInterfaces discoverNamedInterfaces(JavaPackage basePackage) {
return NamedInterfaces.ofAnnotatedPackages(basePackage) //
.and(NamedInterfaces.ofAnnotatedTypes(basePackage)) //
.and(NamedInterface.unnamed(basePackage));
return NamedInterfaces.of(NamedInterface.unnamed(basePackage))
.and(ofAnnotatedPackages(basePackage))
.and(ofAnnotatedTypes(basePackage));
}
/**
@@ -150,14 +153,14 @@ public class NamedInterfaces implements Iterable<NamedInterface> {
* @param others must not be {@literal null}.
* @return will never be {@literal null}.
*/
NamedInterfaces and(List<NamedInterface> others) {
NamedInterfaces and(Iterable<NamedInterface> others) {
Assert.notNull(others, "Other NamedInterfaces must not be null!");
var namedInterfaces = new ArrayList<NamedInterface>();
var unmergedInterface = this.namedInterfaces;
var unmergedInterfaces = new ArrayList<>(this.namedInterfaces);
if (others.isEmpty()) {
if (!others.iterator().hasNext()) {
return this;
}
@@ -170,23 +173,18 @@ public class NamedInterfaces implements Iterable<NamedInterface> {
// Merge existing with new and add to result
existing.ifPresentOrElse(it -> {
namedInterfaces.add(it.merge(candidate));
unmergedInterface.remove(it);
unmergedInterfaces.remove(it);
},
() -> namedInterfaces.add(candidate));
}
namedInterfaces.addAll(unmergedInterface);
namedInterfaces.addAll(unmergedInterfaces);
return new NamedInterfaces(namedInterfaces);
}
private NamedInterfaces and(NamedInterface namedInterface) {
var result = new ArrayList<NamedInterface>(namedInterfaces.size() + 1);
result.addAll(namedInterfaces);
result.add(namedInterface);
return new NamedInterfaces(result);
private static NamedInterfaces of(NamedInterface interfaces) {
return new NamedInterfaces(List.of(interfaces));
}
private static List<NamedInterface> ofAnnotatedTypes(JavaPackage basePackage) {