Skip to content

Propagate named interface assignments of types to return and parameter types of methods declared #1267

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,14 @@
*/
@AliasFor("value")
String[] name() default {};

/**
* Whether to automatically include types related to the ones declared in the named interface.
* When set to {@code true}, types that appear in public method signatures (parameters and return types)
* of the explicitly declared types will also be considered part of this named interface.
* This is useful to avoid having to explicitly declare all types that are part of the interface's API.
*
* @return {@code true} to automatically include related types, {@code false} otherwise
*/
boolean autoIncludeRelatedTypes() default false;
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,7 @@

import static java.util.stream.Collectors.*;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;
import java.util.*;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;
Expand Down Expand Up @@ -303,14 +297,56 @@ private static List<NamedInterface> ofAnnotatedTypes(Classes classes) {
}

NamedInterface.getDefaultedNames(annotation, it.getPackageName())
.forEach(name -> mappings.add(name, it));
.forEach(name -> {
mappings.add(name, it);
if(annotation.autoIncludeRelatedTypes()) {
mappings.addAll(name, methodSignatureExposedTypes(it));
}
});
});

return mappings.entrySet().stream() //
.map(entry -> NamedInterface.of(entry.getKey(), Classes.of(entry.getValue()))) //
.toList();
}

private static List<JavaClass> methodSignatureExposedTypes(JavaClass javaClass) {
var dependencies = new HashSet<JavaClass>();
var visited = new HashSet<JavaClass>();

collectExposedTypes(javaClass, dependencies, visited);

return dependencies.stream()
.filter(Types.JavaTypes.IS_NOT_CORE_JAVA_TYPE)
.toList();
}

private static void collectExposedTypes(JavaClass type, Set<JavaClass> exposedTypes, Set<JavaClass> visited) {
if (visited.contains(type)) {
return;
}

visited.add(type);

type.getAllMethods().forEach(method -> {
method.getParameterTypes().forEach(paramType -> {
Set<JavaClass> rawTypes = paramType.getAllInvolvedRawTypes();
exposedTypes.addAll(rawTypes);

rawTypes.forEach(rawType -> {
collectExposedTypes(rawType, exposedTypes, visited);
});
});

Set<JavaClass> returnRawTypes = method.getReturnType().getAllInvolvedRawTypes();
exposedTypes.addAll(returnRawTypes);

returnRawTypes.forEach(rawType -> {
collectExposedTypes(rawType, exposedTypes, visited);
});
});
}

/**
* A builder API to manually construct {@link NamedInterfaces} instances. Allows selecting packages to create
* {@link NamedInterface} instances for based on excluding and including predicates, name matches etc. Will always
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package autoinclude;

import autoinclude.dtos.MyDTO;

public interface APublicService {

void doSomething(MyDTO dto);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package autoinclude;

import autoinclude.dtos.MyDTO;

@org.springframework.modulith.NamedInterface(name = "AutoincludeService", autoIncludeRelatedTypes = true)
public interface AutoincludeService {

void doSomething(MyDTO dto);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package autoinclude;

import autoinclude.dtos.MyDTO;

@org.springframework.modulith.NamedInterface(name = "NoAutoincludeService", autoIncludeRelatedTypes = false)
public interface NoAutoincludeService {

void doSomething(MyDTO dto);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package autoinclude.dtos;

public record MyDTO(String name, MyRelatedDTO related) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package autoinclude.dtos;

public enum MyEnum {
ONE, TWO, THREE
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package autoinclude.dtos;

public record MyRelatedDTO(String name) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package autoinclude.events;

import org.springframework.modulith.NamedInterface;

@NamedInterface(name = "events.AnAutoincludeEvent", autoIncludeRelatedTypes = true)
public record AnAutoincludeEvent(RelatedType relatedType, AnEnum anEnum) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package autoinclude.events;

public enum AnEnum {
ONE, TWO, THREE
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package autoinclude.events;

import org.springframework.modulith.NamedInterface;

@NamedInterface(name = "events.NotAutoincludeEvent", autoIncludeRelatedTypes = false)
public record NotAutoincludeEvent(RelatedType relatedType, AnEnum anEnum) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package autoinclude.events;

public record RelatedType(String name) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@org.springframework.modulith.ApplicationModule
package autoinclude;