Search in sources :

Example 81 with HighlightInfo

use of com.intellij.codeInsight.daemon.impl.HighlightInfo in project intellij-community by JetBrains.

the class HighlightUtil method checkNotAllowedModifier.

@Nullable
static HighlightInfo checkNotAllowedModifier(@NotNull PsiKeyword keyword, @NotNull PsiModifierList modifierList) {
    PsiElement modifierOwner = modifierList.getParent();
    Map<String, Set<String>> incompatibleModifierMap = getIncompatibleModifierMap(modifierOwner);
    if (incompatibleModifierMap == null)
        return null;
    @PsiModifier.ModifierConstant String modifier = keyword.getText();
    Set<String> incompatibles = incompatibleModifierMap.get(modifier);
    PsiElement modifierOwnerParent = modifierOwner instanceof PsiMember ? ((PsiMember) modifierOwner).getContainingClass() : modifierOwner.getParent();
    if (modifierOwnerParent == null)
        modifierOwnerParent = modifierOwner.getParent();
    boolean isAllowed = true;
    if (modifierOwner instanceof PsiClass) {
        PsiClass aClass = (PsiClass) modifierOwner;
        if (aClass.isInterface()) {
            if (PsiModifier.STATIC.equals(modifier) || PsiModifier.PRIVATE.equals(modifier) || PsiModifier.PROTECTED.equals(modifier) || PsiModifier.PACKAGE_LOCAL.equals(modifier)) {
                isAllowed = modifierOwnerParent instanceof PsiClass;
            }
        } else {
            if (PsiModifier.PUBLIC.equals(modifier)) {
                isAllowed = modifierOwnerParent instanceof PsiJavaFile || modifierOwnerParent instanceof PsiClass && (modifierOwnerParent instanceof PsiSyntheticClass || ((PsiClass) modifierOwnerParent).getQualifiedName() != null);
            } else if (PsiModifier.STATIC.equals(modifier) || PsiModifier.PRIVATE.equals(modifier) || PsiModifier.PROTECTED.equals(modifier) || PsiModifier.PACKAGE_LOCAL.equals(modifier)) {
                isAllowed = modifierOwnerParent instanceof PsiClass && ((PsiClass) modifierOwnerParent).getQualifiedName() != null || FileTypeUtils.isInServerPageFile(modifierOwnerParent);
            }
            if (aClass.isEnum()) {
                isAllowed &= !(PsiModifier.FINAL.equals(modifier) || PsiModifier.ABSTRACT.equals(modifier));
            }
            if (aClass.getContainingClass() instanceof PsiAnonymousClass) {
                isAllowed &= !(PsiModifier.PRIVATE.equals(modifier) || PsiModifier.PROTECTED.equals(modifier));
            }
        }
    } else if (modifierOwner instanceof PsiMethod) {
        PsiMethod method = (PsiMethod) modifierOwner;
        isAllowed = !(method.isConstructor() && ourConstructorNotAllowedModifiers.contains(modifier));
        PsiClass containingClass = method.getContainingClass();
        if ((method.hasModifierProperty(PsiModifier.PUBLIC) || method.hasModifierProperty(PsiModifier.PROTECTED)) && method.isConstructor() && containingClass != null && containingClass.isEnum()) {
            isAllowed = false;
        }
        if (PsiModifier.PRIVATE.equals(modifier)) {
            isAllowed &= modifierOwnerParent instanceof PsiClass && (!((PsiClass) modifierOwnerParent).isInterface() || PsiUtil.isLanguageLevel9OrHigher(modifierOwner));
        } else if (PsiModifier.STRICTFP.equals(modifier)) {
            isAllowed &= modifierOwnerParent instanceof PsiClass && (!((PsiClass) modifierOwnerParent).isInterface() || PsiUtil.isLanguageLevel8OrHigher(modifierOwner));
        } else if (PsiModifier.PROTECTED.equals(modifier) || PsiModifier.TRANSIENT.equals(modifier) || PsiModifier.SYNCHRONIZED.equals(modifier)) {
            isAllowed &= modifierOwnerParent instanceof PsiClass && !((PsiClass) modifierOwnerParent).isInterface();
        }
        if (containingClass != null && containingClass.isInterface()) {
            isAllowed &= !PsiModifier.NATIVE.equals(modifier);
        }
        if (containingClass != null && containingClass.isAnnotationType()) {
            isAllowed &= !PsiModifier.STATIC.equals(modifier);
            isAllowed &= !PsiModifier.DEFAULT.equals(modifier);
        }
    } else if (modifierOwner instanceof PsiField) {
        if (PsiModifier.PRIVATE.equals(modifier) || PsiModifier.PROTECTED.equals(modifier) || PsiModifier.TRANSIENT.equals(modifier) || PsiModifier.STRICTFP.equals(modifier) || PsiModifier.SYNCHRONIZED.equals(modifier)) {
            isAllowed = modifierOwnerParent instanceof PsiClass && !((PsiClass) modifierOwnerParent).isInterface();
        }
    } else if (modifierOwner instanceof PsiClassInitializer) {
        isAllowed = PsiModifier.STATIC.equals(modifier);
    } else if (modifierOwner instanceof PsiLocalVariable || modifierOwner instanceof PsiParameter) {
        isAllowed = PsiModifier.FINAL.equals(modifier);
    } else if (modifierOwner instanceof PsiReceiverParameter) {
        isAllowed = false;
    }
    isAllowed &= incompatibles != null;
    if (!isAllowed) {
        String message = JavaErrorMessages.message("modifier.not.allowed", modifier);
        HighlightInfo highlightInfo = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(keyword).descriptionAndTooltip(message).create();
        QuickFixAction.registerQuickFixAction(highlightInfo, QUICK_FIX_FACTORY.createModifierListFix(modifierList, modifier, false, false));
        return highlightInfo;
    }
    return null;
}
Also used : HashSet(com.intellij.util.containers.hash.HashSet) HighlightInfo(com.intellij.codeInsight.daemon.impl.HighlightInfo)

