Skip to content

Instantly share code, notes, and snippets.

@MBoegers
Created June 2, 2023 11:47
Show Gist options
  • Save MBoegers/072b1412e9fee01b4728a61e68f44562 to your computer and use it in GitHub Desktop.
Save MBoegers/072b1412e9fee01b4728a61e68f44562 to your computer and use it in GitHub Desktop.
Missing space before extends/implements using J.ClassDeclaration#withExtends and J.ClassDeclaration#withImpelements
package org.openrewrite.java.tree;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.openrewrite.ExecutionContext;
import org.openrewrite.Recipe;
import org.openrewrite.Tree;
import org.openrewrite.TreeVisitor;
import org.openrewrite.java.JavaIsoVisitor;
import org.openrewrite.marker.Markers;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import static org.openrewrite.java.Assertions.java;
class WithMissPaddingReproducer {
static class ExtendWithArrayList extends Recipe {
@Override
public String getDisplayName() {
return "ExtendWithArrayList";
}
@Override
public String getDescription() {
return "Trigger ClassDeclaration#withExtends(java.util.ArrayList) for testing.";
}
@Override
protected TreeVisitor<?, ExecutionContext> getVisitor() {
return new JavaIsoVisitor<>() {
private final J.Identifier arrayList = new J.Identifier(Tree.randomId(),
Space.SINGLE_SPACE,
Markers.EMPTY,
ArrayList.class.getName(),
null,
null);
@Override
public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, ExecutionContext executionContext) {
return classDecl.withExtends(arrayList);
}
};
}
}
static class ImplementsSerializable extends Recipe {
@Override
public String getDisplayName() {
return "ImplementsSerializable";
}
@Override
public String getDescription() {
return "Trigger ClassDeclaration#withImplementation([java.io.Serializable]) for testing.";
}
@Override
protected TreeVisitor<?, ExecutionContext> getVisitor() {
return new JavaIsoVisitor<>() {
private final J.Identifier serializable = new J.Identifier(Tree.randomId(),
Space.SINGLE_SPACE,
Markers.EMPTY,
Serializable.class.getName(),
null,
null);
@Override
public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, ExecutionContext executionContext) {
return classDecl.withImplements(List.of(serializable));
}
};
}
}
@Nested
static class WithExtendsTest implements RewriteTest {
@Override
public void defaults(RecipeSpec spec) {
spec.recipe(new ExtendWithArrayList());
}
@Test
void triggerIt() {
rewriteRun(
java(
"""
package de.example;
class CustomList {
}
""",
"""
package de.example;
class CustomList extends java.util.ArrayList {
}
"""
)
);
}
}
@Nested
static class WithImplementsTest implements RewriteTest {
@Override
public void defaults(RecipeSpec spec) {
spec.recipe(new ImplementsSerializable());
}
@Test
void triggerIt() {
rewriteRun(
java(
"""
package de.example;
class CustomList {
}
""",
"""
package de.example;
class CustomList implements java.io.Serializable {
}
"""
)
);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment