Skip to content

Commit 06f047e

Browse files
committed
GH-1430 - Include module base package comments in module description of canvases.
The APT now also extracts Javadoc from package-info.java and adds it to the documentation source file the Documenter inspects when creating application module canvases. If such documentation is available, an additional row titled "Description" is added to the canvas.
1 parent cd10a66 commit 06f047e

File tree

8 files changed

+74
-5
lines changed

8 files changed

+74
-5
lines changed

spring-modulith-apt/src/main/java/org/springframework/modulith/apt/SpringModulithProcessor.java

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import io.toolisticon.aptk.tools.ElementUtils;
1919
import io.toolisticon.aptk.tools.wrapper.ElementWrapper;
2020
import io.toolisticon.aptk.tools.wrapper.ExecutableElementWrapper;
21+
import io.toolisticon.aptk.tools.wrapper.PackageElementWrapper;
2122
import io.toolisticon.aptk.tools.wrapper.TypeElementWrapper;
2223

2324
import java.io.File;
@@ -158,9 +159,7 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
158159

159160
roundEnv.getRootElements().stream()
160161
.map(ElementWrapper::wrap)
161-
.filter(ElementWrapper::isTypeElement)
162-
.map(TypeElementWrapper::toTypeElement)
163-
.flatMap(this::handle)
162+
.flatMap(this::toMetadata)
164163
.forEach(metadata::add);
165164

166165
return false;
@@ -213,8 +212,20 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
213212
return false;
214213
}
215214

216-
private Stream<TypeMetadata> handle(TypeElementWrapper type) {
217-
return getTypes(type).flatMap(this::toMetadata);
215+
private Stream<TypeMetadata> toMetadata(ElementWrapper<?> wrapper) {
216+
217+
if (wrapper.isTypeElement()) {
218+
return getTypes(TypeElementWrapper.toTypeElement(wrapper)).flatMap(this::toMetadata);
219+
}
220+
221+
if (wrapper.isPackageElement()) {
222+
223+
var pkg = PackageElementWrapper.toPackageElement(wrapper);
224+
225+
return Stream.of(new TypeMetadata(pkg.getPackageName(), getComment(pkg), Collections.emptyList()));
226+
}
227+
228+
return Stream.empty();
218229
}
219230

220231
private Stream<TypeMetadata> toMetadata(TypeElementWrapper it) {

spring-modulith-apt/src/test/java/org/springframework/modulith/apt/SpringModulithProcessorUnitTests.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,19 @@ void extractsJavadocFromMethodWithClassNestedInInterfaceAsParameter() {
7171
});
7272
}
7373

74+
@Test // GH-1430
75+
void extractsPackageComments() throws Exception {
76+
77+
assertSucceded(getSourceFile("package-info"));
78+
79+
var output = new File(JSON_LOCATION);
80+
81+
var context = JsonPath.parse(output);
82+
var comment = context.read("$[?(@.name == 'example')].comment", String[].class)[0];
83+
84+
assertThat(comment).isEqualTo("Test comment.");
85+
}
86+
7487
private static DoCustomAssertions assertSucceded(String source) {
7588

7689
return assertSourceProcessed(source)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/**
2+
* Test comment.
3+
*/
4+
package example;

spring-modulith-docs/src/main/java/org/springframework/modulith/docs/Asciidoctor.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,10 @@ public String renderBeanReferences(ApplicationModule module) {
410410
return bullets.isBlank() ? "None" : bullets;
411411
}
412412

413+
public String renderModuleDescription(ApplicationModule module) {
414+
return docSource.get().getDocumentation(module.getBasePackage()).orElse("");
415+
}
416+
413417
public String renderHeadline(int i, String modules) {
414418
return "=".repeat(i) + " " + modules + System.lineSeparator();
415419
}

spring-modulith-docs/src/main/java/org/springframework/modulith/docs/CodeReplacingDocumentationSource.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import java.util.Optional;
1919

20+
import org.springframework.modulith.core.JavaPackage;
2021
import org.springframework.util.Assert;
2122

2223
import com.tngtech.archunit.core.domain.JavaClass;
@@ -69,4 +70,14 @@ public Optional<String> getDocumentation(JavaClass type) {
6970
return delegate.getDocumentation(type)
7071
.map(asciidoctor::toAsciidoctor);
7172
}
73+
74+
/*
75+
* (non-Javadoc)
76+
* @see org.springframework.modulith.docs.DocumentationSource#getDocumentation(org.springframework.modulith.core.JavaPackage)
77+
*/
78+
@Override
79+
public Optional<String> getDocumentation(JavaPackage pkg) {
80+
return delegate.getDocumentation(pkg)
81+
.map(asciidoctor::toAsciidoctor);
82+
}
7283
}

spring-modulith-docs/src/main/java/org/springframework/modulith/docs/DocumentationSource.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
import java.util.Optional;
1919

20+
import org.springframework.modulith.core.JavaPackage;
21+
2022
import com.tngtech.archunit.core.domain.JavaClass;
2123
import com.tngtech.archunit.core.domain.JavaMethod;
2224

@@ -43,4 +45,13 @@ interface DocumentationSource {
4345
* @since 1.3
4446
*/
4547
Optional<String> getDocumentation(JavaClass type);
48+
49+
/**
50+
* Returns the documentation for the given {@link JavaPackage}.
51+
*
52+
* @param pkg must not be {@literal null}.
53+
* @return will never be {@literal null}.
54+
* @since 2.0
55+
*/
56+
Optional<String> getDocumentation(JavaPackage pkg);
4657
}

spring-modulith-docs/src/main/java/org/springframework/modulith/docs/Documenter.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,7 @@ String toModuleCanvas(ApplicationModule module, CanvasOptions options) {
442442

443443
.append(startTable("%autowidth.stretch, cols=\"h,a\""))
444444
.append(addTableRow("Base package", asciidoctor.toInlineCode(module.getBasePackage().getName()), options))
445+
.append(addTableRow("Description", asciidoctor.renderModuleDescription(module), options))
445446

446447
// Spring components
447448
.append(addTableRow("Spring components", asciidoctor.renderSpringBeans(module, options), options)) //

spring-modulith-docs/src/main/java/org/springframework/modulith/docs/SpringModulithDocumentationSource.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.jspecify.annotations.Nullable;
2727
import org.springframework.boot.json.BasicJsonParser;
2828
import org.springframework.core.io.Resource;
29+
import org.springframework.modulith.core.JavaPackage;
2930
import org.springframework.modulith.docs.metadata.MethodMetadata;
3031
import org.springframework.modulith.docs.metadata.TypeMetadata;
3132
import org.springframework.modulith.docs.util.BuildSystemUtils;
@@ -114,6 +115,19 @@ public Optional<String> getDocumentation(JavaMethod method) {
114115
.filter(StringUtils::hasText);
115116
}
116117

118+
/*
119+
* (non-Javadoc)
120+
* @see org.springframework.modulith.docs.DocumentationSource#getDocumentation(org.springframework.modulith.core.JavaPackage)
121+
*/
122+
@Override
123+
public Optional<String> getDocumentation(JavaPackage pkg) {
124+
125+
return metadata.stream()
126+
.filter(it -> it.name().equals(pkg.getName()))
127+
.findFirst()
128+
.map(TypeMetadata::comment);
129+
}
130+
117131
@SuppressWarnings("unchecked")
118132
private static Collection<TypeMetadata> from(Resource resource) {
119133

0 commit comments

Comments
 (0)