Example 82 with HighlightInfo

use of com.intellij.codeInsight.daemon.impl.HighlightInfo in project intellij-community by JetBrains.

the class HighlightUtil method checkAssignability.

@Nullable
private static HighlightInfo checkAssignability(@Nullable PsiType lType, @Nullable PsiType rType, @Nullable PsiExpression expression, @NotNull TextRange textRange, int navigationShift) {
    if (lType == rType)
        return null;
    if (expression == null) {
        if (rType == null || lType == null || TypeConversionUtil.isAssignable(lType, rType))
            return null;
    } else if (TypeConversionUtil.areTypesAssignmentCompatible(lType, expression)) {
        return null;
    }
    if (rType == null) {
        rType = expression.getType();
    }
    HighlightInfo highlightInfo = createIncompatibleTypeHighlightInfo(lType, rType, textRange, navigationShift);
    if (rType != null && expression != null && isCastIntentionApplicable(expression, lType)) {
        QuickFixAction.registerQuickFixAction(highlightInfo, QUICK_FIX_FACTORY.createAddTypeCastFix(lType, expression));
    }
    if (expression != null) {
        QuickFixAction.registerQuickFixAction(highlightInfo, QUICK_FIX_FACTORY.createWrapLongWithMathToIntExactFix(lType, expression));
        QuickFixAction.registerQuickFixAction(highlightInfo, QUICK_FIX_FACTORY.createWrapWithOptionalFix(lType, expression));
        QuickFixAction.registerQuickFixAction(highlightInfo, QUICK_FIX_FACTORY.createWrapExpressionFix(lType, expression));
        QuickFixAction.registerQuickFixAction(highlightInfo, QUICK_FIX_FACTORY.createWrapStringWithFileFix(lType, expression));
        AddTypeArgumentsConditionalFix.register(highlightInfo, expression, lType);
        registerCollectionToArrayFixAction(highlightInfo, rType, lType, expression);
    }
    ChangeNewOperatorTypeFix.register(highlightInfo, expression, lType);
    return highlightInfo;
}
Also used : HighlightInfo(com.intellij.codeInsight.daemon.impl.HighlightInfo)

Example 83 with HighlightInfo

use of com.intellij.codeInsight.daemon.impl.HighlightInfo in project intellij-community by JetBrains.

the class HighlightUtil method checkExceptionThrownInTry.

