Search in sources :

Example 36 with HighlightInfo

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

the class GenericsHighlightUtil method checkTypeParametersList.

@Nullable
static HighlightInfo checkTypeParametersList(PsiTypeParameterList list, PsiTypeParameter[] parameters, @NotNull LanguageLevel level) {
    final PsiElement parent = list.getParent();
    if (parent instanceof PsiClass && ((PsiClass) parent).isEnum()) {
        String description = JavaErrorMessages.message("generics.enum.may.not.have.type.parameters");
        return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(list).descriptionAndTooltip(description).create();
    }
    if (PsiUtil.isAnnotationMethod(parent)) {
        String description = JavaErrorMessages.message("generics.annotation.members.may.not.have.type.parameters");
        return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(list).descriptionAndTooltip(description).create();
    }
    if (parent instanceof PsiClass && ((PsiClass) parent).isAnnotationType()) {
        String description = JavaErrorMessages.message("annotation.may.not.have.type.parameters");
        return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(list).descriptionAndTooltip(description).create();
    }
    for (int i = 0; i < parameters.length; i++) {
        final PsiTypeParameter typeParameter1 = parameters[i];
        final HighlightInfo cyclicInheritance = HighlightClassUtil.checkCyclicInheritance(typeParameter1);
        if (cyclicInheritance != null)
            return cyclicInheritance;
        String name1 = typeParameter1.getName();
        for (int j = i + 1; j < parameters.length; j++) {
            final PsiTypeParameter typeParameter2 = parameters[j];
            String name2 = typeParameter2.getName();
            if (Comparing.strEqual(name1, name2)) {
                String message = JavaErrorMessages.message("generics.duplicate.type.parameter", name1);
                return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(typeParameter2).descriptionAndTooltip(message).create();
            }
        }
        if (!level.isAtLeast(LanguageLevel.JDK_1_7)) {
            for (PsiJavaCodeReferenceElement referenceElement : typeParameter1.getExtendsList().getReferenceElements()) {
                final PsiElement resolve = referenceElement.resolve();
                if (resolve instanceof PsiTypeParameter && ArrayUtilRt.find(parameters, resolve) > i) {
                    return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(referenceElement.getTextRange()).descriptionAndTooltip("Illegal forward reference").create();
                }
            }
        }
    }
    return null;
}
Also used : HighlightInfo(com.intellij.codeInsight.daemon.impl.HighlightInfo) Nullable(org.jetbrains.annotations.Nullable)

Example 37 with HighlightInfo

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

the class HighlightMethodUtil method checkConstructorName.

static HighlightInfo checkConstructorName(PsiMethod method) {
    String methodName = method.getName();
    PsiClass aClass = method.getContainingClass();
    HighlightInfo errorResult = null;
    if (aClass != null) {
        String className = aClass instanceof PsiAnonymousClass ? null : aClass.getName();
        if (className == null || !Comparing.strEqual(methodName, className)) {
            PsiElement element = method.getNameIdentifier();
            String description = JavaErrorMessages.message("missing.return.type");
            errorResult = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(element).descriptionAndTooltip(description).create();
            if (className != null) {
                QuickFixAction.registerQuickFixAction(errorResult, QUICK_FIX_FACTORY.createRenameElementFix(method, className));
            }
        }
    }
    return errorResult;
}
Also used : HighlightInfo(com.intellij.codeInsight.daemon.impl.HighlightInfo)

Example 38 with HighlightInfo

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

the class HighlightMethodUtil method checkOverrideEquivalentInheritedMethods.

