Search in sources :

Example 1 with NullableNotNullManager

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

the class MethodsChainLookupRangingHelper method chainToWeightableLookupElement.

@SuppressWarnings("ConstantConditions")
@Nullable
private static LookupElement chainToWeightableLookupElement(final MethodsChain chain, final ChainCompletionContext context, final CachedRelevantStaticMethodSearcher staticMethodSearcher) {
    final int chainSize = chain.size();
    assert chainSize != 0;
    final int lastMethodWeight = chain.getChainWeight();
    int unreachableParametersCount = 0;
    int notMatchedStringVars = 0;
    int matchedParametersInContext = 0;
    Boolean isFirstMethodStatic = null;
    Boolean hasCallingVariableInContext = null;
    LookupElement chainLookupElement = null;
    PsiClass newVariableClass = null;
    final NullableNotNullManager nullableNotNullManager = NullableNotNullManager.getInstance(context.getProject());
    for (final PsiMethod[] psiMethods : chain.getPath()) {
        final PsiMethod method = MethodChainsSearchUtil.getMethodWithMinNotPrimitiveParameters(psiMethods, Collections.singleton(context.getTarget().getClassQName()));
        if (method == null) {
            return null;
        }
        if (isFirstMethodStatic == null) {
            isFirstMethodStatic = psiMethods[0].hasModifierProperty(PsiModifier.STATIC);
        }
        final PsiClass qualifierClass;
        final boolean isHead = chainLookupElement == null;
        if (isHead) {
            final String qualifierClassName = chain.getQualifierClassName();
            qualifierClass = JavaPsiFacade.getInstance(context.getProject()).findClass(qualifierClassName, context.getResolveScope());
        } else {
            qualifierClass = null;
        }
        final MethodProcResult procResult = processMethod(method, qualifierClass, isHead, lastMethodWeight, context, staticMethodSearcher, nullableNotNullManager);
        if (procResult == null) {
            return null;
        }
        if (hasCallingVariableInContext == null) {
            hasCallingVariableInContext = procResult.hasCallingVariableInContext();
        }
        if (isHead && procResult.isIntroduceNewVariable()) {
            newVariableClass = qualifierClass;
        }
        matchedParametersInContext += procResult.getMatchedParametersInContext();
        unreachableParametersCount += procResult.getUnreachableParametersCount();
        notMatchedStringVars += procResult.getNotMatchedStringVars();
        chainLookupElement = isHead ? procResult.getLookupElement() : new JavaChainLookupElement(chainLookupElement, procResult.getLookupElement());
    }
    if (newVariableClass != null) {
        chainLookupElement = ChainCompletionNewVariableLookupElement.create(newVariableClass, chainLookupElement);
    }
    final ChainRelevance relevance = new ChainRelevance(chainSize, lastMethodWeight, unreachableParametersCount, notMatchedStringVars, hasCallingVariableInContext, isFirstMethodStatic, matchedParametersInContext);
    return new WeightableChainLookupElement(chainLookupElement, relevance);
}
Also used : NullableNotNullManager(com.intellij.codeInsight.NullableNotNullManager) WeightableChainLookupElement(com.intellij.compiler.classFilesIndex.chainsSearch.completion.lookup.WeightableChainLookupElement) JavaChainLookupElement(com.intellij.codeInsight.completion.JavaChainLookupElement) SubLookupElement(com.intellij.compiler.classFilesIndex.chainsSearch.completion.lookup.sub.SubLookupElement) LookupElement(com.intellij.codeInsight.lookup.LookupElement) ChainCompletionLookupElementUtil.createLookupElement(com.intellij.compiler.classFilesIndex.chainsSearch.completion.lookup.ChainCompletionLookupElementUtil.createLookupElement) WeightableChainLookupElement(com.intellij.compiler.classFilesIndex.chainsSearch.completion.lookup.WeightableChainLookupElement) ChainCompletionNewVariableLookupElement(com.intellij.compiler.classFilesIndex.chainsSearch.completion.lookup.ChainCompletionNewVariableLookupElement) GetterLookupSubLookupElement(com.intellij.compiler.classFilesIndex.chainsSearch.completion.lookup.sub.GetterLookupSubLookupElement) VariableSubLookupElement(com.intellij.compiler.classFilesIndex.chainsSearch.completion.lookup.sub.VariableSubLookupElement) JavaChainLookupElement(com.intellij.codeInsight.completion.JavaChainLookupElement) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with NullableNotNullManager

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

