Skip to content

Commit

Permalink
It now doesn't matter to the component finder what the ordering of so…
Browse files Browse the repository at this point in the history
…urce or class type providers is.
  • Loading branch information
simonbrowndotje committed Sep 19, 2024
1 parent 3c83773 commit 12ddd83
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class Type {
private static final String STRUCTURIZR_PROPERTY_ANNOTATION = "Lcom/structurizr/annotation/Property;";
private static final String STRUCTURIZR_PROPERTIES_ANNOTATION = "Lcom/structurizr/annotation/Properties;";

private final JavaClass javaClass;
private JavaClass javaClass = null;
private final String fullyQualifiedName;
private String description;
private String source;
Expand Down Expand Up @@ -72,6 +72,10 @@ public JavaClass getJavaClass() {
return this.javaClass;
}

void setJavaClass(JavaClass javaClass) {
this.javaClass = javaClass;
}

public void addDependency(Type type) {
this.dependencies.add(type);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,7 @@ void run(Collection<TypeProvider> typeProviders, TypeFilter typeFilter, TypeRepo
}

if (accepted) {
if (type.getJavaClass() != null) {
// this is the BCEL identified type
typeRepository.add(type);
} else {
// this is the source code identified type
Type bcelType = typeRepository.getType(type.getFullyQualifiedName());
if (bcelType != null) {
bcelType.setDescription(type.getDescription());
bcelType.setSource(type.getSource());
}
}
typeRepository.add(type);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,20 @@ public final class TypeRepository {
private final Set<Type> types = new LinkedHashSet<>();

public void add(Type type) {
this.types.add(type);
Type t = getType(type.getFullyQualifiedName());
if (t == null) {
// type isn't yet registered, so add it
types.add(type);
} else {
if (type.getJavaClass() != null) {
// this is the BCEL identified type
t.setJavaClass(type.getJavaClass());
} else {
// this is the source code identified type
t.setDescription(type.getDescription());
t.setSource(type.getSource());
}
}
}

public Set<Type> getTypes() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ public PrefixSourceUrlStrategy(String prefix) {

@Override
public String urlOf(Type type) {
return prefix + (type.getSource().replaceAll("\\\\", "/"));
if (type.getSource() != null) {
return prefix + (type.getSource().replaceAll("\\\\", "/"));
} else {
return null;
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ void run() {

ComponentFinder componentFinder = new ComponentFinderBuilder()
.forContainer(container)
.fromClasses(new File("build/classes/java/test"))
.fromSource(new File("src/test/java"))
.fromClasses(new File("build/classes/java/test"))
.filteredBy(new IncludeFullyQualifiedNameRegexFilter("com\\.structurizr\\.component\\.example\\..*"))
.withStrategy(new ComponentFinderStrategyBuilder()
.withTechnology("Web Controller")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.structurizr.component;

import org.apache.bcel.classfile.ClassParser;
import org.apache.bcel.classfile.JavaClass;
import org.junit.jupiter.api.Test;

import java.io.File;

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

public class TypeRepositoryTests {

@Test
void add_MergesClassInformation() throws Exception {
String fqn = "com.structurizr.component.TypeRepositoryTests";
File classes = new File("build/classes/java/test");
ClassParser parser = new ClassParser(new File(classes, "com/structurizr/component/TypeRepositoryTests.class").getAbsolutePath());
JavaClass javaClass = parser.parse();

Type classType = new Type(javaClass);
Type sourceType = new Type(fqn);
sourceType.setSource("source path");

TypeRepository typeRepository = new TypeRepository();
typeRepository.add(sourceType); // source first
typeRepository.add(classType);

assertSame(javaClass, typeRepository.getType(fqn).getJavaClass());
assertEquals("source path", typeRepository.getType(fqn).getSource());
}

@Test
void add_MergesSourceInformation() throws Exception {
String fqn = "com.structurizr.component.TypeRepositoryTests";
File classes = new File("build/classes/java/test");
ClassParser parser = new ClassParser(new File(classes, "com/structurizr/component/TypeRepositoryTests.class").getAbsolutePath());
JavaClass javaClass = parser.parse();

Type classType = new Type(javaClass);
Type sourceType = new Type(fqn);
sourceType.setSource("source path");

TypeRepository typeRepository = new TypeRepository();
typeRepository.add(classType); // class first
typeRepository.add(sourceType);

assertSame(javaClass, typeRepository.getType(fqn).getJavaClass());
assertEquals("source path", typeRepository.getType(fqn).getSource());
}

}

0 comments on commit 12ddd83

Please sign in to comment.