static HighlightInfo checkOverrideEquivalentInheritedMethods(PsiClass aClass, PsiFile containingFile, @NotNull LanguageLevel languageLevel) {
    String description = null;
    boolean appendImplementMethodFix = true;
    final Collection<HierarchicalMethodSignature> visibleSignatures = aClass.getVisibleSignatures();
    PsiResolveHelper resolveHelper = JavaPsiFacade.getInstance(aClass.getProject()).getResolveHelper();
    Ultimate: for (HierarchicalMethodSignature signature : visibleSignatures) {
        PsiMethod method = signature.getMethod();
        if (!resolveHelper.isAccessible(method, aClass, null))
            continue;
        List<HierarchicalMethodSignature> superSignatures = signature.getSuperSignatures();
        boolean allAbstracts = method.hasModifierProperty(PsiModifier.ABSTRACT);
        final PsiClass containingClass = method.getContainingClass();
        //to be checked at method level
        if (aClass.equals(containingClass))
            continue;
        if (aClass.isInterface() && !containingClass.isInterface())
            continue;
        HighlightInfo highlightInfo;
        if (allAbstracts) {
            superSignatures = new ArrayList<>(superSignatures);
            superSignatures.add(0, signature);
            highlightInfo = checkInterfaceInheritedMethodsReturnTypes(superSignatures, languageLevel);
        } else {
            highlightInfo = checkMethodIncompatibleReturnType(signature, superSignatures, false);
        }
        if (highlightInfo != null)
            description = highlightInfo.getDescription();
        if (method.hasModifierProperty(PsiModifier.STATIC)) {
            for (HierarchicalMethodSignature superSignature : superSignatures) {
                PsiMethod superMethod = superSignature.getMethod();
                if (!superMethod.hasModifierProperty(PsiModifier.STATIC)) {
                    description = JavaErrorMessages.message("static.method.cannot.override.instance.method", JavaHighlightUtil.formatMethod(method), HighlightUtil.formatClass(containingClass), JavaHighlightUtil.formatMethod(superMethod), HighlightUtil.formatClass(superMethod.getContainingClass()));
                    appendImplementMethodFix = false;
                    break Ultimate;
                }
            }
            continue;
        }
        if (description == null) {
            highlightInfo = checkMethodIncompatibleThrows(signature, superSignatures, false, aClass);
            if (highlightInfo != null)
                description = highlightInfo.getDescription();
        }
        if (description == null) {
            highlightInfo = checkMethodWeakerPrivileges(signature, superSignatures, false, containingFile);
            if (highlightInfo != null)
                description = highlightInfo.getDescription();
        }
        if (description != null)
            break;
    }
    if (description != null) {
        // show error info at the class level
        TextRange textRange = HighlightNamesUtil.getClassDeclarationTextRange(aClass);
        final HighlightInfo highlightInfo = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(textRange).descriptionAndTooltip(description).create();
        if (appendImplementMethodFix) {
            QuickFixAction.registerQuickFixAction(highlightInfo, QUICK_FIX_FACTORY.createImplementMethodsFix(aClass));
        }
        return highlightInfo;
    }
    return null;
}
Also used : HighlightInfo(com.intellij.codeInsight.daemon.impl.HighlightInfo) TextRange(com.intellij.openapi.util.TextRange)

Example 39 with HighlightInfo

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

the class HighlightMethodUtil method checkMethodCanHaveBody.

