Search in sources :

Example 51 with HighlightInfo

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

the class HighlightClassUtil method checkQualifiedNew.

@Nullable
static HighlightInfo checkQualifiedNew(PsiNewExpression expression, PsiType type, PsiClass aClass) {
    PsiExpression qualifier = expression.getQualifier();
    if (qualifier == null)
        return null;
    if (type instanceof PsiArrayType) {
        String description = JavaErrorMessages.message("invalid.qualified.new");
        HighlightInfo info = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(expression).descriptionAndTooltip(description).create();
        QuickFixAction.registerQuickFixAction(info, QUICK_FIX_FACTORY.createRemoveNewQualifierFix(expression, null));
        return info;
    }
    HighlightInfo info = null;
    if (aClass != null) {
        if (aClass.hasModifierProperty(PsiModifier.STATIC)) {
            String description = JavaErrorMessages.message("qualified.new.of.static.class");
            info = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(expression).descriptionAndTooltip(description).create();
            if (!aClass.isEnum()) {
                QuickFixAction.registerQuickFixAction(info, QUICK_FIX_FACTORY.createModifierListFix(aClass, PsiModifier.STATIC, false, false));
            }
        } else if (aClass instanceof PsiAnonymousClass) {
            final PsiClass baseClass = PsiUtil.resolveClassInType(((PsiAnonymousClass) aClass).getBaseClassType());
            if (baseClass != null && baseClass.isInterface()) {
                info = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(expression).descriptionAndTooltip("Anonymous class implements interface; cannot have qualifier for new").create();
            }
        }
        QuickFixAction.registerQuickFixAction(info, QUICK_FIX_FACTORY.createRemoveNewQualifierFix(expression, aClass));
    }
    return info;
}
Also used : HighlightInfo(com.intellij.codeInsight.daemon.impl.HighlightInfo) Nullable(org.jetbrains.annotations.Nullable)

Example 52 with HighlightInfo

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

the class HighlightClassUtil method checkCannotInheritFromFinal.

@Nullable
static HighlightInfo checkCannotInheritFromFinal(PsiClass superClass, PsiElement elementToHighlight) {
    HighlightInfo errorResult = null;
    if (superClass.hasModifierProperty(PsiModifier.FINAL) || superClass.isEnum()) {
        String message = JavaErrorMessages.message("inheritance.from.final.class", superClass.getQualifiedName());
        errorResult = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(elementToHighlight).descriptionAndTooltip(message).create();
        QuickFixAction.registerQuickFixAction(errorResult, QUICK_FIX_FACTORY.createModifierListFix(superClass, PsiModifier.FINAL, false, false));
    }
    return errorResult;
}
Also used : HighlightInfo(com.intellij.codeInsight.daemon.impl.HighlightInfo) Nullable(org.jetbrains.annotations.Nullable)

Example 53 with HighlightInfo

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

the class HighlightClassUtil method checkAbstractInstantiation.

/**
   * new ref(...) or new ref(..) { ... } where ref is abstract class
   */
@Nullable
static HighlightInfo checkAbstractInstantiation(@NotNull PsiJavaCodeReferenceElement ref) {
    PsiElement parent = ref.getParent();
    HighlightInfo highlightInfo = null;
    if (parent instanceof PsiAnonymousClass && parent.getParent() instanceof PsiNewExpression && !PsiUtilCore.hasErrorElementChild(parent.getParent())) {
        PsiAnonymousClass aClass = (PsiAnonymousClass) parent;
        highlightInfo = checkClassWithAbstractMethods(aClass, ref.getTextRange());
    }
    return highlightInfo;
}
Also used : HighlightInfo(com.intellij.codeInsight.daemon.impl.HighlightInfo) Nullable(org.jetbrains.annotations.Nullable)

Example 54 with HighlightInfo

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

the class VariableAccessFromInnerClassFix method canBeFinal.

private static boolean canBeFinal(@NotNull PsiVariable variable, @NotNull List<PsiReferenceExpression> references) {
    // if there is at least one assignment to this variable, it cannot be final
    Map<PsiElement, Collection<PsiReferenceExpression>> uninitializedVarProblems = new THashMap<>();
    Map<PsiElement, Collection<ControlFlowUtil.VariableInfo>> finalVarProblems = new THashMap<>();
    for (PsiReferenceExpression expression : references) {
        if (ControlFlowUtil.isVariableAssignedInLoop(expression, variable))
            return false;
        HighlightInfo highlightInfo = HighlightControlFlowUtil.checkVariableInitializedBeforeUsage(expression, variable, uninitializedVarProblems, variable.getContainingFile());
        if (highlightInfo != null)
            return false;
        highlightInfo = HighlightControlFlowUtil.checkFinalVariableMightAlreadyHaveBeenAssignedTo(variable, expression, finalVarProblems);
        if (highlightInfo != null)
            return false;
        if (variable instanceof PsiParameter && PsiUtil.isAccessedForWriting(expression))
            return false;
    }
    return true;
}
Also used : THashMap(gnu.trove.THashMap) HighlightInfo(com.intellij.codeInsight.daemon.impl.HighlightInfo) ControlFlowUtil(com.intellij.psi.controlFlow.ControlFlowUtil) HighlightControlFlowUtil(com.intellij.codeInsight.daemon.impl.analysis.HighlightControlFlowUtil)

Example 55 with HighlightInfo

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

the class LossyEncodingTest method testDetectWrongEncoding0.

public void testDetectWrongEncoding0() throws Exception {
    String threeNotoriousRussianLetters = "ЖОП";
    configureByText(FileTypes.PLAIN_TEXT, threeNotoriousRussianLetters);
    VirtualFile virtualFile = getFile().getVirtualFile();
    final Document document = FileDocumentManager.getInstance().getDocument(virtualFile);
    WriteCommandAction.runWriteCommandAction(getProject(), () -> {
        document.insertString(0, " ");
        document.deleteString(0, 1);
    });
    assertTrue(FileDocumentManager.getInstance().isDocumentUnsaved(document));
    assertEquals(CharsetToolkit.UTF8_CHARSET, virtualFile.getCharset());
    Charset WINDOWS_1251 = Charset.forName("windows-1251");
    virtualFile.setCharset(WINDOWS_1251);
    // save in wrong encoding
    FileDocumentManager.getInstance().saveAllDocuments();
    assertEquals(WINDOWS_1251, virtualFile.getCharset());
    assertEquals(threeNotoriousRussianLetters, new String(virtualFile.contentsToByteArray(), WINDOWS_1251));
    virtualFile.setCharset(CharsetToolkit.UTF8_CHARSET);
    doHighlighting();
    List<HighlightInfo> infos = DaemonCodeAnalyzerEx.getInstanceEx(getProject()).getFileLevelHighlights(getProject(), getFile());
    HighlightInfo info = assertOneElement(infos);
    assertEquals("File was loaded in the wrong encoding: 'UTF-8'", info.getDescription());
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) HighlightInfo(com.intellij.codeInsight.daemon.impl.HighlightInfo) Charset(java.nio.charset.Charset) Document(com.intellij.openapi.editor.Document)

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