mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
[java-inspections] NullableStuffInspectionBase: do not report "non-annotated overrides annotated" in the presense of container annotation
Fixes IDEA-323362 IntelliJ gives "Not annotated method overrides method annotated with @Nonnull" despite @NonNullApi GitOrigin-RevId: 1b4298ffc3a1fc9ce4f4e5b9412e3af5a3b116d9
This commit is contained in:
committed by
intellij-monorepo-bot
parent
f46f96d05f
commit
5f3ce51f8a
+3
-2
@@ -797,9 +797,10 @@ public class NullableStuffInspectionBase extends AbstractBaseJavaLocalInspection
|
||||
if (REPORT_NOT_ANNOTATED_METHOD_OVERRIDES_NOTNULL &&
|
||||
!(method.getReturnType() instanceof PsiPrimitiveType) &&
|
||||
!method.isConstructor()) {
|
||||
NullabilityAnnotationInfo info = getNullityManager(method).findEffectiveNullabilityInfo(method);
|
||||
NullableNotNullManager manager = getNullityManager(method);
|
||||
NullabilityAnnotationInfo info = manager.findEffectiveNullabilityInfo(method);
|
||||
if ((info == null || info.isInferred() ||
|
||||
(!info.isContainer() && !info.isExternal() && !PsiTreeUtil.isAncestor(method, info.getAnnotation(), true))) &&
|
||||
(info.getInheritedFrom() != null && manager.findContainerAnnotation(method) == null)) &&
|
||||
isNotNullNotInferred(superMethod, true, IGNORE_EXTERNAL_SUPER_NOTNULL) &&
|
||||
!hasInheritableNotNull(superMethod)) {
|
||||
return true;
|
||||
|
||||
@@ -127,7 +127,7 @@ public abstract class NullableNotNullManager {
|
||||
if (result != null) {
|
||||
return result;
|
||||
}
|
||||
return findNullityDefaultInHierarchy(owner);
|
||||
return findContainerAnnotation(owner);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -166,7 +166,7 @@ public abstract class NullableNotNullManager {
|
||||
NullabilityAnnotationInfo plain = findPlainAnnotation(parameter, false, false, getAllNullabilityAnnotationsWithNickNames());
|
||||
// Plain not null annotation is not inherited
|
||||
if (plain != null) return null;
|
||||
NullabilityAnnotationInfo defaultInfo = findNullityDefaultInHierarchy(parameter);
|
||||
NullabilityAnnotationInfo defaultInfo = findContainerAnnotation(parameter);
|
||||
if (defaultInfo != null) {
|
||||
return defaultInfo.getNullability() == Nullability.NOT_NULL ? defaultInfo.withInheritedFrom(parameter) : null;
|
||||
}
|
||||
@@ -182,7 +182,7 @@ public abstract class NullableNotNullManager {
|
||||
}
|
||||
|
||||
private @Nullable NullabilityAnnotationInfo findNullityDefaultFiltered(@NotNull PsiModifierListOwner owner) {
|
||||
NullabilityAnnotationInfo defaultInfo = findNullityDefaultInHierarchy(owner);
|
||||
NullabilityAnnotationInfo defaultInfo = findContainerAnnotation(owner);
|
||||
if (defaultInfo != null && (defaultInfo.getNullability() == Nullability.NULLABLE || !hasHardcodedContracts(owner))) {
|
||||
return defaultInfo;
|
||||
}
|
||||
@@ -294,7 +294,15 @@ public abstract class NullableNotNullManager {
|
||||
return findNullabilityDefault(context, PsiAnnotation.TargetType.TYPE_USE);
|
||||
}
|
||||
|
||||
@Nullable NullabilityAnnotationInfo findNullityDefaultInHierarchy(@NotNull PsiModifierListOwner owner) {
|
||||
/**
|
||||
* Looks for applicable container annotation, ignoring explicit, inferred, external, or inherited annotations.
|
||||
* Usually, should not be used directly, as {@link #findEffectiveNullabilityInfo(PsiModifierListOwner)} will
|
||||
* return container annotation if it's applicable.
|
||||
*
|
||||
* @param owner member to find annotation for
|
||||
* @return container annotation applicable to the owner location
|
||||
*/
|
||||
public @Nullable NullabilityAnnotationInfo findContainerAnnotation(@NotNull PsiModifierListOwner owner) {
|
||||
return findNullabilityDefault(owner, AnnotationTargetUtil.getTargetsForLocation(owner.getModifierList()));
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
import javax.annotation.Nonnull;
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
class Test {
|
||||
static class Super {
|
||||
@Nonnull
|
||||
public String foo(@Nonnull String foo) {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
@NonNullApi
|
||||
static class FooSub extends Super {
|
||||
|
||||
@Override
|
||||
public String foo(String foo) {
|
||||
return super.foo(foo);
|
||||
}
|
||||
}
|
||||
|
||||
@Target(ElementType.TYPE)
|
||||
@javax.annotation.Nonnull
|
||||
@javax.annotation.meta.TypeQualifierDefault({ElementType.METHOD, ElementType.PARAMETER})
|
||||
public @interface NonNullApi {
|
||||
}
|
||||
}
|
||||
+6
@@ -248,6 +248,12 @@ public class NullableStuffInspectionTest extends LightJavaCodeInsightFixtureTest
|
||||
myFixture.enableInspections(myInspection);
|
||||
myFixture.checkHighlighting(true, false, true);
|
||||
}
|
||||
|
||||
public void testDefaultOverridesExplicit() {
|
||||
DataFlowInspectionTest.addJavaxNullabilityAnnotations(myFixture);
|
||||
DataFlowInspectionTest.addJavaxDefaultNullabilityAnnotations(myFixture);
|
||||
doTest();
|
||||
}
|
||||
|
||||
public void testBeanValidationNotNull() {
|
||||
myFixture.addClass("package javax.annotation.constraints; public @interface NotNull{}");
|
||||
|
||||
Reference in New Issue
Block a user