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:
@@ -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.
|
||||
*
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
/*
|
||||
* Copyright 2023 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package example.ni;
|
||||
|
||||
import org.springframework.modulith.NamedInterface;
|
||||
|
||||
/**
|
||||
* @author Oliver Drotbohm
|
||||
*/
|
||||
@NamedInterface("api")
|
||||
public interface AnnotatedNamedInterfaceType {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
* Copyright 2023 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package example.ninvalid;
|
||||
|
||||
import org.springframework.modulith.NamedInterface;
|
||||
|
||||
/**
|
||||
* @author Oliver Drotbohm
|
||||
*/
|
||||
@NamedInterface
|
||||
public interface InvalidDefaultNamedInterface {}
|
||||
@@ -17,11 +17,13 @@ package org.springframework.modulith.core;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
import example.ni.AnnotatedNamedInterfaceType;
|
||||
import example.ni.RootType;
|
||||
import example.ni.api.ApiType;
|
||||
import example.ni.internal.AdditionalSpiType;
|
||||
import example.ni.internal.DefaultedNamedInterfaceType;
|
||||
import example.ni.spi.SpiType;
|
||||
import example.ninvalid.InvalidDefaultNamedInterface;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
@@ -39,21 +41,29 @@ class NamedInterfacesUnitTests {
|
||||
@Test
|
||||
void discoversNamedInterfaces() {
|
||||
|
||||
var classes = TestUtils.getClasses(RootType.class);
|
||||
var javaPackage = JavaPackage.of(classes, RootType.class.getPackageName());
|
||||
|
||||
var javaPackage = TestUtils.getPackage(RootType.class);
|
||||
var interfaces = NamedInterfaces.discoverNamedInterfaces(javaPackage);
|
||||
|
||||
assertThat(interfaces).map(NamedInterface::getName)
|
||||
.containsExactlyInAnyOrder(NamedInterface.UNNAMED_NAME, "api", "spi", "kpi", "internal");
|
||||
|
||||
assertInterfaceContains(interfaces, NamedInterface.UNNAMED_NAME, RootType.class);
|
||||
assertInterfaceContains(interfaces, "api", ApiType.class);
|
||||
assertInterfaceContains(interfaces, "api", ApiType.class, AnnotatedNamedInterfaceType.class);
|
||||
assertInterfaceContains(interfaces, "spi", SpiType.class, AdditionalSpiType.class);
|
||||
assertInterfaceContains(interfaces, "kpi", AdditionalSpiType.class);
|
||||
assertInterfaceContains(interfaces, "internal", DefaultedNamedInterfaceType.class);
|
||||
}
|
||||
|
||||
@Test // GH-183
|
||||
void rejectsDefaultingNamedInterfaceTypeInBasePackage() {
|
||||
|
||||
var javaPackage = TestUtils.getPackage(InvalidDefaultNamedInterface.class);
|
||||
|
||||
assertThatIllegalStateException().isThrownBy(() -> NamedInterfaces.discoverNamedInterfaces(javaPackage))
|
||||
.withMessageContaining("named interface defaulting")
|
||||
.withMessageContaining(InvalidDefaultNamedInterface.class.getSimpleName());
|
||||
}
|
||||
|
||||
private static void assertInterfaceContains(NamedInterfaces interfaces, String name, Class<?>... types) {
|
||||
|
||||
var classNames = Arrays.stream(types).map(Class::getName).toArray(String[]::new);
|
||||
|
||||
@@ -70,4 +70,8 @@ class TestUtils {
|
||||
.importPackagesOf(packageType)
|
||||
.that(resideInAPackage(packageType.getPackage().getName() + "..")));
|
||||
}
|
||||
|
||||
public static JavaPackage getPackage(Class<?> packageType) {
|
||||
return JavaPackage.of(TestUtils.getClasses(packageType), packageType.getPackageName());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user