Skip to content

Commit b8a504e

Browse files
committed
Avoid NPE on record.getClazz()
Fixes #767
1 parent ef0b38b commit b8a504e

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

src/main/java/org/openrewrite/staticanalysis/OperatorWrap.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ public J.TypeParameter visitTypeParameter(J.TypeParameter typeParam, ExecutionCo
179179
@Override
180180
public J.InstanceOf visitInstanceOf(J.InstanceOf instanceOf, ExecutionContext ctx) {
181181
J.InstanceOf i = super.visitInstanceOf(instanceOf, ctx);
182-
if (Boolean.TRUE.equals(operatorWrapStyle.getLiteralInstanceof())) {
182+
if (Boolean.TRUE.equals(operatorWrapStyle.getLiteralInstanceof()) && i.getClazz() != null) {
183183
if (OperatorWrapStyle.WrapOption.NL == operatorWrapStyle.getWrapOption()) {
184184
if (i.getClazz().getPrefix().getWhitespace().contains("\n")) {
185185
i = i.getPadding().withExpression(

src/test/java/org/openrewrite/staticanalysis/OperatorWrapTest.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -672,4 +672,35 @@ static void method() {
672672
)
673673
);
674674
}
675+
676+
@Test
677+
void recordPatternInstanceOf() {
678+
rewriteRun(
679+
spec -> spec.parser(JavaParser.fromJavaVersion().styles(operatorWrapStyle())),
680+
//language=java
681+
java(
682+
"""
683+
import java.util.Arrays;
684+
import java.util.Objects;
685+
686+
record SomeRecord(int field, byte[] value) {
687+
@Override
688+
public boolean equals(Object o) {
689+
if (this == o) return true;
690+
if (!(o instanceof SomeRecord f)) {
691+
return false;
692+
}
693+
return field == f.field
694+
&& Arrays.equals(value, f.value);
695+
}
696+
697+
@Override
698+
public int hashCode() {
699+
return 31 * Objects.hash(field) + Arrays.hashCode(value);
700+
}
701+
}
702+
"""
703+
)
704+
);
705+
}
675706
}

0 commit comments

Comments
 (0)