Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.openrewrite.java.JavaIsoVisitor;
import org.openrewrite.java.JavaParser;
import org.openrewrite.java.JavaTemplate;
import org.openrewrite.java.marker.CompactConstructor;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.TypeUtils;

Expand All @@ -51,8 +52,9 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
return new JavaIsoVisitor<ExecutionContext>() {
@Override
public J.@Nullable MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, ExecutionContext ctx) {
if (method.isConstructor() &&
method.getParameters().get(0) instanceof J.Empty &&
if (method.isConstructor() &&
!method.getMarkers().findFirst(CompactConstructor.class).isPresent() &&
method.getParameters().get(0) instanceof J.Empty &&
method.getBody() != null && method.getBody().getStatements().isEmpty()) {
J.ClassDeclaration enclosing = getCursor().firstEnclosing(J.ClassDeclaration.class);
AccessLevel accessLevel = LombokUtils.getAccessLevel(method);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,24 @@ public A() {
);
}

@Test
void keepCompactConstructor() {
rewriteRun(
//language=java
java(
"""
public record Foo(String id) {
public Foo {
if (id == null || id.isBlank()) {
throw new IllegalArgumentException("ID cannot be null or blank");
}
}
}
"""
)
);
}

@Test
void replaceEmptyProtectedConstructor() {
rewriteRun(
Expand Down