Search in sources :

Example 6 with NullableNotNullManager

use of com.intellij.codeInsight.NullableNotNullManager in project intellij-community by JetBrains.

the class NullableStuffInspectionBase method isNotNullNotInferred.

private static boolean isNotNullNotInferred(@NotNull PsiModifierListOwner owner, boolean checkBases, boolean skipExternal) {
    Project project = owner.getProject();
    NullableNotNullManager manager = NullableNotNullManager.getInstance(project);
    if (!manager.isNotNull(owner, checkBases))
        return false;
    PsiAnnotation anno = manager.getNotNullAnnotation(owner, checkBases);
    if (anno == null || AnnotationUtil.isInferredAnnotation(anno))
        return false;
    if (skipExternal && AnnotationUtil.isExternalAnnotation(anno))
        return false;
    return true;
}
Also used : Project(com.intellij.openapi.project.Project) NullableNotNullManager(com.intellij.codeInsight.NullableNotNullManager)

Example 7 with NullableNotNullManager

use of com.intellij.codeInsight.NullableNotNullManager in project intellij-community by JetBrains.

the class NullableStuffInspectionBase method checkNullableStuffForMethod.

private void checkNullableStuffForMethod(PsiMethod method, final ProblemsHolder holder, boolean isOnFly) {
    Annotated annotated = check(method, holder, method.getReturnType());
    List<PsiMethod> superMethods = ContainerUtil.map(method.findSuperMethodSignaturesIncludingStatic(true), signature -> signature.getMethod());
    final NullableNotNullManager nullableManager = NullableNotNullManager.getInstance(holder.getProject());
    checkSupers(method, holder, annotated, superMethods);
    checkParameters(method, holder, superMethods, nullableManager, isOnFly);
    checkOverriders(method, holder, annotated, nullableManager);
}
Also used : NullableNotNullManager(com.intellij.codeInsight.NullableNotNullManager)

Example 8 with NullableNotNullManager

use of com.intellij.codeInsight.NullableNotNullManager in project intellij-community by JetBrains.

the class DfaPsiUtil method getElementNullability.

@NotNull
public static Nullness getElementNullability(@Nullable PsiType resultType, @Nullable PsiModifierListOwner owner) {
    if (resultType != null) {
        for (PsiAnnotation annotation : resultType.getAnnotations()) {
            String qualifiedName = annotation.getQualifiedName();
            NullableNotNullManager nnn = NullableNotNullManager.getInstance(annotation.getProject());
            if (nnn.getNullables().contains(qualifiedName)) {
                return Nullness.NULLABLE;
            }
            if (nnn.getNotNulls().contains(qualifiedName)) {
                return Nullness.NOT_NULL;
            }
        }
    }
    if (owner == null || resultType instanceof PsiPrimitiveType) {
        return Nullness.UNKNOWN;
    }
    if (owner instanceof PsiEnumConstant || PsiUtil.isAnnotationMethod(owner)) {
        return Nullness.NOT_NULL;
    }
    if (owner instanceof PsiMethod && isEnumPredefinedMethod((PsiMethod) owner)) {
        return Nullness.NOT_NULL;
    }
    if (NullableNotNullManager.isNullable(owner)) {
        return Nullness.NULLABLE;
    }
    if (isNotNullLocally(owner)) {
        return Nullness.NOT_NULL;
    }
    if (PsiJavaPatterns.psiParameter().withParents(PsiParameterList.class, PsiLambdaExpression.class).accepts(owner)) {
        PsiLambdaExpression lambda = (PsiLambdaExpression) owner.getParent().getParent();
        int index = lambda.getParameterList().getParameterIndex((PsiParameter) owner);
        Nullness nullness = inferLambdaParameterNullness(lambda, index);
        if (nullness != Nullness.UNKNOWN) {
            return nullness;
        }
        PsiMethod sam = LambdaUtil.getFunctionalInterfaceMethod(lambda.getFunctionalInterfaceType());
        if (sam != null && index < sam.getParameterList().getParametersCount()) {
            return getElementNullability(null, sam.getParameterList().getParameters()[index]);
        }
    }
    return Nullness.UNKNOWN;
}
Also used : NullableNotNullManager(com.intellij.codeInsight.NullableNotNullManager) NotNull(org.jetbrains.annotations.NotNull)

Example 9 with NullableNotNullManager

