From e93ec048be927787c80afbf35da4bcea1b789674 Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Sat, 21 Dec 2024 19:27:00 +0100 Subject: [PATCH] Add a UsesType precondition to ReplaceConstant --- .../main/java/org/openrewrite/java/ReplaceConstant.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/rewrite-java/src/main/java/org/openrewrite/java/ReplaceConstant.java b/rewrite-java/src/main/java/org/openrewrite/java/ReplaceConstant.java index eedef216d95..b8f7f001e14 100644 --- a/rewrite-java/src/main/java/org/openrewrite/java/ReplaceConstant.java +++ b/rewrite-java/src/main/java/org/openrewrite/java/ReplaceConstant.java @@ -19,6 +19,7 @@ import lombok.Value; import org.jspecify.annotations.Nullable; import org.openrewrite.*; +import org.openrewrite.java.search.UsesType; import org.openrewrite.java.tree.J; import org.openrewrite.java.tree.JavaType; import org.openrewrite.java.tree.TypeUtils; @@ -53,7 +54,7 @@ public String getDescription() { @Override public TreeVisitor getVisitor() { - return new JavaVisitor() { + JavaVisitor replacementVisitor = new JavaVisitor() { J.@Nullable Literal literal; @Override @@ -117,5 +118,9 @@ private J.Literal buildLiteral() { return literal; } }; + return Preconditions.check( + new UsesType<>(owningType, true), + replacementVisitor + ); } }