Skip to content

Commit 753fd36

Browse files
fix IndexOutOfBoundsException that occurs in UseNoArgsConstructor when visiting record compact constructor method (#964)
* fix IndexOutOfBoundsException * fix * fix --------- Co-authored-by: zakaria-shahen <zakaria-shahen@users.noreply.github.com> Co-authored-by: Tim te Beek <tim@moderne.io>
1 parent 9b9e288 commit 753fd36

2 files changed

Lines changed: 22 additions & 2 deletions

File tree

src/main/java/org/openrewrite/java/migrate/lombok/UseNoArgsConstructor.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.openrewrite.java.JavaIsoVisitor;
2626
import org.openrewrite.java.JavaParser;
2727
import org.openrewrite.java.JavaTemplate;
28+
import org.openrewrite.java.marker.CompactConstructor;
2829
import org.openrewrite.java.tree.J;
2930
import org.openrewrite.java.tree.TypeUtils;
3031

@@ -51,8 +52,9 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
5152
return new JavaIsoVisitor<ExecutionContext>() {
5253
@Override
5354
public J.@Nullable MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, ExecutionContext ctx) {
54-
if (method.isConstructor() &&
55-
method.getParameters().get(0) instanceof J.Empty &&
55+
if (method.isConstructor() &&
56+
!method.getMarkers().findFirst(CompactConstructor.class).isPresent() &&
57+
method.getParameters().get(0) instanceof J.Empty &&
5658
method.getBody() != null && method.getBody().getStatements().isEmpty()) {
5759
J.ClassDeclaration enclosing = getCursor().firstEnclosing(J.ClassDeclaration.class);
5860
AccessLevel accessLevel = LombokUtils.getAccessLevel(method);

src/test/java/org/openrewrite/java/migrate/lombok/UseNoArgsConstructorTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,24 @@ public A() {
6969
);
7070
}
7171

72+
@Test
73+
void keepCompactConstructor() {
74+
rewriteRun(
75+
//language=java
76+
java(
77+
"""
78+
public record Foo(String id) {
79+
public Foo {
80+
if (id == null || id.isBlank()) {
81+
throw new IllegalArgumentException("ID cannot be null or blank");
82+
}
83+
}
84+
}
85+
"""
86+
)
87+
);
88+
}
89+
7290
@Test
7391
void replaceEmptyProtectedConstructor() {
7492
rewriteRun(

0 commit comments

Comments
 (0)