From 2473a970f205ece5603a6047c131824755c1b4f4 Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Wed, 13 Nov 2024 14:28:47 +0100 Subject: [PATCH 1/2] Account for a second common resource filtering pattern --- .../maven/RemoveUnusedProperties.java | 74 ++++++++++--------- .../maven/RemoveUnusedPropertiesTest.java | 15 +++- 2 files changed, 49 insertions(+), 40 deletions(-) diff --git a/rewrite-maven/src/main/java/org/openrewrite/maven/RemoveUnusedProperties.java b/rewrite-maven/src/main/java/org/openrewrite/maven/RemoveUnusedProperties.java index 6969bb1a4e8..2e1ecf56a6d 100644 --- a/rewrite-maven/src/main/java/org/openrewrite/maven/RemoveUnusedProperties.java +++ b/rewrite-maven/src/main/java/org/openrewrite/maven/RemoveUnusedProperties.java @@ -17,13 +17,9 @@ import lombok.EqualsAndHashCode; import lombok.Value; +import org.jetbrains.annotations.NotNull; import org.jspecify.annotations.Nullable; -import org.openrewrite.ExecutionContext; -import org.openrewrite.Option; -import org.openrewrite.ScanningRecipe; -import org.openrewrite.SourceFile; -import org.openrewrite.Tree; -import org.openrewrite.TreeVisitor; +import org.openrewrite.*; import org.openrewrite.java.tree.JavaSourceFile; import org.openrewrite.maven.internal.MavenPomDownloader; import org.openrewrite.maven.tree.MavenResolutionResult; @@ -38,12 +34,7 @@ import java.nio.file.InvalidPathException; import java.nio.file.Path; -import java.util.Collections; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Map; -import java.util.Optional; -import java.util.Set; +import java.util.*; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -76,14 +67,14 @@ public Map> getFilteredResourceUsages() { Map> result = new HashMap<>(); filteredResourcePathsToDeclaringPoms.forEach((filteredResourcePath, mrr) -> nonPomPathsToUsages.forEach((usagePath, properties) -> { - if (usagePath.startsWith(filteredResourcePath)) { - properties.forEach(property -> { - result.putIfAbsent(property, new HashSet<>()); - result.get(property).add(mrr); - }); - } - } - )); + if (usagePath.startsWith(filteredResourcePath)) { + properties.forEach(property -> { + result.putIfAbsent(property, new HashSet<>()); + result.get(property).add(mrr); + }); + } + } + )); return result; } } @@ -99,15 +90,10 @@ private String getPropertyPattern() { @Override public TreeVisitor getScanner(RemoveUnusedProperties.Accumulator acc) { - Pattern propertyMatcher = Pattern.compile(getPropertyPattern()); - Pattern propertyUsageMatcher = Pattern.compile("[^$]*\\$\\{(" + propertyMatcher.pattern() + ")}[^$]*"); - - MavenIsoVisitor findPomUsagesVisitor = - new FindPomUsagesVisitor(propertyUsageMatcher, acc); - MavenIsoVisitor findFilteredResourcePathsVisitor = - new FindFilteredResourcePathsVisitor(acc); - PlainTextVisitor findResourceUsagesVisitor = - new FindResourceUsagesVisitor(propertyUsageMatcher, acc); + String patternOrDefault = getPropertyPattern(); + MavenIsoVisitor findPomUsagesVisitor = new FindPomUsagesVisitor(dollarPropertyMatcher(patternOrDefault), acc); + MavenIsoVisitor findFilteredResourcePathsVisitor = new FindFilteredResourcePathsVisitor(acc); + PlainTextVisitor findResourceUsagesVisitor = new FindResourceUsagesVisitor(patternOrDefault, acc); return new TreeVisitor() { @Override @@ -126,6 +112,14 @@ public TreeVisitor getScanner(RemoveUnusedProperties.Accumu }; } + private static @NotNull Pattern dollarPropertyMatcher(String patternOrDefault) { + return Pattern.compile("[^$]*\\$\\{(" + patternOrDefault + ")}[^$]*"); + } + + private static @NotNull Pattern atPropertyMatcher(String patternOrDefault) { + return Pattern.compile("@(" + patternOrDefault + ")@"); + } + @Override public TreeVisitor getVisitor(RemoveUnusedProperties.Accumulator acc) { Pattern propertyMatcher = Pattern.compile(getPropertyPattern()); @@ -182,7 +176,7 @@ private boolean isAncestor(MavenResolutionResult project, ResolvedGroupArtifactV } private boolean parentHasProperty(MavenResolutionResult resolutionResult, String propertyName, - ExecutionContext ctx) { + ExecutionContext ctx) { MavenPomDownloader downloader = new MavenPomDownloader(resolutionResult.getProjectPoms(), ctx, resolutionResult.getMavenSettings(), resolutionResult.getActiveProfiles()); try { @@ -239,11 +233,12 @@ public Xml.Tag visitTag(Xml.Tag tag, ExecutionContext ctx) { if (resourceMatcher.matches(getCursor())) { String directory = tag.getChildValue("directory").orElse(null); if (tag.getChildValue("filtering").map(Boolean::valueOf).orElse(false) - && directory != null) { + && directory != null) { Path path = getCursor().firstEnclosingOrThrow(SourceFile.class).getSourcePath(); try { acc.filteredResourcePathsToDeclaringPoms.put(path.getParent().resolve(directory), getResolutionResult()); - } catch (InvalidPathException ignored) {} // fail quietly + } catch (InvalidPathException ignored) { + } // fail quietly } return tag; } else { @@ -253,17 +248,24 @@ public Xml.Tag visitTag(Xml.Tag tag, ExecutionContext ctx) { } private static class FindResourceUsagesVisitor extends PlainTextVisitor { - private final Pattern propertyUsageMatcher; + private final Pattern dollarMatcher; + private final Pattern atMatcher; private final Accumulator acc; - public FindResourceUsagesVisitor(Pattern propertyUsageMatcher, Accumulator acc) { - this.propertyUsageMatcher = propertyUsageMatcher; + public FindResourceUsagesVisitor(String pattern, Accumulator acc) { + this.dollarMatcher = dollarPropertyMatcher(pattern); + this.atMatcher = atPropertyMatcher(pattern); this.acc = acc; } @Override public PlainText visitText(PlainText text, ExecutionContext ctx) { - Matcher matcher = propertyUsageMatcher.matcher(text.getText()); + Matcher matcher = dollarMatcher.matcher(text.getText()); + while (matcher.find()) { + acc.nonPomPathsToUsages.putIfAbsent(text.getSourcePath(), new HashSet<>()); + acc.nonPomPathsToUsages.get(text.getSourcePath()).add(matcher.group(1)); + } + matcher = atMatcher.matcher(text.getText()); while (matcher.find()) { acc.nonPomPathsToUsages.putIfAbsent(text.getSourcePath(), new HashSet<>()); acc.nonPomPathsToUsages.get(text.getSourcePath()).add(matcher.group(1)); diff --git a/rewrite-maven/src/test/java/org/openrewrite/maven/RemoveUnusedPropertiesTest.java b/rewrite-maven/src/test/java/org/openrewrite/maven/RemoveUnusedPropertiesTest.java index eadf33f0ac7..f65bbf7418d 100644 --- a/rewrite-maven/src/test/java/org/openrewrite/maven/RemoveUnusedPropertiesTest.java +++ b/rewrite-maven/src/test/java/org/openrewrite/maven/RemoveUnusedPropertiesTest.java @@ -16,6 +16,8 @@ package org.openrewrite.maven; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; import org.openrewrite.test.RecipeSpec; import org.openrewrite.test.RewriteTest; @@ -388,8 +390,12 @@ void keepsPropertyUsedByChild() { ); } - @Test - void keepsPropertyUsedByFilteredResource() { + @ParameterizedTest + @ValueSource(strings = { + "Hello ${a}", + "Hello @a@" + }) + void keepsPropertyUsedByFilteredResource(String text) { rewriteRun( mavenProject("my-project", pomXml( @@ -416,7 +422,8 @@ void keepsPropertyUsedByFilteredResource() { - """, """ + """, + """ 4.0.0 @@ -441,7 +448,7 @@ void keepsPropertyUsedByFilteredResource() { """ ), srcMainResources( - text("Hello ${a}") + text(text) ) ) ); From 8aa1c23388e7268c5dcdc7f4239d1f6fba6b5cbd Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Wed, 13 Nov 2024 14:37:24 +0100 Subject: [PATCH 2/2] Drop JetBrains annotations --- .../java/org/openrewrite/maven/RemoveUnusedProperties.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/rewrite-maven/src/main/java/org/openrewrite/maven/RemoveUnusedProperties.java b/rewrite-maven/src/main/java/org/openrewrite/maven/RemoveUnusedProperties.java index 2e1ecf56a6d..3251d64f736 100644 --- a/rewrite-maven/src/main/java/org/openrewrite/maven/RemoveUnusedProperties.java +++ b/rewrite-maven/src/main/java/org/openrewrite/maven/RemoveUnusedProperties.java @@ -17,7 +17,6 @@ import lombok.EqualsAndHashCode; import lombok.Value; -import org.jetbrains.annotations.NotNull; import org.jspecify.annotations.Nullable; import org.openrewrite.*; import org.openrewrite.java.tree.JavaSourceFile; @@ -112,11 +111,11 @@ public TreeVisitor getScanner(RemoveUnusedProperties.Accumu }; } - private static @NotNull Pattern dollarPropertyMatcher(String patternOrDefault) { + private static Pattern dollarPropertyMatcher(String patternOrDefault) { return Pattern.compile("[^$]*\\$\\{(" + patternOrDefault + ")}[^$]*"); } - private static @NotNull Pattern atPropertyMatcher(String patternOrDefault) { + private static Pattern atPropertyMatcher(String patternOrDefault) { return Pattern.compile("@(" + patternOrDefault + ")@"); }