the class AssignmentToNullInspection method buildFix.

@Override
protected InspectionGadgetsFix buildFix(Object... infos) {
    final Object info = infos[0];
    if (!(info instanceof PsiReferenceExpression)) {
        return null;
    }
    final PsiReferenceExpression referenceExpression = (PsiReferenceExpression) info;
    if (TypeUtils.isOptional(referenceExpression.getType())) {
        return null;
    }
    final PsiElement target = referenceExpression.resolve();
    if (!(target instanceof PsiVariable)) {
        return null;
    }
    final PsiVariable variable = (PsiVariable) target;
    if (NullableNotNullManager.isNotNull(variable)) {
        return null;
    }
    final NullableNotNullManager manager = NullableNotNullManager.getInstance(target.getProject());
    return new DelegatingFix(new AddAnnotationPsiFix(manager.getDefaultNullable(), variable, PsiNameValuePair.EMPTY_ARRAY));
}
Also used : AddAnnotationPsiFix(com.intellij.codeInsight.intention.AddAnnotationPsiFix) DelegatingFix(com.siyeh.ig.DelegatingFix) NullableNotNullManager(com.intellij.codeInsight.NullableNotNullManager)

Example 3 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 4 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)

Example 5 with NullableNotNullManager

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

the class ExtractMethodObjectProcessor method createField.

private PsiField createField(PsiParameter parameter, PsiMethod constructor, boolean isFinal) {
    final String parameterName = parameter.getName();
    PsiType type = parameter.getType();
    if (type instanceof PsiEllipsisType)
        type = ((PsiEllipsisType) type).toArrayType();
    try {
        final JavaCodeStyleManager styleManager = JavaCodeStyleManager.getInstance(getMethod().getProject());
        final String fieldName = styleManager.suggestVariableName(VariableKind.FIELD, styleManager.variableNameToPropertyName(parameterName, VariableKind.PARAMETER), null, type).names[0];
        PsiField field = myElementFactory.createField(fieldName, type);
        final PsiModifierList modifierList = field.getModifierList();
        LOG.assertTrue(modifierList != null);
        final NullableNotNullManager manager = NullableNotNullManager.getInstance(myProject);
        if (manager.isNullable(parameter, false)) {
            modifierList.addAfter(myElementFactory.createAnnotationFromText("@" + manager.getDefaultNullable(), field), null);
        }
        modifierList.setModifierProperty(PsiModifier.FINAL, isFinal);
        final PsiCodeBlock methodBody = constructor.getBody();
        LOG.assertTrue(methodBody != null);
        @NonNls final String stmtText;
        if (Comparing.strEqual(parameterName, fieldName)) {
            stmtText = "this." + fieldName + " = " + parameterName + ";";
        } else {
            stmtText = fieldName + " = " + parameterName + ";";
        }
        PsiStatement assignmentStmt = myElementFactory.createStatementFromText(stmtText, methodBody);
        assignmentStmt = (PsiStatement) CodeStyleManager.getInstance(constructor.getProject()).reformat(assignmentStmt);
        methodBody.add(assignmentStmt);
        field = (PsiField) myInnerClass.add(field);
        return field;
    } catch (IncorrectOperationException e) {
        LOG.error(e);
    }
    return null;
}
Also used : NonNls(org.jetbrains.annotations.NonNls) NullableNotNullManager(com.intellij.codeInsight.NullableNotNullManager) JavaCodeStyleManager(com.intellij.psi.codeStyle.JavaCodeStyleManager) IncorrectOperationException(com.intellij.util.IncorrectOperationException)

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