generated from moderneinc/rewrite-recipe-starter
-
Notifications
You must be signed in to change notification settings - Fork 2
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
1 parent
20086a4
commit 7bd1cbf
Showing
9 changed files
with
302 additions
and
12 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
27 changes: 27 additions & 0 deletions
27
src/main/java/com/sap/openrewrite/recipe/CdsElementEqualsRules.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,27 @@ | ||
package com.sap.openrewrite.recipe; | ||
|
||
import org.openrewrite.java.template.RecipeDescriptor; | ||
|
||
import com.google.errorprone.refaster.annotation.AfterTemplate; | ||
import com.google.errorprone.refaster.annotation.BeforeTemplate; | ||
import com.sap.cds.reflect.CdsElement; | ||
|
||
public class CdsElementEqualsRules { | ||
|
||
@RecipeDescriptor( | ||
name = "Replace CdsElement == comparisons", | ||
description = "Replaces CdsElement == comparions with equals") | ||
public static class SetTenantToSystemUser { | ||
|
||
@BeforeTemplate | ||
public boolean useObjectIdentity(CdsElement e1, CdsElement e2) { | ||
return e1 == e2; | ||
} | ||
|
||
@AfterTemplate | ||
public boolean useEquals(CdsElement e1, CdsElement e2) { | ||
return e1.equals(e2); | ||
} | ||
} | ||
|
||
} |
51 changes: 51 additions & 0 deletions
51
src/main/java/com/sap/openrewrite/recipe/FindCdsModelEquals.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,51 @@ | ||
package com.sap.openrewrite.recipe; | ||
|
||
import org.openrewrite.ExecutionContext; | ||
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.search.UsesType; | ||
import org.openrewrite.java.template.Semantics; | ||
import org.openrewrite.java.tree.J; | ||
import org.openrewrite.marker.SearchResult; | ||
|
||
import com.sap.cds.reflect.CdsModel; | ||
|
||
public class FindCdsModelEquals extends Recipe { | ||
|
||
@Override | ||
public String getDisplayName() { | ||
return "Finds CdsModel == comparisons"; | ||
} | ||
|
||
@Override | ||
public String getDescription() { | ||
return "Finds usages of CdsModel ==."; | ||
} | ||
|
||
@Override | ||
public TreeVisitor<?, ExecutionContext> getVisitor() { | ||
JavaVisitor<ExecutionContext> javaVisitor = new JavaIsoVisitor<>() { | ||
final JavaTemplate useObjectIdentity = Semantics.expression(this, "useObjectIdentity", | ||
(CdsModel m1, CdsModel m2) -> m1 == m2).build(); | ||
|
||
@Override | ||
public J.Binary visitBinary(J.Binary elem, ExecutionContext ctx) { | ||
J.Binary b = super.visitBinary(elem, ctx); | ||
if (useObjectIdentity.matches(getCursor())) { | ||
return SearchResult.found(elem); | ||
} | ||
return b; | ||
} | ||
|
||
}; | ||
return Preconditions.check( | ||
new UsesType<>("com.sap.cds.reflect.CdsModel", true), | ||
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
28 changes: 28 additions & 0 deletions
28
src/main/java/com/sap/openrewrite/recipe/RequestContexRunnerRules.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,28 @@ | ||
package com.sap.openrewrite.recipe; | ||
|
||
import org.openrewrite.java.template.RecipeDescriptor; | ||
|
||
import com.google.errorprone.refaster.annotation.AfterTemplate; | ||
import com.google.errorprone.refaster.annotation.BeforeTemplate; | ||
import com.sap.cds.services.runtime.RequestContextRunner; | ||
|
||
public class RequestContexRunnerRules { | ||
|
||
@RecipeDescriptor( | ||
name = "Replaces setTenant() with systemUser()", | ||
description = "Descriptions") | ||
public static class SetTenantToSystemUser { | ||
|
||
@BeforeTemplate | ||
public RequestContextRunner modifyUserBefore(RequestContextRunner runner, String tenant) { | ||
return runner.modifyUser(u -> u.setTenant(tenant)); | ||
} | ||
|
||
@AfterTemplate | ||
public RequestContextRunner systemUserAfter(RequestContextRunner runner, String tenant) { | ||
return runner.systemUser(tenant); | ||
} | ||
|
||
} | ||
|
||
} |
51 changes: 51 additions & 0 deletions
51
src/test/java/com/sap/openrewrite/recipe/CdsElementEqualsRulesTest.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,51 @@ | ||
package com.sap.openrewrite.recipe; | ||
|
||
import static org.openrewrite.java.Assertions.java; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.openrewrite.java.JavaParser; | ||
import org.openrewrite.test.RecipeSpec; | ||
import org.openrewrite.test.RewriteTest; | ||
|
||
public class CdsElementEqualsRulesTest implements RewriteTest { | ||
|
||
@Override | ||
public void defaults(RecipeSpec spec) { | ||
spec.recipe(new CdsElementEqualsRulesRecipes()).parser(JavaParser.fromJavaVersion().classpath("cds4j-api")); | ||
} | ||
|
||
@Test | ||
void testReplaceModifyUser() { | ||
rewriteRun( | ||
java( | ||
|
||
""" | ||
import com.sap.cds.reflect.CdsEntity; | ||
import com.sap.cds.reflect.CdsElement; | ||
class Test { | ||
boolean test(CdsEntity entity, CdsElement elem) { | ||
return entity.getElement("test") == elem; | ||
} | ||
} | ||
""", | ||
""" | ||
import com.sap.cds.reflect.CdsEntity; | ||
import com.sap.cds.reflect.CdsElement; | ||
class Test { | ||
boolean test(CdsEntity entity, CdsElement elem) { | ||
return entity.getElement("test").equals(elem); | ||
} | ||
} | ||
""" | ||
) | ||
); | ||
|
||
} | ||
|
||
} |
49 changes: 49 additions & 0 deletions
49
src/test/java/com/sap/openrewrite/recipe/FindCdsModelEqualsTest.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,49 @@ | ||
package com.sap.openrewrite.recipe; | ||
|
||
import static org.openrewrite.java.Assertions.java; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.openrewrite.java.JavaParser; | ||
import org.openrewrite.test.RecipeSpec; | ||
import org.openrewrite.test.RewriteTest; | ||
|
||
public class FindCdsModelEqualsTest implements RewriteTest { | ||
|
||
@Override | ||
public void defaults(RecipeSpec spec) { | ||
spec.recipe(new FindCdsModelEquals()).parser(JavaParser.fromJavaVersion().classpath("cds4j-api")); | ||
} | ||
|
||
@Test | ||
void testReplaceModifyUser() { | ||
rewriteRun( | ||
java( | ||
|
||
""" | ||
import com.sap.cds.reflect.CdsModel; | ||
class Test { | ||
boolean test(CdsModel m1, CdsModel m2) { | ||
return m1 == m2; | ||
} | ||
} | ||
""", | ||
""" | ||
import com.sap.cds.reflect.CdsModel; | ||
class Test { | ||
boolean test(CdsModel m1, CdsModel m2) { | ||
return /*~~>*/m1 == m2; | ||
} | ||
} | ||
""" | ||
) | ||
); | ||
|
||
} | ||
|
||
} |
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
51 changes: 51 additions & 0 deletions
51
src/test/java/com/sap/openrewrite/recipe/RequestContextRunnerRulesTest.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,51 @@ | ||
package com.sap.openrewrite.recipe; | ||
|
||
import static org.openrewrite.java.Assertions.java; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.openrewrite.java.JavaParser; | ||
import org.openrewrite.test.RecipeSpec; | ||
import org.openrewrite.test.RewriteTest; | ||
|
||
public class RequestContextRunnerRulesTest implements RewriteTest { | ||
|
||
@Override | ||
public void defaults(RecipeSpec spec) { | ||
spec.recipe(new RequestContexRunnerRulesRecipes()).parser(JavaParser.fromJavaVersion().classpath("cds-services-api")); | ||
} | ||
|
||
@Test | ||
void testReplaceModifyUser() { | ||
rewriteRun( | ||
java( | ||
|
||
""" | ||
import com.sap.cds.services.runtime.CdsRuntime; | ||
class Test { | ||
void test(CdsRuntime runtime) { | ||
String test = "test"; | ||
runtime.requestContext().modifyUser(u -> u.setTenant(test)); | ||
} | ||
} | ||
""", | ||
""" | ||
import com.sap.cds.services.runtime.CdsRuntime; | ||
class Test { | ||
void test(CdsRuntime runtime) { | ||
String test = "test"; | ||
runtime.requestContext().systemUser(test); | ||
} | ||
} | ||
""" | ||
) | ||
); | ||
|
||
} | ||
|
||
} |