Search in sources :

Example 11 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)

Example 12 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 13 with NullableNotNullManager

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

the class FieldFromParameterUtils method createFieldAndAddAssignment.

public static void createFieldAndAddAssignment(@NotNull final Project project, @NotNull final PsiClass targetClass, @NotNull final PsiMethod method, @NotNull final PsiParameter parameter, @NotNull final PsiType fieldType, @NotNull final String fieldName, final boolean isStatic, final boolean isFinal) throws IncorrectOperationException {
    PsiManager psiManager = PsiManager.getInstance(project);
    final JavaPsiFacade psiFacade = JavaPsiFacade.getInstance(psiManager.getProject());
    PsiElementFactory factory = psiFacade.getElementFactory();
    PsiField field = factory.createField(fieldName, fieldType);
    PsiModifierList modifierList = field.getModifierList();
    if (modifierList == null)
        return;
    modifierList.setModifierProperty(PsiModifier.STATIC, isStatic);
    modifierList.setModifierProperty(PsiModifier.FINAL, isFinal);
    NullableNotNullManager manager = NullableNotNullManager.getInstance(project);
    if (manager.copyNullableAnnotation(parameter, field) == null && isFinal) {
        manager.copyNotNullAnnotation(parameter, field);
    }
    PsiCodeBlock methodBody = method.getBody();
    if (methodBody == null)
        return;
    PsiStatement[] statements = methodBody.getStatements();
    Ref<Pair<PsiField, Boolean>> anchorRef = new Ref<>();
    int i = findFieldAssignmentAnchor(statements, anchorRef, targetClass, parameter);
    Pair<PsiField, Boolean> fieldAnchor = anchorRef.get();
    String stmtText = fieldName + " = " + parameter.getName() + ";";
    final PsiVariable variable = psiFacade.getResolveHelper().resolveReferencedVariable(fieldName, methodBody);
    if (variable != null && !(variable instanceof PsiField)) {
        String prefix = isStatic ? targetClass.getName() == null ? "" : targetClass.getName() + "." : "this.";
        stmtText = prefix + stmtText;
    }
    PsiStatement assignmentStmt = factory.createStatementFromText(stmtText, methodBody);
    assignmentStmt = (PsiStatement) CodeStyleManager.getInstance(project).reformat(assignmentStmt);
    if (i == statements.length) {
        methodBody.add(assignmentStmt);
    } else {
        methodBody.addAfter(assignmentStmt, i > 0 ? statements[i - 1] : null);
    }
    if (fieldAnchor != null) {
        PsiVariable psiVariable = fieldAnchor.getFirst();
        psiVariable.normalizeDeclaration();
    }
    if (targetClass.findFieldByName(fieldName, false) == null) {
        if (fieldAnchor != null) {
            Boolean insertBefore = fieldAnchor.getSecond();
            PsiField inField = fieldAnchor.getFirst();
            if (insertBefore.booleanValue()) {
                targetClass.addBefore(field, inField);
            } else {
                targetClass.addAfter(field, inField);
            }
        } else {
            targetClass.add(field);
        }
    }
}
Also used : NullableNotNullManager(com.intellij.codeInsight.NullableNotNullManager) Ref(com.intellij.openapi.util.Ref) Pair(com.intellij.openapi.util.Pair)

Example 14 with NullableNotNullManager

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

the class NullableStuffInspectionTest method testOverrideCustomDefault.

public void testOverrideCustomDefault() {
    DataFlowInspectionTest.addJavaxNullabilityAnnotations(myFixture);
    myFixture.addClass("package custom;" + "public @interface CheckForNull {}");
    final NullableNotNullManager nnnManager = NullableNotNullManager.getInstance(getProject());
    nnnManager.setNullables("custom.CheckForNull");
    Disposer.register(myFixture.getTestRootDisposable(), new Disposable() {

        @Override
        public void dispose() {
            nnnManager.setNullables();
        }
    });
    myFixture.addClass("package foo;" + "import static java.lang.annotation.ElementType.*;" + "@javax.annotation.meta.TypeQualifierDefault(METHOD) " + "@javax.annotation.Nonnull " + "public @interface ReturnValuesAreNonnullByDefault {}");
    myFixture.addFileToProject("foo/package-info.java", "@ReturnValuesAreNonnullByDefault package foo;");
    myFixture.configureFromExistingVirtualFile(myFixture.copyFileToProject(getTestName(false) + ".java", "foo/Classes.java"));
    myFixture.enableInspections(myInspection);
    myFixture.checkHighlighting(true, false, true);
}
Also used : Disposable(com.intellij.openapi.Disposable) NullableNotNullManager(com.intellij.codeInsight.NullableNotNullManager)

Example 15 with NullableNotNullManager

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

the class DataFlowInspection8Test method setCustomAnnotations.

static void setCustomAnnotations(Project project, Disposable parentDisposable, String notNull, String nullable) {
    NullableNotNullManager nnnManager = NullableNotNullManager.getInstance(project);
    nnnManager.setNotNulls(notNull);
    nnnManager.setNullables(nullable);
    Disposer.register(parentDisposable, () -> {
        nnnManager.setNotNulls();
        nnnManager.setNullables();
    });
}
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