Skip to content

Fix NumberToDuraion recipe to handle variable #5208

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
String durationStr = durationCreationStr();

JavaTemplate template = JavaTemplate
.builder(durationStr + "(#{})")
.builder(durationStr + "(#{any()})")
.contextSensitive()
.imports("java.time.Duration")
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,37 @@ void timeUnitSpecified_shouldHonor() {
)
);
}

@Test
@EnabledOnJre({JRE.JAVA_8})
void variableProvided_shouldRewrite() {
rewriteRun(
spec -> spec.recipe(new NumberToDuration("com.amazonaws.ClientConfiguration setRequestTimeout(int)",
TimeUnit.SECONDS))
.parser(Java8Parser.builder().classpath("sqs", "aws-core", "sdk-core", "aws-java-sdk-sqs")),
java(
"import com.amazonaws.ClientConfiguration;\n"
+ "\n"
+ "public class Example {\n"
+ " \n"
+ " void test() {\n"
+ " int timeout = 1000;\n"
+ " ClientConfiguration clientConfiguration = new ClientConfiguration();\n"
+ " clientConfiguration.setRequestTimeout(timeout);\n"
+ " }\n"
+ "}\n",
"import com.amazonaws.ClientConfiguration;\n\n"
+ "import java.time.Duration;\n"
+ "\n"
+ "public class Example {\n"
+ " \n"
+ " void test() {\n"
+ " int timeout = 1000;\n"
+ " ClientConfiguration clientConfiguration = new ClientConfiguration();\n"
+ " clientConfiguration.setRequestTimeout(Duration.ofSeconds(timeout));\n"
+ " }\n"
+ "}"
)
);
}
}
Loading