@Nullable
static HighlightInfo checkMethodCanHaveBody(@NotNull PsiMethod method, @NotNull LanguageLevel languageLevel) {
    PsiClass aClass = method.getContainingClass();
    boolean hasNoBody = method.getBody() == null;
    boolean isInterface = aClass != null && aClass.isInterface();
    boolean isExtension = method.hasModifierProperty(PsiModifier.DEFAULT);
    boolean isStatic = method.hasModifierProperty(PsiModifier.STATIC);
    boolean isPrivate = method.hasModifierProperty(PsiModifier.PRIVATE);
    final List<IntentionAction> additionalFixes = new ArrayList<>();
    String description = null;
    if (hasNoBody) {
        if (isExtension) {
            description = JavaErrorMessages.message("extension.method.should.have.a.body");
            additionalFixes.add(QUICK_FIX_FACTORY.createAddMethodBodyFix(method));
        } else if (isInterface) {
            if (isStatic && languageLevel.isAtLeast(LanguageLevel.JDK_1_8)) {
                description = "Static methods in interfaces should have a body";
            } else if (isPrivate && languageLevel.isAtLeast(LanguageLevel.JDK_1_9)) {
                description = "Private methods in interfaces should have a body";
            }
        }
    } else if (isInterface) {
        if (!isExtension && !isStatic && !isPrivate) {
            description = JavaErrorMessages.message("interface.methods.cannot.have.body");
            if (languageLevel.isAtLeast(LanguageLevel.JDK_1_8)) {
                additionalFixes.add(QUICK_FIX_FACTORY.createModifierListFix(method, PsiModifier.DEFAULT, true, false));
                additionalFixes.add(QUICK_FIX_FACTORY.createModifierListFix(method, PsiModifier.STATIC, true, false));
            }
        }
    } else if (isExtension) {
        description = JavaErrorMessages.message("extension.method.in.class");
    } else if (method.hasModifierProperty(PsiModifier.ABSTRACT)) {
        description = JavaErrorMessages.message("abstract.methods.cannot.have.a.body");
    } else if (method.hasModifierProperty(PsiModifier.NATIVE)) {
        description = JavaErrorMessages.message("native.methods.cannot.have.a.body");
    }
    if (description == null)
        return null;
    TextRange textRange = HighlightNamesUtil.getMethodDeclarationTextRange(method);
    HighlightInfo info = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(textRange).descriptionAndTooltip(description).create();
    if (!hasNoBody) {
        QuickFixAction.registerQuickFixAction(info, QUICK_FIX_FACTORY.createDeleteMethodBodyFix(method));
    }
    if (method.hasModifierProperty(PsiModifier.ABSTRACT) && !isInterface) {
        QuickFixAction.registerQuickFixAction(info, QUICK_FIX_FACTORY.createModifierListFix(method, PsiModifier.ABSTRACT, false, false));
    }
    for (IntentionAction intentionAction : additionalFixes) {
        QuickFixAction.registerQuickFixAction(info, intentionAction);
    }
    return info;
}
Also used : IntentionAction(com.intellij.codeInsight.intention.IntentionAction) HighlightInfo(com.intellij.codeInsight.daemon.impl.HighlightInfo) TextRange(com.intellij.openapi.util.TextRange) Nullable(org.jetbrains.annotations.Nullable)

Example 40 with HighlightInfo

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

the class HighlightClassUtil method checkClassWithAbstractMethods.

@Nullable
static HighlightInfo checkClassWithAbstractMethods(PsiClass aClass, PsiElement implementsFixElement, TextRange range) {
    PsiMethod abstractMethod = ClassUtil.getAnyAbstractMethod(aClass);
    if (abstractMethod == null) {
        return null;
    }
    final PsiClass superClass = abstractMethod.getContainingClass();
    if (superClass == null) {
        return null;
    }
    String baseClassName = HighlightUtil.formatClass(aClass, false);
    String methodName = JavaHighlightUtil.formatMethod(abstractMethod);
    String message = JavaErrorMessages.message(aClass instanceof PsiEnumConstantInitializer || implementsFixElement instanceof PsiEnumConstant ? "enum.constant.should.implement.method" : "class.must.be.abstract", baseClassName, methodName, HighlightUtil.formatClass(superClass, false));
    HighlightInfo errorResult = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(range).descriptionAndTooltip(message).create();
    final PsiMethod anyMethodToImplement = ClassUtil.getAnyMethodToImplement(aClass);
    if (anyMethodToImplement != null) {
        if (!anyMethodToImplement.hasModifierProperty(PsiModifier.PACKAGE_LOCAL) || JavaPsiFacade.getInstance(aClass.getProject()).arePackagesTheSame(aClass, superClass)) {
            QuickFixAction.registerQuickFixAction(errorResult, QUICK_FIX_FACTORY.createImplementMethodsFix(implementsFixElement));
        } else {
            QuickFixAction.registerQuickFixAction(errorResult, QUICK_FIX_FACTORY.createModifierListFix(anyMethodToImplement, PsiModifier.PROTECTED, true, true));
            QuickFixAction.registerQuickFixAction(errorResult, QUICK_FIX_FACTORY.createModifierListFix(anyMethodToImplement, PsiModifier.PUBLIC, true, true));
        }
    }
    if (!(aClass instanceof PsiAnonymousClass) && HighlightUtil.getIncompatibleModifier(PsiModifier.ABSTRACT, aClass.getModifierList()) == null) {
        QuickFixAction.registerQuickFixAction(errorResult, QUICK_FIX_FACTORY.createModifierListFix(aClass, PsiModifier.ABSTRACT, true, false));
    }
    return errorResult;
}
Also used : HighlightInfo(com.intellij.codeInsight.daemon.impl.HighlightInfo) Nullable(org.jetbrains.annotations.Nullable)

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