CauseItem (DfaNullability nullability, PsiModifierListOwner owner, JavaElementKind memberKind, PsiElement anchor) { if (owner != null) { NullabilityAnnotationInfo info = NullableNotNullManager.getInstance(owner.getProject()).findEffectiveNullabilityInfo(owner); String name = ((PsiNamedElement)owner).getName(); if (info != null && DfaNullability.fromNullability(info.getNullability()) == nullability) { String message; if (info.isInferred()) { if (owner instanceof PsiParameter && anchor instanceof PsiReferenceExpression && ((PsiReferenceExpression)anchor).isReferenceTo(owner)) { // Do not use inference inside method itself return null; } message = JavaAnalysisBundle .message("dfa.find.cause.nullability.inferred", memberKind.subject(), name, nullability.getPresentationName()); } else if (info.isExternal()) { message = JavaAnalysisBundle .message("dfa.find.cause.nullability.externally.annotated", memberKind.subject(), name, nullability.getPresentationName()); } else if (info.isContainer()) { PsiAnnotationOwner annoOwner = info.getAnnotation().getOwner(); message = JavaAnalysisBundle.message("dfa.find.cause.nullability.inherited.from.container", memberKind.subject(), name, nullability.getPresentationName()); if (annoOwner instanceof PsiModifierList modifierList && modifierList.getParent() instanceof PsiClass aClass) { message = JavaAnalysisBundle.message("dfa.find.cause.nullability.inherited.from.class", memberKind.subject(), name, aClass.getName(), nullability.getPresentationName()); if ("package-info".equals(aClass.getName())) { PsiFile file = aClass.getContainingFile(); if (file instanceof PsiJavaFile) { message = JavaAnalysisBundle.message("dfa.find.cause.nullability.inherited.from.package", memberKind.subject(), name, ((PsiJavaFile)file).getPackageName(), nullability.getPresentationName()); } } } if (annoOwner instanceof PsiNamedElement) { message = JavaAnalysisBundle.message("dfa.find.cause.nullability.inherited.from.named.element", memberKind.subject(), name, ((PsiNamedElement)annoOwner).getName(), nullability.getPresentationName()); } } else { message = JavaAnalysisBundle.message("dfa.find.cause.nullability.explicitly.annotated", memberKind.subject(), name, nullability.getPresentationName()); } if (info.getAnnotation().getContainingFile() == anchor.getContainingFile()) { anchor = info.getAnnotation(); } else if (owner.getContainingFile() == anchor.getContainingFile()) { anchor = owner.getNavigationElement(); if (anchor instanceof PsiNameIdentifierOwner) { anchor = ((PsiNameIdentifierOwner)anchor).getNameIdentifier(); } } return new CauseItem(message, anchor); } if (owner instanceof PsiField && FieldChecker.getChecker(getFactory().getContext()).canTrustFieldInitializer((PsiField)owner)) { Pair<PsiExpression, Nullability> fieldNullability = NullabilityUtil.getNullabilityFromFieldInitializers((PsiField)owner); if (fieldNullability.second == DfaNullability.toNullability(nullability)) { PsiExpression initializer = fieldNullability.first; if (initializer != null) { if (initializer.getContainingFile() == anchor.getContainingFile()) { anchor = initializer; } return new CauseItem(JavaAnalysisBundle.message("dfa.find.cause.field.initializer.nullability", name, DfaNullability.fromNullability(fieldNullability.second).getPresentationName()), anchor); } if (owner.getContainingFile() == anchor.getContainingFile()) { anchor = owner; } return new CauseItem(JavaAnalysisBundle.message("dfa.find.cause.field.assigned.nullability", name, DfaNullability.fromNullability(fieldNullability.second).getPresentationName()), anchor); } } } return null; }