@Nullable
static List<HighlightInfo> checkExceptionThrownInTry(@NotNull final PsiParameter parameter, @NotNull final Set<PsiClassType> thrownTypes) {
    final PsiElement declarationScope = parameter.getDeclarationScope();
    if (!(declarationScope instanceof PsiCatchSection))
        return null;
    final PsiType caughtType = parameter.getType();
    if (caughtType instanceof PsiClassType) {
        HighlightInfo info = checkSimpleCatchParameter(parameter, thrownTypes, (PsiClassType) caughtType);
        return info == null ? null : Collections.singletonList(info);
    }
    if (caughtType instanceof PsiDisjunctionType) {
        return checkMultiCatchParameter(parameter, thrownTypes);
    }
    return null;
}
Also used : HighlightInfo(com.intellij.codeInsight.daemon.impl.HighlightInfo)

Example 84 with HighlightInfo

use of com.intellij.codeInsight.daemon.impl.HighlightInfo in project intellij-community by JetBrains.

the class HighlightUtil method checkIllegalModifierCombination.

@Nullable
static HighlightInfo checkIllegalModifierCombination(@NotNull PsiKeyword keyword, @NotNull PsiModifierList modifierList) {
    @PsiModifier.ModifierConstant String modifier = keyword.getText();
    String incompatible = getIncompatibleModifier(modifier, modifierList);
    if (incompatible != null) {
        String message = JavaErrorMessages.message("incompatible.modifiers", modifier, incompatible);
        HighlightInfo highlightInfo = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(keyword).descriptionAndTooltip(message).create();
        QuickFixAction.registerQuickFixAction(highlightInfo, QUICK_FIX_FACTORY.createModifierListFix(modifierList, modifier, false, false));
        return highlightInfo;
    }
    return null;
}
Also used : HighlightInfo(com.intellij.codeInsight.daemon.impl.HighlightInfo)

Example 85 with HighlightInfo

use of com.intellij.codeInsight.daemon.impl.HighlightInfo in project intellij-community by JetBrains.

the class ModuleHighlightUtil method checkPackageAccessTargets.

@NotNull
static List<HighlightInfo> checkPackageAccessTargets(@NotNull PsiPackageAccessibilityStatement statement) {
    List<HighlightInfo> results = ContainerUtil.newSmartList();
    Set<String> targets = ContainerUtil.newTroveSet();
    for (PsiJavaModuleReferenceElement refElement : statement.getModuleReferences()) {
        String refText = refElement.getReferenceText();
        PsiPolyVariantReference ref = refElement.getReference();
        assert ref != null : statement;
        if (!targets.add(refText)) {
            boolean exports = statement.getRole() == Role.EXPORTS;
            String message = JavaErrorMessages.message(exports ? "module.duplicate.exports.target" : "module.duplicate.opens.target", refText);
            HighlightInfo info = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(refElement).descriptionAndTooltip(message).create();
            QuickFixAction.registerQuickFixAction(info, factory().createDeleteFix(refElement, QuickFixBundle.message("delete.reference.fix.text")));
            results.add(info);
        } else if (ref.multiResolve(true).length == 0) {
            String message = JavaErrorMessages.message("module.not.found", refElement.getReferenceText());
            results.add(HighlightInfo.newHighlightInfo(HighlightInfoType.WARNING).range(refElement).descriptionAndTooltip(message).create());
        }
    }
    return results;
}
Also used : HighlightInfo(com.intellij.codeInsight.daemon.impl.HighlightInfo) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

HighlightInfo (com.intellij.codeInsight.daemon.impl.HighlightInfo)221 Nullable (org.jetbrains.annotations.Nullable)51 TextRange (com.intellij.openapi.util.TextRange)33 IntentionAction (com.intellij.codeInsight.intention.IntentionAction)30 VirtualFile (com.intellij.openapi.vfs.VirtualFile)28 NotNull (org.jetbrains.annotations.NotNull)17 HighlightInfoType (com.intellij.codeInsight.daemon.impl.HighlightInfoType)16 Document (com.intellij.openapi.editor.Document)12 ArrayList (java.util.ArrayList)11 PsiElement (com.intellij.psi.PsiElement)10 File (java.io.File)8 TextAttributes (com.intellij.openapi.editor.markup.TextAttributes)7 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)6 Pair (com.intellij.openapi.util.Pair)6 PsiFile (com.intellij.psi.PsiFile)6 Editor (com.intellij.openapi.editor.Editor)5 NonNls (org.jetbrains.annotations.NonNls)5 StringUtil (com.intellij.openapi.util.text.StringUtil)4 IElementType (com.intellij.psi.tree.IElementType)4 ContainerUtil (com.intellij.util.containers.ContainerUtil)4