Skip to content

Commit

Permalink
Adopt rewrite-recipe-bom and show Refaster recipes (#35)
Browse files Browse the repository at this point in the history
* test

* Add Refaster style recipes as an example

* Document what each dependency is for

---------

Co-authored-by: Tim te Beek <[email protected]>
Co-authored-by: Tim te Beek <[email protected]>
  • Loading branch information
3 people authored Jan 20, 2024
1 parent 7ca5a6e commit 49ff90d
Show file tree
Hide file tree
Showing 3 changed files with 181 additions and 3 deletions.
17 changes: 14 additions & 3 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,27 @@ plugins {
group = "com.yourorg"
description = "Rewrite recipes."

// The bom version can also be set to a specific version or latest.release.
val latest = "latest.integration"
dependencies {
implementation(platform("org.openrewrite:rewrite-bom:${latest}"))
// The bom version can also be set to a specific version
// https://github.com/openrewrite/rewrite-recipe-bom/releases
implementation(platform("org.openrewrite.recipe:rewrite-recipe-bom:latest.release"))

implementation("org.openrewrite:rewrite-java")
runtimeOnly("org.openrewrite:rewrite-java-17")

// Refaster style recipes need the rewrite-templating annotation processor and dependency for generated recipes
// https://github.com/openrewrite/rewrite-templating/releases
annotationProcessor("org.openrewrite:rewrite-templating:latest.release")
implementation("org.openrewrite:rewrite-templating")
// The `@BeforeTemplate` and `@AfterTemplate` annotations are needed for refaster style recipes
compileOnly("com.google.errorprone:error_prone_core:2.19.1:with-dependencies") {
exclude("com.google.auto.service", "auto-service-annotations")
}

// Need to have a slf4j binding to see any output enabled from the parser.
runtimeOnly("ch.qos.logback:logback-classic:1.2.+")

// Our recipe converts Guava's `Lists` type
testRuntimeOnly("com.google.guava:guava:latest.release")
}

Expand Down
61 changes: 61 additions & 0 deletions src/main/java/com/yourorg/SimplifyTernary.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright 2024 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 com.yourorg;

import com.google.errorprone.refaster.annotation.AfterTemplate;
import com.google.errorprone.refaster.annotation.BeforeTemplate;
import org.openrewrite.java.template.RecipeDescriptor;

@RecipeDescriptor(
name = "Simplify ternary expressions",
description = "Simplifies various types of ternary expressions to improve code readability."
)
public class SimplifyTernary {

@RecipeDescriptor(
name = "Replace `booleanExpression ? true : false` with `booleanExpression`",
description = "Replace ternary expressions like `booleanExpression ? true : false` with `booleanExpression`."
)
public static class SimplifyTernaryTrueFalse {

@BeforeTemplate
boolean before(boolean expr) {
return expr ? true : false;
}

@AfterTemplate
boolean after(boolean expr) {
return expr;
}
}

@RecipeDescriptor(
name = "Replace `booleanExpression ? false : true` with `!booleanExpression`",
description = "Replace ternary expressions like `booleanExpression ? false : true` with `!booleanExpression`."
)
public static class SimplifyTernaryFalseTrue {

@BeforeTemplate
boolean before(boolean expr) {
return expr ? false : true;
}

@AfterTemplate
boolean after(boolean expr) {
return !(expr);
}
}
}
106 changes: 106 additions & 0 deletions src/test/java/com/yourorg/SimplifyTernaryTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
* Copyright 2024 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 com.yourorg;

import org.junit.jupiter.api.Test;
import org.openrewrite.test.RewriteTest;

import static org.openrewrite.java.Assertions.java;

class SimplifyTernaryTest implements RewriteTest {
@Test
void simplified() {
rewriteRun(
spec -> spec.recipe(new SimplifyTernaryRecipes()),
//language=java
java(
"""
class Test {
boolean trueCondition1 = true ? true : false;
boolean trueCondition2 = false ? false : true;
boolean trueCondition3 = booleanExpression() ? true : false;
boolean trueCondition4 = trueCondition1 && trueCondition2 ? true : false;
boolean trueCondition5 = !true ? false : true;
boolean trueCondition6 = !false ? true : false;
boolean falseCondition1 = true ? false : true;
boolean falseCondition2 = !false ? false : true;
boolean falseCondition3 = booleanExpression() ? false : true;
boolean falseCondition4 = trueCondition1 && trueCondition2 ? false : true;
boolean falseCondition5 = !false ? false : true;
boolean falseCondition6 = !true ? true : false;
boolean binary1 = booleanExpression() && booleanExpression() ? true : false;
boolean binary2 = booleanExpression() && booleanExpression() ? false : true;
boolean binary3 = booleanExpression() || booleanExpression() ? true : false;
boolean binary4 = booleanExpression() || booleanExpression() ? false : true;
boolean booleanExpression() {
return true;
}
}
""",
"""
class Test {
boolean trueCondition1 = true;
boolean trueCondition2 = true;
boolean trueCondition3 = booleanExpression();
boolean trueCondition4 = trueCondition1 && trueCondition2;
boolean trueCondition5 = true;
boolean trueCondition6 = true;
boolean falseCondition1 = false;
boolean falseCondition2 = false;
boolean falseCondition3 = !booleanExpression();
boolean falseCondition4 = !(trueCondition1 && trueCondition2);
boolean falseCondition5 = false;
boolean falseCondition6 = false;
boolean binary1 = booleanExpression() && booleanExpression();
boolean binary2 = !(booleanExpression() && booleanExpression());
boolean binary3 = booleanExpression() || booleanExpression();
boolean binary4 = !(booleanExpression() || booleanExpression());
boolean booleanExpression() {
return true;
}
}
"""
)
);
}

@Test
void unchanged() {
rewriteRun(
spec -> spec.recipe(new SimplifyTernaryRecipes()),
//language=java
java(
"""
class Test {
boolean unchanged1 = booleanExpression() ? booleanExpression() : !booleanExpression();
boolean unchanged2 = booleanExpression() ? true : !booleanExpression();
boolean unchanged3 = booleanExpression() ? booleanExpression() : false;
boolean booleanExpression() {
return true;
}
}
"""
)
);
}
}

0 comments on commit 49ff90d

Please sign in to comment.