Skip to content

Commit

Permalink
Some tweaks to support a Spring Modulith style approach, where packag…
Browse files Browse the repository at this point in the history
…es are considered to be components.
  • Loading branch information
simonbrowndotje committed Aug 19, 2024
1 parent 4bab88f commit 3db0c02
Show file tree
Hide file tree
Showing 17 changed files with 132 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,12 @@ public final class ComponentFinder {
}

this.container = container;
this.componentFinderStrategies.addAll(componentFinderStrategies);

findTypes(typeProviders);
}

private void findTypes(Collection<TypeProvider> typeProviders) {
for (TypeProvider typeProvider : typeProviders) {
Set<com.structurizr.component.Type> types = typeProvider.getTypes();
for (com.structurizr.component.Type type : types) {
Expand All @@ -59,8 +64,6 @@ public final class ComponentFinder {
findDependencies(type);
}
}

this.componentFinderStrategies.addAll(componentFinderStrategies);
}

private void findDependencies(com.structurizr.component.Type type) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import java.util.LinkedHashSet;
import java.util.Set;

final class TypeRepository {
public final class TypeRepository {

private final Set<Type> types = new LinkedHashSet<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public boolean matches(Type type) {
}

if (type.getJavaClass() == null) {
throw new IllegalArgumentException("This type matcher requires a BCEL JavaClass");
return false;
}

AnnotationEntry[] annotationEntries = type.getJavaClass().getAnnotationEntries();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public boolean matches(Type type) {
}

if (type.getJavaClass() == null) {
throw new IllegalArgumentException("This type matcher requires a BCEL JavaClass");
return false;
}

JavaClass javaClass = type.getJavaClass();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public boolean matches(Type type) {
}

if (type.getJavaClass() == null) {
throw new IllegalArgumentException("This type matcher requires a BCEL JavaClass");
return false;
}

JavaClass javaClass = type.getJavaClass();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.structurizr.component.naming;

import com.structurizr.component.Type;

/**
* Uses Apache commons-lang to split a camel-cased package name into separate words
* (e.g. "com.example.order.package-info" -> "Order").
*/
public class DefaultPackageNamingStrategy implements NamingStrategy {

@Override
public String nameOf(Type type) {
String packageName = type.getPackageName();
if (packageName.contains(".")) {
packageName = packageName.substring(packageName.lastIndexOf(".") + 1);
}

String[] parts = org.apache.commons.lang3.StringUtils.splitByCharacterTypeCamelCase(packageName);
String name = String.join(" ", parts);
name = name.substring(0, 1).toUpperCase() + name.substring(1);

return name;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.github.javaparser.ParserConfiguration;
import com.github.javaparser.StaticJavaParser;
import com.github.javaparser.ast.Node;
import com.github.javaparser.ast.PackageDeclaration;
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
import com.github.javaparser.ast.comments.JavadocComment;
import com.github.javaparser.ast.visitor.VoidVisitorAdapter;
Expand Down Expand Up @@ -69,6 +71,28 @@ public void visit(ClassOrInterfaceDeclaration n, Object arg) {

type.setDescription(new JavadocCommentFilter(maximumDescriptionLength).filterAndTruncate(description));
}
types.add(type);
}
}

@Override
public void visit(PackageDeclaration n, Object arg) {
String PACKAGE_INFO_JAVA_SOURCE = "package-info.java";
String PACKAGE_INFO_SUFFIX = ".package-info";

if (path.getName().endsWith(PACKAGE_INFO_JAVA_SOURCE)) {
String fullyQualifiedName = n.getName().asString() + PACKAGE_INFO_SUFFIX;

Type type = new Type(fullyQualifiedName);
type.setSource(path.getAbsolutePath());

Node rootNode = n.findRootNode();
if (rootNode != null && rootNode.getComment().isPresent() && rootNode.getComment().get() instanceof JavadocComment) {
JavadocComment javadocComment = (JavadocComment)rootNode.getComment().get();
String description = javadocComment.parse().getDescription().toText();

type.setDescription(new JavadocCommentFilter(maximumDescriptionLength).filterAndTruncate(description));
}

types.add(type);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
package com.structurizr.component.supporting;

import com.structurizr.component.Type;
import com.structurizr.component.TypeRepository;

import java.util.Set;
import java.util.stream.Collectors;

/**
* A strategy that finds all referenced types in the same package as the component type.
*/
public class AllReferencedTypesInSamePackageSupportingTypesStrategy implements SupportingTypesStrategy {
public class AllReferencedTypesInPackageSupportingTypesStrategy implements SupportingTypesStrategy {

@Override
public Set<Type> findSupportingTypes(Type type) {
public Set<Type> findSupportingTypes(Type type, TypeRepository typeRepository) {
String packageName = type.getPackageName();

return type.getDependencies().stream()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.structurizr.component.supporting;

import com.structurizr.component.Type;
import com.structurizr.component.TypeRepository;

import java.util.Set;

Expand All @@ -10,7 +11,7 @@
public class AllReferencedTypesSupportingTypesStrategy implements SupportingTypesStrategy {

@Override
public Set<Type> findSupportingTypes(Type type) {
public Set<Type> findSupportingTypes(Type type, TypeRepository typeRepository) {
return type.getDependencies();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.structurizr.component.supporting;

import com.structurizr.component.Type;
import com.structurizr.component.TypeRepository;

import java.util.Set;
import java.util.stream.Collectors;

/**
* A strategy that finds all referenced types in the same package as the component type.
*/
public class AllTypesInPackageSupportingTypesStrategy implements SupportingTypesStrategy {

@Override
public Set<Type> findSupportingTypes(Type type, TypeRepository typeRepository) {
String packageName = type.getPackageName();

return typeRepository.getTypes().stream()
.filter(dependency -> dependency.getPackageName().equals(packageName))
.collect(Collectors.toSet());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.structurizr.component.supporting;

import com.structurizr.component.Type;
import com.structurizr.component.TypeRepository;

import java.util.Set;
import java.util.stream.Collectors;

/**
* A strategy that finds all referenced types in the same package as the component type.
*/
public class AllTypesUnderPackageSupportingTypesStrategy implements SupportingTypesStrategy {

@Override
public Set<Type> findSupportingTypes(Type type, TypeRepository typeRepository) {
String packageName = type.getPackageName();

return typeRepository.getTypes().stream()
.filter(dependency -> dependency.getPackageName().startsWith(packageName))
.collect(Collectors.toSet());
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.structurizr.component.supporting;

import com.structurizr.component.Type;
import com.structurizr.component.TypeRepository;

import java.util.Collections;
import java.util.Set;
Expand All @@ -11,7 +12,7 @@
public class DefaultSupportingTypesStrategy implements SupportingTypesStrategy {

@Override
public Set<Type> findSupportingTypes(Type type) {
public Set<Type> findSupportingTypes(Type type, TypeRepository typeRepository) {
return Collections.emptySet();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.structurizr.component.supporting;

import com.structurizr.component.Type;
import com.structurizr.component.TypeRepository;

import java.util.Set;

Expand All @@ -9,6 +10,6 @@
*/
public interface SupportingTypesStrategy {

Set<Type> findSupportingTypes(Type type);
Set<Type> findSupportingTypes(Type type, TypeRepository typeRepository);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.structurizr.component.naming;

import com.structurizr.component.Type;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class DefaultPackageNamingStrategyTests {

@Test
void nameOf() {
assertEquals("Order Management", new DefaultPackageNamingStrategy().nameOf(new Type("com.example.orderManagement.package-info")));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class AllReferencedTypesInSamePackageSupportingTypesStrategyTests {
public class AllReferencedTypesInPackageSupportingTypesStrategyTests {

@Test
void findSupportingTypes() {
Expand All @@ -21,7 +21,7 @@ void findSupportingTypes() {

type.addDependency(new Type("com.example.util.SomeUtils"));

Set<Type> supportingTypes = new AllReferencedTypesInSamePackageSupportingTypesStrategy().findSupportingTypes(type);
Set<Type> supportingTypes = new AllReferencedTypesInPackageSupportingTypesStrategy().findSupportingTypes(type, null);
assertEquals(1, supportingTypes.size());
assertTrue(supportingTypes.contains(dependency1));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ void findSupportingTypes() {

type.addDependency(new Type("com.example.util.SomeUtils"));

Set<Type> supportingTypes = new AllReferencedTypesSupportingTypesStrategy().findSupportingTypes(type);
Set<Type> supportingTypes = new AllReferencedTypesSupportingTypesStrategy().findSupportingTypes(type, null);
assertEquals(2, supportingTypes.size());
assertTrue(supportingTypes.contains(dependency1));
assertTrue(supportingTypes.contains(dependency2));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ void findSupportingTypes() {
Type type = new Type("com.example.a.A");
type.addDependency(new Type("com.example.a.AImpl"));

Set<Type> supportingTypes = new DefaultSupportingTypesStrategy().findSupportingTypes(type);
Set<Type> supportingTypes = new DefaultSupportingTypesStrategy().findSupportingTypes(type, null);
assertTrue(supportingTypes.isEmpty());
}

Expand Down

0 comments on commit 3db0c02

Please sign in to comment.