Skip to content

Commit

Permalink
Adds support for custom type matchers with a parameter.
Browse files Browse the repository at this point in the history
  • Loading branch information
simonbrowndotje committed Aug 24, 2024
1 parent f6b55e5 commit bc93a0f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,15 @@ void parseMatcher(ComponentFinderStrategyDslContext context, Tokens tokens, File
default:
try {
Class<? extends TypeMatcher> typeMatcherClass = context.loadClass(type, dslFile);
Constructor<? extends TypeMatcher> constructor = typeMatcherClass.getDeclaredConstructor();
TypeMatcher typeMatcher = constructor.newInstance();

TypeMatcher typeMatcher;
if (tokens.size() == 3) {
String parameter = tokens.get(2);
typeMatcher = typeMatcherClass.getDeclaredConstructor(String.class).newInstance(parameter);
} else {
typeMatcher = typeMatcherClass.getDeclaredConstructor().newInstance();
}

context.getComponentFinderStrategyBuilder().matchedBy(typeMatcher);
} catch (Exception e) {
throw new RuntimeException("Type matcher \"" + type + "\" could not be loaded - " + e.getClass() + ": " + e.getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.structurizr.component.ComponentFinderBuilder;
import com.structurizr.component.matcher.AnnotationTypeMatcher;
import com.structurizr.component.matcher.NameSuffixTypeMatcher;
import org.junit.jupiter.api.Test;

import java.io.File;
Expand Down Expand Up @@ -142,11 +143,17 @@ void test_parseMatcher_WhenACustomTypeMatcherIsUsedButCannotBeLoaded() {
}

@Test
void test_parseMatcher_WhenACustomTypeMatcherIsUsed() {
void test_parseMatcher_WhenACustomTypeMatcherIsUsedWithoutParameters() {
parser.parseMatcher(context, tokens("matcher", "com.structurizr.dsl.example.CustomTypeMatcher"), new File("."));
assertEquals("ComponentFinderStrategyBuilder{technology='null', typeMatcher=CustomTypeMatcher{}, typeFilter=DefaultTypeFilter{}, supportingTypesStrategy=DefaultSupportingTypesStrategy{}, namingStrategy=DefaultNamingStrategy{}, componentVisitor=DefaultComponentVisitor{}}", context.getComponentFinderStrategyBuilder().toString());
}

@Test
void test_parseMatcher_WhenACustomTypeMatcherIsUsedWithAParameter() {
parser.parseMatcher(context, tokens("matcher", NameSuffixTypeMatcher.class.getCanonicalName(), "Component"), new File("."));
assertEquals("ComponentFinderStrategyBuilder{technology='null', typeMatcher=NameSuffixTypeMatcher{suffix='Component'}, typeFilter=DefaultTypeFilter{}, supportingTypesStrategy=DefaultSupportingTypesStrategy{}, namingStrategy=DefaultNamingStrategy{}, componentVisitor=DefaultComponentVisitor{}}", context.getComponentFinderStrategyBuilder().toString());
}

@Test
void test_parseFilter_ThrowsAnException_WhenNoTypeIsSpecified() {
try {
Expand Down

0 comments on commit bc93a0f

Please sign in to comment.