diff --git a/java/java-psi-api/src/com/intellij/codeInsight/NullableNotNullManager.java b/java/java-psi-api/src/com/intellij/codeInsight/NullableNotNullManager.java index e560522d666a..c1c286891640 100644 --- a/java/java-psi-api/src/com/intellij/codeInsight/NullableNotNullManager.java +++ b/java/java-psi-api/src/com/intellij/codeInsight/NullableNotNullManager.java @@ -350,7 +350,7 @@ public abstract class NullableNotNullManager { private @Nullable NullabilityAnnotationInfo findNullabilityDefault(@NotNull PsiElement place, @NotNull PsiAnnotation.TargetType @NotNull ... placeTargetTypes) { - PsiElement element = place.getContext(); + PsiElement element = place; while (element != null) { if (element instanceof PsiModifierListOwner) { NullabilityAnnotationInfo result = getNullityDefault((PsiModifierListOwner)element, placeTargetTypes).forContext(place); diff --git a/java/java-tests/testData/inspection/dataFlow/fixture/JSpecifyNullUnmarkedOverNullMarked.java b/java/java-tests/testData/inspection/dataFlow/fixture/JSpecifyNullUnmarkedOverNullMarked.java new file mode 100644 index 000000000000..c33e11296077 --- /dev/null +++ b/java/java-tests/testData/inspection/dataFlow/fixture/JSpecifyNullUnmarkedOverNullMarked.java @@ -0,0 +1,14 @@ +import org.jspecify.annotations.NullMarked; +import org.jspecify.annotations.NullUnmarked; +import org.jspecify.annotations.Nullable; + +import java.util.List; + +@NullMarked +class Container { + + @NullUnmarked + List get() { + return null; + } +} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/java/codeInspection/DataFlowInspection21Test.java b/java/java-tests/testSrc/com/intellij/java/codeInspection/DataFlowInspection21Test.java index 88c174f50db7..90e6ffd496ab 100644 --- a/java/java-tests/testSrc/com/intellij/java/codeInspection/DataFlowInspection21Test.java +++ b/java/java-tests/testSrc/com/intellij/java/codeInspection/DataFlowInspection21Test.java @@ -221,5 +221,11 @@ public class DataFlowInspection21Test extends DataFlowInspectionTestCase { addJSpecifyNullMarked(myFixture); doTest(); } + + public void testJSpecifyNullUnmarkedOverNullMarked() { + addJSpecifyNullMarked(myFixture); + setupTypeUseAnnotations("org.jspecify.annotations", myFixture); + doTest(); + } } \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/java/codeInspection/DataFlowInspectionTestCase.java b/java/java-tests/testSrc/com/intellij/java/codeInspection/DataFlowInspectionTestCase.java index 585606b47974..bf393b7bfd46 100644 --- a/java/java-tests/testSrc/com/intellij/java/codeInspection/DataFlowInspectionTestCase.java +++ b/java/java-tests/testSrc/com/intellij/java/codeInspection/DataFlowInspectionTestCase.java @@ -63,9 +63,16 @@ public abstract class DataFlowInspectionTestCase extends LightJavaCodeInsightFix """ package org.jspecify.annotations; import java.lang.annotation.*; - @Target({ElementType.TYPE, ElementType.MODULE}) + @Target({ElementType.TYPE, ElementType.METHOD, ElementType.MODULE}) public @interface NullMarked {}"""; fixture.addClass(nullMarked); + @Language("JAVA") String nullUnmarked = + """ + package org.jspecify.annotations; + import java.lang.annotation.*; + @Target({ElementType.TYPE, ElementType.METHOD, ElementType.MODULE}) + public @interface NullUnmarked {}"""; + fixture.addClass(nullUnmarked); } public static void setupTypeUseAnnotations(String pkg, JavaCodeInsightTestFixture fixture) {