Skip to content

Commit

Permalink
Parameter reuse supported in Refaster templates
Browse files Browse the repository at this point in the history
  • Loading branch information
jkschneider committed Sep 30, 2023
1 parent 022373a commit 6965fdc
Show file tree
Hide file tree
Showing 6 changed files with 7 additions and 37 deletions.
19 changes: 2 additions & 17 deletions src/test/resources/refaster/MatchingRecipes.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,3 @@
/*
* Copyright 2023 the original author or authors.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package foo;

import org.openrewrite.ExecutionContext;
Expand Down Expand Up @@ -82,7 +67,7 @@ public J visitMethodInvocation(J.MethodInvocation elem, ExecutionContext ctx) {
return super.visitMethodInvocation(elem, ctx);
}
return embed(
apply(after, getCursor(), elem.getCoordinates().replace(), matcher.parameter(0), matcher.parameter(0)),
apply(after, getCursor(), elem.getCoordinates().replace(), matcher.parameter(0)),
getCursor(),
ctx
);
Expand All @@ -92,7 +77,7 @@ public J visitMethodInvocation(J.MethodInvocation elem, ExecutionContext ctx) {
return super.visitMethodInvocation(elem, ctx);
}
return embed(
apply(after, getCursor(), elem.getCoordinates().replace(), matcher.parameter(0), matcher.parameter(0)),
apply(after, getCursor(), elem.getCoordinates().replace(), matcher.parameter(0)),
getCursor(),
ctx
);
Expand Down
17 changes: 1 addition & 16 deletions src/test/resources/refaster/MultipleDereferencesRecipes.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,3 @@
/*
* Copyright 2023 the original author or authors.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package foo;

import org.openrewrite.ExecutionContext;
Expand Down Expand Up @@ -128,7 +113,7 @@ public J visitMethodInvocation(J.MethodInvocation elem, ExecutionContext ctx) {
JavaTemplate.Matcher matcher;
if ((matcher = matcher(before, getCursor())).find()) {
return embed(
apply(after, getCursor(), elem.getCoordinates().replace(), matcher.parameter(0), matcher.parameter(0)),
apply(after, getCursor(), elem.getCoordinates().replace(), matcher.parameter(0)),
getCursor(),
ctx
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
public class ShouldAddClasspathRecipes$FullyQualifiedRecipe$1_after {
public static JavaTemplate.Builder getTemplate() {
return JavaTemplate
.builder("org.slf4j.LoggerFactory.getLogger(#{any(java.lang.String)})")
.builder("org.slf4j.LoggerFactory.getLogger(#{message:any(java.lang.String)})")
.javaParser(JavaParser.fromJavaVersion().classpath("slf4j-api"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@
public class ShouldAddClasspathRecipes$FullyQualifiedRecipe$1_before {
public static JavaTemplate.Builder getTemplate() {
return JavaTemplate
.builder("System.out.println(#{any(java.lang.String)})");
.builder("System.out.println(#{message:any(java.lang.String)})");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
public class ShouldAddClasspathRecipes$UnqualifiedRecipe$1_after {
public static JavaTemplate.Builder getTemplate() {
return JavaTemplate
.builder("org.slf4j.LoggerFactory.getLogger(#{any(java.lang.String)})")
.builder("org.slf4j.LoggerFactory.getLogger(#{message:any(java.lang.String)})")
.javaParser(JavaParser.fromJavaVersion().classpath("slf4j-api"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@
public class ShouldAddClasspathRecipes$UnqualifiedRecipe$1_before {
public static JavaTemplate.Builder getTemplate() {
return JavaTemplate
.builder("System.out.println(#{any(java.lang.String)})");
.builder("System.out.println(#{message:any(java.lang.String)})");
}
}

0 comments on commit 6965fdc

Please sign in to comment.