use of com.intellij.codeInsight.NullableNotNullManager in project intellij-community by JetBrains.

the class OverrideImplementsAnnotationsHandlerImpl method getAnnotations.

@Override
public String[] getAnnotations(Project project) {
    final NullableNotNullManager manager = NullableNotNullManager.getInstance(project);
    final Collection<String> anns = new ArrayList<>(manager.getNotNulls());
    anns.addAll(manager.getNullables());
    anns.add(AnnotationUtil.NLS);
    final CodeStyleSettings settings = CodeStyleSettingsManager.getSettings(project);
    anns.addAll(settings.getRepeatAnnotations());
    return ArrayUtil.toStringArray(anns);
}
Also used : CodeStyleSettings(com.intellij.psi.codeStyle.CodeStyleSettings) NullableNotNullManager(com.intellij.codeInsight.NullableNotNullManager) ArrayList(java.util.ArrayList)

Example 10 with NullableNotNullManager

use of com.intellij.codeInsight.NullableNotNullManager in project intellij-community by JetBrains.

the class ExtractMethodProcessor method initNullness.

private Nullness initNullness() {
    if (!PsiUtil.isLanguageLevel5OrHigher(myElements[0]) || PsiUtil.resolveClassInType(myReturnType) == null)
        return null;
    final NullableNotNullManager manager = NullableNotNullManager.getInstance(myProject);
    final PsiClass nullableAnnotationClass = JavaPsiFacade.getInstance(myProject).findClass(manager.getDefaultNullable(), myElements[0].getResolveScope());
    if (nullableAnnotationClass != null) {
        final PsiElement elementInCopy = myTargetClass.getContainingFile().copy().findElementAt(myTargetClass.getTextOffset());
        final PsiClass classCopy = PsiTreeUtil.getParentOfType(elementInCopy, PsiClass.class);
        if (classCopy == null) {
            return null;
        }
        final PsiMethod emptyMethod = (PsiMethod) classCopy.addAfter(generateEmptyMethod("name"), classCopy.getLBrace());
        prepareMethodBody(emptyMethod, false);
        if (myNotNullConditionalCheck || myNullConditionalCheck) {
            return Nullness.NULLABLE;
        }
        return DfaUtil.inferMethodNullity(emptyMethod);
    }
    return null;
}
Also used : NullableNotNullManager(com.intellij.codeInsight.NullableNotNullManager)

Aggregations

NullableNotNullManager (com.intellij.codeInsight.NullableNotNullManager)22 Project (com.intellij.openapi.project.Project)6 NotNull (org.jetbrains.annotations.NotNull)4 Nullable (org.jetbrains.annotations.Nullable)3 JavaCodeStyleManager (com.intellij.psi.codeStyle.JavaCodeStyleManager)2 NonNls (org.jetbrains.annotations.NonNls)2 CodeInsightUtil (com.intellij.codeInsight.CodeInsightUtil)1 ExternalAnnotationsManager (com.intellij.codeInsight.ExternalAnnotationsManager)1 JavaChainLookupElement (com.intellij.codeInsight.completion.JavaChainLookupElement)1 JavaCompletionUtil (com.intellij.codeInsight.completion.JavaCompletionUtil)1 HighlightManager (com.intellij.codeInsight.highlighting.HighlightManager)1 AddAnnotationPsiFix (com.intellij.codeInsight.intention.AddAnnotationPsiFix)1 AddNullableNotNullAnnotationFix (com.intellij.codeInsight.intention.impl.AddNullableNotNullAnnotationFix)1 LookupElement (com.intellij.codeInsight.lookup.LookupElement)1 LookupManager (com.intellij.codeInsight.lookup.LookupManager)1 ScopeHighlighter (com.intellij.codeInsight.unwrap.ScopeHighlighter)1 AnnotateMethodFix (com.intellij.codeInspection.AnnotateMethodFix)1 ChainCompletionLookupElementUtil.createLookupElement (com.intellij.compiler.classFilesIndex.chainsSearch.completion.lookup.ChainCompletionLookupElementUtil.createLookupElement)1 ChainCompletionNewVariableLookupElement (com.intellij.compiler.classFilesIndex.chainsSearch.completion.lookup.ChainCompletionNewVariableLookupElement)1 WeightableChainLookupElement (com.intellij.compiler.classFilesIndex.chainsSearch.completion.lookup.WeightableChainLookupElement)1