-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Shobhit
committed
Jan 16, 2025
1 parent
c47d8f4
commit 45a3098
Showing
2 changed files
with
114 additions
and
0 deletions.
There are no files selected for viewing
68 changes: 68 additions & 0 deletions
68
src/main/java/com/yourorg/ReplaceMapOfWithUnmodifiableFixedOrderMap.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package com.yourorg; | ||
|
||
import org.openrewrite.ExecutionContext; | ||
import org.openrewrite.NlsRewrite; | ||
import org.openrewrite.Preconditions; | ||
import org.openrewrite.Recipe; | ||
import org.openrewrite.TreeVisitor; | ||
import org.openrewrite.java.JavaIsoVisitor; | ||
import org.openrewrite.java.JavaTemplate; | ||
import org.openrewrite.java.JavaVisitor; | ||
import org.openrewrite.java.MethodMatcher; | ||
import org.openrewrite.java.search.UsesMethod; | ||
import org.openrewrite.java.tree.Expression; | ||
import org.openrewrite.java.tree.J; | ||
|
||
import java.util.List; | ||
|
||
public class ReplaceMapOfWithUnmodifiableFixedOrderMap extends Recipe { | ||
|
||
private static final MethodMatcher MATCHER = new MethodMatcher("java.util.Map of(..)"); | ||
|
||
@Override | ||
public @NlsRewrite.DisplayName String getDisplayName() { | ||
return "Use `UnMofifiableFixedOrderMap` instead of `Map.of()` or `Map.ofEntries()`"; | ||
} | ||
|
||
@Override | ||
public @NlsRewrite.Description String getDescription() { | ||
return "UnMofifiableFixedOrderMap gives fixed order of iteration on successive test runs which prevents" + | ||
" randomness during regression using Snapshot-testing."; | ||
} | ||
|
||
@Override | ||
public TreeVisitor<?, ExecutionContext> getVisitor() { | ||
return Preconditions.check( | ||
Preconditions.or(new UsesMethod<>(MATCHER)), | ||
new JavaVisitor<ExecutionContext>() { | ||
|
||
@Override | ||
public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) { | ||
|
||
// Ensure arguments are in key-value pairs | ||
List<Expression> arguments = method.getArguments(); | ||
if (arguments.size() % 2 != 0) { | ||
return method; // Not key-value pairs, skip | ||
} | ||
|
||
// Add required imports | ||
maybeAddImport("com.phonepe.payments.paymentservice.util.UnmodifiableFixedOrderMap"); | ||
maybeRemoveImport("java.util.Map"); | ||
|
||
// Build the replacement template | ||
StringBuilder builderTemplate = new StringBuilder("UnmodifiableFixedOrderMap.<String, Boolean>builder()"); | ||
for (int i = 0; i < arguments.size(); i += 2) { | ||
builderTemplate.append(".put(\"").append(arguments.get(i)).append("\", ").append(arguments.get(i+1)).append(")"); | ||
} | ||
builderTemplate.append(".build()"); | ||
|
||
JavaTemplate template = JavaTemplate.builder(builderTemplate.toString()) | ||
.imports("com.phonepe.payments.paymentservice.util.UnmodifiableFixedOrderMap") | ||
.build(); | ||
|
||
return template.apply(getCursor(), method.getCoordinates().replace()); | ||
|
||
} | ||
}); | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
src/test/java/com/yourorg/ReplaceMapOfWithUnmodifiableFixedOrderMapTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package com.yourorg; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.openrewrite.test.RecipeSpec; | ||
import org.openrewrite.test.RewriteTest; | ||
|
||
import static org.openrewrite.java.Assertions.java; | ||
|
||
public class ReplaceMapOfWithUnmodifiableFixedOrderMapTest implements RewriteTest { | ||
|
||
@Override | ||
public void defaults(RecipeSpec spec) { | ||
spec.recipe(new ReplaceMapOfWithUnmodifiableFixedOrderMap()); | ||
} | ||
|
||
@Test | ||
void replaceMapOf() { | ||
rewriteRun( | ||
//language=java | ||
java( | ||
""" | ||
import java.util.Map; | ||
import static com.example.Constants.*; | ||
public class TestClass { | ||
void test() { | ||
Map<String, Boolean> map = Map.of("POLARIS_ROUTER_KEY", false); | ||
} | ||
} | ||
""", | ||
""" | ||
import static com.example.Constants.*; | ||
import com.example.UnmodifiableFixedOrderMap; | ||
public class TestClass { | ||
void test() { | ||
Map<String, Boolean> map = UnmodifiableFixedOrderMap.<String, Boolean>builder() | ||
.put("POLARIS_ROUTER_KEY", false) | ||
.build(); | ||
} | ||
} | ||
""" | ||
) | ||
); | ||
} | ||
} |