Skip to content

Commit

Permalink
Fixed property usage pattern matching search results
Browse files Browse the repository at this point in the history
  • Loading branch information
Joan Viladrosa committed Oct 19, 2023
1 parent 94a1786 commit 375baee
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@
@Value
public class FindProperties extends Recipe {

@Option(displayName = "Property pattern",
description = "Regular expression pattern used to match property tag names.",
example = "guava*")
@Option(displayName = "Property pattern", description = "Regular expression pattern used to match property tag names.", example = "guava*")
String propertyPattern;

UUID searchId = randomId();
Expand All @@ -55,8 +53,11 @@ public String getDescription() {

@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
Pattern propertyMatcher = Pattern.compile(propertyPattern.replace(".", "\\.")
Pattern propertyMatcher = Pattern.compile(propertyPattern
.replace(".", "\\.")
.replace("*", ".*"));
Pattern propertyUsageMatcher = Pattern.compile(
".*\\$\\{" + propertyMatcher.pattern() + "}.*");
return new MavenVisitor<ExecutionContext>() {
@Override
public Xml visitTag(Xml.Tag tag, ExecutionContext context) {
Expand All @@ -66,7 +67,7 @@ public Xml visitTag(Xml.Tag tag, ExecutionContext context) {
}

Optional<String> value = tag.getValue();
if (t.getContent() != null && value.isPresent() && value.get().contains("${")) {
if (value.isPresent() && propertyUsageMatcher.matcher(value.get()).matches()) {
//noinspection unchecked
t = t.withContent(ListUtils.mapFirst((List<Content>) t.getContent(), v ->
SearchResult.found(v, getResolutionResult().getPom().getValue(value.get()))));
Expand All @@ -77,10 +78,11 @@ public Xml visitTag(Xml.Tag tag, ExecutionContext context) {
}

public static Set<Xml.Tag> find(Xml.Document xml, String propertyPattern) {
Pattern propertyMatcher = Pattern.compile(propertyPattern.replace(".", "\\.")
Pattern propertyMatcher = Pattern.compile(propertyPattern
.replace(".", "\\.")
.replace("*", ".*"));
Set<Xml.Tag> found = new HashSet<>();
new MavenVisitor<Set<Xml.Tag>>(){
new MavenVisitor<Set<Xml.Tag>>() {
@Override
public Xml visitTag(Xml.Tag tag, Set<Xml.Tag> tags) {
Xml.Tag t = (Xml.Tag) super.visitTag(tag, tags);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,51 @@ void findProperty() {
)
);
}

@Test
void doesNotMatchOtherPropertyUsages() {
rewriteRun(
spec -> spec.recipe(new FindProperties("guava*")),
pomXml(
"""
<project>
<properties>
<someNullProp/>
<guava.version>28.2-jre</guava.version>
<other.property>guava</other.property>
</properties>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1</version>
<dependencies>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>${other.property}</artifactId>
<version>${guava.version}</version>
</dependency>
</dependencies>
</project>
""",
"""
<project>
<properties>
<someNullProp/>
<!--~~>--><guava.version>28.2-jre</guava.version>
<other.property>guava</other.property>
</properties>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1</version>
<dependencies>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>${other.property}</artifactId>
<version><!--~~(28.2-jre)~~>-->${guava.version}</version>
</dependency>
</dependencies>
</project>
"""
)
);
}
}

0 comments on commit 375baee

Please sign in to comment.