-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Parameter reuse supported in Refaster templates
- Loading branch information
1 parent
691e526
commit 3eadd4e
Showing
9 changed files
with
165 additions
and
55 deletions.
There are no files selected for viewing
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
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
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
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
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
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,19 @@ | ||
package foo; | ||
|
||
import com.google.errorprone.refaster.annotation.AfterTemplate; | ||
import com.google.errorprone.refaster.annotation.BeforeTemplate; | ||
import org.openrewrite.java.template.Matches; | ||
import org.openrewrite.java.template.MethodInvocationMatcher; | ||
import org.openrewrite.java.template.NotMatches; | ||
|
||
public class ParameterReuse { | ||
@BeforeTemplate | ||
boolean before(String s) { | ||
return s == s; | ||
} | ||
|
||
@AfterTemplate | ||
boolean after(String s) { | ||
return s.equals(s); | ||
} | ||
} |
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,55 @@ | ||
package foo; | ||
|
||
import org.openrewrite.ExecutionContext; | ||
import org.openrewrite.Preconditions; | ||
import org.openrewrite.Recipe; | ||
import org.openrewrite.TreeVisitor; | ||
import org.openrewrite.internal.lang.NonNullApi; | ||
import org.openrewrite.java.JavaTemplate; | ||
import org.openrewrite.java.JavaVisitor; | ||
import org.openrewrite.java.search.*; | ||
import org.openrewrite.java.template.Primitive; | ||
import org.openrewrite.java.template.Semantics; | ||
import org.openrewrite.java.template.function.*; | ||
import org.openrewrite.java.template.internal.AbstractRefasterJavaVisitor; | ||
import org.openrewrite.java.tree.*; | ||
|
||
import java.util.function.Supplier; | ||
|
||
|
||
@NonNullApi | ||
public class ParameterReuseRecipe extends Recipe { | ||
|
||
@Override | ||
public String getDisplayName() { | ||
return "Refaster template `ParameterReuse`"; | ||
} | ||
|
||
@Override | ||
public String getDescription() { | ||
return "Recipe created for the following Refaster template:\n```java\npublic class ParameterReuse {\n \n @BeforeTemplate()\n boolean before(String s) {\n return s == s;\n }\n \n @AfterTemplate()\n boolean after(String s) {\n return s.equals(s);\n }\n}\n```\n."; | ||
} | ||
|
||
@Override | ||
public TreeVisitor<?, ExecutionContext> getVisitor() { | ||
JavaVisitor<ExecutionContext> javaVisitor = new AbstractRefasterJavaVisitor() { | ||
final Supplier<JavaTemplate> before = memoize(() -> Semantics.expression(this, "before", (String s) -> s == s).build()); | ||
final Supplier<JavaTemplate> after = memoize(() -> Semantics.expression(this, "after", (String s) -> s.equals(s)).build()); | ||
|
||
@Override | ||
public J visitBinary(J.Binary elem, ExecutionContext ctx) { | ||
JavaTemplate.Matcher matcher; | ||
if ((matcher = matcher(before, getCursor())).find()) { | ||
return embed( | ||
apply(after, getCursor(), elem.getCoordinates().replace(), matcher.parameter(0)), | ||
getCursor(), | ||
ctx | ||
); | ||
} | ||
return super.visitBinary(elem, ctx); | ||
} | ||
|
||
}; | ||
return javaVisitor; | ||
} | ||
} |
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,19 @@ | ||
package foo; | ||
|
||
import com.google.errorprone.refaster.annotation.AfterTemplate; | ||
import com.google.errorprone.refaster.annotation.BeforeTemplate; | ||
import org.openrewrite.java.template.Matches; | ||
import org.openrewrite.java.template.MethodInvocationMatcher; | ||
import org.openrewrite.java.template.NotMatches; | ||
|
||
public class ParameterReuse { | ||
@BeforeTemplate | ||
boolean before(String s) { | ||
return s.equals(s); | ||
} | ||
|
||
@AfterTemplate | ||
boolean after() { | ||
return true; | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
src/test/resources/template/ParameterReuseRecipe$1_before.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,9 @@ | ||
package foo; | ||
import org.openrewrite.java.*; | ||
|
||
public class ParameterReuseRecipe$1_before { | ||
public static JavaTemplate.Builder getTemplate() { | ||
return JavaTemplate | ||
.builder("#{s:any(java.lang.String)}.equals(#{s})"); | ||
} | ||
} |