Skip to content

Commit

Permalink
Fixed property usage pattern matching search results (#3634)
Browse files Browse the repository at this point in the history
* Fixed property usage pattern matching search results

* format

* use actual regular expression for propertyPattern

* added javadoc

* fixed wrong usage of tag parameter instead of super visitor return

* fixed tests with regular expression
  • Loading branch information
Joan Viladrosa authored Oct 19, 2023
1 parent 576b7f9 commit 68067ef
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public class FindProperties extends Recipe {

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

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

@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
Pattern propertyMatcher = Pattern.compile(propertyPattern.replace(".", "\\.")
.replace("*", ".*"));
Pattern propertyMatcher = Pattern.compile(propertyPattern);
Pattern propertyUsageMatcher = Pattern.compile(".*\\$\\{" + propertyMatcher.pattern() + "}.*");
return new MavenVisitor<ExecutionContext>() {
@Override
public Xml visitTag(Xml.Tag tag, ExecutionContext context) {
Xml.Tag t = (Xml.Tag) super.visitTag(tag, context);
if (isPropertyTag() && propertyMatcher.matcher(tag.getName()).matches()) {
if (isPropertyTag() && propertyMatcher.matcher(t.getName()).matches()) {
t = SearchResult.found(t);
}

Optional<String> value = tag.getValue();
if (t.getContent() != null && value.isPresent() && value.get().contains("${")) {
Optional<String> value = t.getValue();
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 @@ -76,11 +76,16 @@ public Xml visitTag(Xml.Tag tag, ExecutionContext context) {
};
}

/**
*
* @param xml The xml document of the pom.xml
* @param propertyPattern Regular expression pattern used to match property tag names
* @return Set of Maven project property tags that matches the {@code propertyPattern} within a pom.xml
*/
public static Set<Xml.Tag> find(Xml.Document xml, String propertyPattern) {
Pattern propertyMatcher = Pattern.compile(propertyPattern.replace(".", "\\.")
.replace("*", ".*"));
Pattern propertyMatcher = Pattern.compile(propertyPattern);
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 @@ -27,7 +27,7 @@ class FindPropertiesTest implements RewriteTest {
@Test
void findProperty() {
rewriteRun(
spec -> spec.recipe(new FindProperties("guava*")),
spec -> spec.recipe(new FindProperties("guava.*")),
pomXml(
"""
<project>
Expand Down 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 68067ef

Please sign in to comment.