From e089b0667f7c3ddd8a550eedeadf962a3c3c9b39 Mon Sep 17 00:00:00 2001 From: Manu Sridharan Date: Sat, 26 Oct 2024 17:19:33 -0700 Subject: [PATCH] Handle records in `targetTypeMatches` --- .../com/uber/nullaway/jdk17/RecordTests.java | 23 +++++++++++++++++++ .../com/uber/nullaway/NullabilityUtil.java | 8 ++++++- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/jdk-recent-unit-tests/src/test/java/com/uber/nullaway/jdk17/RecordTests.java b/jdk-recent-unit-tests/src/test/java/com/uber/nullaway/jdk17/RecordTests.java index 4e83490177..7d682d6437 100644 --- a/jdk-recent-unit-tests/src/test/java/com/uber/nullaway/jdk17/RecordTests.java +++ b/jdk-recent-unit-tests/src/test/java/com/uber/nullaway/jdk17/RecordTests.java @@ -252,4 +252,27 @@ public void recordDeconstructionPatternSwitchCase() { "}") .doTest(); } + + @Test + public void issue1059() { + defaultCompilationHelper + .addSourceLines( + "BarEntity.java", + "package com.uber;", + "import org.jspecify.annotations.NonNull;", + "import org.jspecify.annotations.Nullable;", + "public class BarEntity {", + " public interface Identifiable {", + " @Nullable", + " ID id();", + " }", + " public static class Id {}", + " public record NameChanged(BarEntity.Id id, Class type) implements Identifiable<@NonNull Id> {", + " public NameChanged(BarEntity.Id id) {", + " this(id, BarEntity.class);", + " }", + " }", + "}") + .doTest(); + } } diff --git a/nullaway/src/main/java/com/uber/nullaway/NullabilityUtil.java b/nullaway/src/main/java/com/uber/nullaway/NullabilityUtil.java index eee5eb88b5..fc93add428 100644 --- a/nullaway/src/main/java/com/uber/nullaway/NullabilityUtil.java +++ b/nullaway/src/main/java/com/uber/nullaway/NullabilityUtil.java @@ -370,7 +370,13 @@ private static boolean targetTypeMatches(Symbol sym, TypeAnnotationPosition posi // on other types in the signature (e.g. `class Foo extends Bar<@A Baz> {}`). return false; default: - throw new AssertionError("unsupported element kind " + sym.getKind() + " symbol " + sym); + // Compare with toString() to preserve compatibility with JDK 11 + if (sym.getKind().toString().equals("RECORD")) { + // Records are treated like classes + return false; + } else { + throw new AssertionError("unsupported element kind " + sym.getKind() + " symbol " + sym); + } } }