Klassisch
String query =
"SELECT * FROM\n" +
"my_table\n" +
"WHERE something = 1;";
Merlin Bögershausen / @mboegie
Refactoring is a controlled technique for improving the design of an existing code base. Its essence is applying a series of small behavior-preserving transformations, each of which "too small to be worth doing".
Klassisch
String query =
"SELECT * FROM\n" +
"my_table\n" +
"WHERE something = 1;";
Java 15+
String query = """
SELECT * FROM
my_table
WHERE something = 1;\
""";
mvn -U org.openrewrite.maven:rewrite-maven-plugin:run
<plugin>
<groupId>org.openrewrite.maven</groupId>
<artifactId>rewrite-maven-plugin</artifactId>
<version>4.45.0</version>
</plugin>
-Drewrite.activeRecipes=\
org.openrewrite.java.RemoveUnusedImports
<configuration>
<activeRecipes>
<recipe>
org.openrewrite.java.RemoveUnusedImports
</recipe>
</activeRecipes>
</configuration>
./mvnw rewrite:run
Changes have been made to .github/workflows/maven-build.yml by:
org.openrewrite.java.spring.boot3.UpgradeSpringBoot_3_0
org.openrewrite.java.spring.boot3.MavenPomUpgrade
org.openrewrite.java.migrate.JavaVersion17
org.openrewrite.java.migrate.UpgradeJavaVersion:
{version=17}
org.openrewrite.java.migrate.UpgradeToJava17
org.openrewrite.github.SetupJavaUpgradeJavaVersion
// 0
class A { // 1
void test() {
int a;
a = 0;
}
}
-J.CompilationUnit // 0
\-J.ClassDeclaration // 1 - start
|-J.Identifier | "A"
\-J.Block // 1 - end
\-J.MethodDeclaration | "MethodDeclaration{A{name=test,return=void,parameters=[]}}"
Recipe ein ausführbares Refactoring
public class MakePublic extends Recipe {
@Override
protected JavaVisitor<ExecutionContext> getVisitor() {
return new MakePublicVisitor(); // 1
}
public String getDisplayName() {
return "Change class visibility to public 🤡";
}
private class MakePublicVisitor
extends JavaVisitor<ExecutionContext> {}
}
Bearbeite den LST mit Visitors
class JavaVisitor<P> extends TreeVisitor<J, P> {
J visitStatement(Statement statement) {}
J visitAnnotatedType(J.AnnotatedType annotatedType) {}
J visitAnnotation(J.Annotation annotation) {}
J visitAssert(J.Assert azzert) {}
J visitAssignment(J.Assignment assign) {}
J visitAssignmentOperation(J.AssignmentOperation assignOp) {}
//...
}
new JavaIsoVisitor<ExecutionContext> {
public J.ClassDeclaration visitClassDeclaration(
J.ClassDeclaration cd, ExecutionContext ctx) { // 1
cd = super.visitClassDeclaration(cd, ctx);
List<J.Modifier> modifiers = cd.getModifiers(); // 2
modifiers.removeIf( //2
m -> J.Modifier.Type.Private.equals(m.getType()));
// and Protected & Publicπ
modifiers.add(PUBLIC_MODIFIER); // 3
return cd.withModifiers(modifiers); // 4
}
}
Be declarative!
---
type: specs.openrewrite.org/v1beta/recipe
name: io.github.mboegers.rewrite.MakePublic
displayName: Change method access level to public
recipeList:
- org.openrewrite.java.ChangeMethodAccessLevel:
methodPattern: *
newAccessLevel: public
matchOverrides: null
type: specs.openrewrite.org/v1beta/style
name: io.moderne.spring.style
styleConfigs:
- org.openrewrite.java.style.NeedBracesStyle:
allowSingleLineStatement: false
allowEmptyLoopBody: true
for(int i = 0; i < 10; i++);
if(success()) return false;
for(int i = 0; i < 10; i++);
if(success()){
return false;
}
Project Logos from respective Project Page
Migration Types from Moderne Prove of Value
Moderne UI Screenshot by Author