From bc93a0f9f658d5a037de2b4248e93a68e5b5480e Mon Sep 17 00:00:00 2001 From: Simon Brown Date: Sat, 24 Aug 2024 15:51:28 +0100 Subject: [PATCH] Adds support for custom type matchers with a parameter. --- .../dsl/ComponentFinderStrategyParser.java | 11 +++++++++-- .../dsl/ComponentFinderStrategyParserTests.java | 9 ++++++++- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/structurizr-dsl/src/main/java/com/structurizr/dsl/ComponentFinderStrategyParser.java b/structurizr-dsl/src/main/java/com/structurizr/dsl/ComponentFinderStrategyParser.java index 61d89c4b..8f2f7daf 100644 --- a/structurizr-dsl/src/main/java/com/structurizr/dsl/ComponentFinderStrategyParser.java +++ b/structurizr-dsl/src/main/java/com/structurizr/dsl/ComponentFinderStrategyParser.java @@ -93,8 +93,15 @@ void parseMatcher(ComponentFinderStrategyDslContext context, Tokens tokens, File default: try { Class typeMatcherClass = context.loadClass(type, dslFile); - Constructor 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()); diff --git a/structurizr-dsl/src/test/java/com/structurizr/dsl/ComponentFinderStrategyParserTests.java b/structurizr-dsl/src/test/java/com/structurizr/dsl/ComponentFinderStrategyParserTests.java index ba92a335..ad95614e 100644 --- a/structurizr-dsl/src/test/java/com/structurizr/dsl/ComponentFinderStrategyParserTests.java +++ b/structurizr-dsl/src/test/java/com/structurizr/dsl/ComponentFinderStrategyParserTests.java @@ -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; @@ -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 {