Search in sources :

Example 1 with LocalQuickFixOnPsiElementAsIntentionAdapter

use of com.intellij.codeInspection.LocalQuickFixOnPsiElementAsIntentionAdapter in project intellij-community by JetBrains.

the class HighlightMethodUtil method checkConstructorHandleSuperClassExceptions.

static HighlightInfo checkConstructorHandleSuperClassExceptions(PsiMethod method) {
    if (!method.isConstructor()) {
        return null;
    }
    PsiCodeBlock body = method.getBody();
    PsiStatement[] statements = body == null ? null : body.getStatements();
    if (statements == null)
        return null;
    // if we have unhandled exception inside method body, we could not have been called here,
    // so the only problem it can catch here is with super ctr only
    Collection<PsiClassType> unhandled = ExceptionUtil.collectUnhandledExceptions(method, method.getContainingClass());
    if (unhandled.isEmpty())
        return null;
    String description = HighlightUtil.getUnhandledExceptionsDescriptor(unhandled);
    TextRange textRange = HighlightNamesUtil.getMethodDeclarationTextRange(method);
    HighlightInfo highlightInfo = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(textRange).descriptionAndTooltip(description).create();
    for (PsiClassType exception : unhandled) {
        QuickFixAction.registerQuickFixAction(highlightInfo, new LocalQuickFixOnPsiElementAsIntentionAdapter(QUICK_FIX_FACTORY.createMethodThrowsFix(method, exception, true, false)));
    }
    return highlightInfo;
}
Also used : LocalQuickFixOnPsiElementAsIntentionAdapter(com.intellij.codeInspection.LocalQuickFixOnPsiElementAsIntentionAdapter) HighlightInfo(com.intellij.codeInsight.daemon.impl.HighlightInfo) TextRange(com.intellij.openapi.util.TextRange)

Example 2 with LocalQuickFixOnPsiElementAsIntentionAdapter

use of com.intellij.codeInspection.LocalQuickFixOnPsiElementAsIntentionAdapter in project intellij-community by JetBrains.

the class HighlightMethodUtil method checkMethodIncompatibleThrows.

static HighlightInfo checkMethodIncompatibleThrows(MethodSignatureBackedByPsiMethod methodSignature, List<HierarchicalMethodSignature> superMethodSignatures, boolean includeRealPositionInfo, PsiClass analyzedClass) {
    PsiMethod method = methodSignature.getMethod();
    PsiClass aClass = method.getContainingClass();
    if (aClass == null)
        return null;
    PsiSubstitutor superSubstitutor = TypeConversionUtil.getSuperClassSubstitutor(aClass, analyzedClass, PsiSubstitutor.EMPTY);
    PsiClassType[] exceptions = method.getThrowsList().getReferencedTypes();
    PsiJavaCodeReferenceElement[] referenceElements;
    List<PsiElement> exceptionContexts;
    if (includeRealPositionInfo) {
        exceptionContexts = new ArrayList<>();
        referenceElements = method.getThrowsList().getReferenceElements();
    } else {
        exceptionContexts = null;
        referenceElements = null;
    }
    List<PsiClassType> checkedExceptions = new ArrayList<>();
    for (int i = 0; i < exceptions.length; i++) {
        PsiClassType exception = exceptions[i];
        if (exception == null) {
            LOG.error("throws: " + method.getThrowsList().getText() + "; method: " + method);
        }
        if (!ExceptionUtil.isUncheckedException(exception)) {
            checkedExceptions.add(exception);
            if (includeRealPositionInfo && i < referenceElements.length) {
                PsiJavaCodeReferenceElement exceptionRef = referenceElements[i];
                exceptionContexts.add(exceptionRef);
            }
        }
    }
    for (MethodSignatureBackedByPsiMethod superMethodSignature : superMethodSignatures) {
        PsiMethod superMethod = superMethodSignature.getMethod();
        int index = getExtraExceptionNum(methodSignature, superMethodSignature, checkedExceptions, superSubstitutor);
        if (index != -1) {
            if (aClass.isInterface()) {
                final PsiClass superContainingClass = superMethod.getContainingClass();
                if (superContainingClass != null && !superContainingClass.isInterface())
                    continue;
                if (superContainingClass != null && !aClass.isInheritor(superContainingClass, true))
                    continue;
            }
            PsiClassType exception = checkedExceptions.get(index);
            String description = JavaErrorMessages.message("overridden.method.does.not.throw", createClashMethodMessage(method, superMethod, true), JavaHighlightUtil.formatType(exception));
            TextRange textRange;
            if (includeRealPositionInfo) {
                PsiElement exceptionContext = exceptionContexts.get(index);
                textRange = exceptionContext.getTextRange();
            } else {
                textRange = TextRange.EMPTY_RANGE;
            }
            HighlightInfo errorResult = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(textRange).descriptionAndTooltip(description).create();
            QuickFixAction.registerQuickFixAction(errorResult, new LocalQuickFixOnPsiElementAsIntentionAdapter(QUICK_FIX_FACTORY.createMethodThrowsFix(method, exception, false, false)));
            QuickFixAction.registerQuickFixAction(errorResult, new LocalQuickFixOnPsiElementAsIntentionAdapter(QUICK_FIX_FACTORY.createMethodThrowsFix(superMethod, exception, true, true)));
            return errorResult;
        }
    }
    return null;
}
Also used : LocalQuickFixOnPsiElementAsIntentionAdapter(com.intellij.codeInspection.LocalQuickFixOnPsiElementAsIntentionAdapter) HighlightInfo(com.intellij.codeInsight.daemon.impl.HighlightInfo) TextRange(com.intellij.openapi.util.TextRange)

Example 3 with LocalQuickFixOnPsiElementAsIntentionAdapter

use of com.intellij.codeInspection.LocalQuickFixOnPsiElementAsIntentionAdapter in project intellij-community by JetBrains.

the class HighlightUtil method checkArrayInitializer.

@Nullable
static Collection<HighlightInfo> checkArrayInitializer(final PsiExpression initializer, PsiType type) {
    if (!(initializer instanceof PsiArrayInitializerExpression))
        return null;
    if (!(type instanceof PsiArrayType))
        return null;
    final PsiType componentType = ((PsiArrayType) type).getComponentType();
    final PsiArrayInitializerExpression arrayInitializer = (PsiArrayInitializerExpression) initializer;
    boolean arrayTypeFixChecked = false;
    VariableArrayTypeFix fix = null;
    final Collection<HighlightInfo> result = ContainerUtil.newArrayList();
    final PsiExpression[] initializers = arrayInitializer.getInitializers();
    for (PsiExpression expression : initializers) {
        final HighlightInfo info = checkArrayInitializerCompatibleTypes(expression, componentType);
        if (info != null) {
            result.add(info);
            if (!arrayTypeFixChecked) {
                final PsiType checkResult = JavaHighlightUtil.sameType(initializers);
                fix = checkResult != null ? VariableArrayTypeFix.createFix(arrayInitializer, checkResult) : null;
                arrayTypeFixChecked = true;
            }
            if (fix != null) {
                QuickFixAction.registerQuickFixAction(info, new LocalQuickFixOnPsiElementAsIntentionAdapter(fix));
            }
        }
    }
    return result;
}
Also used : LocalQuickFixOnPsiElementAsIntentionAdapter(com.intellij.codeInspection.LocalQuickFixOnPsiElementAsIntentionAdapter) HighlightInfo(com.intellij.codeInsight.daemon.impl.HighlightInfo)

Aggregations

HighlightInfo (com.intellij.codeInsight.daemon.impl.HighlightInfo)3 LocalQuickFixOnPsiElementAsIntentionAdapter (com.intellij.codeInspection.LocalQuickFixOnPsiElementAsIntentionAdapter)3 TextRange (com.intellij.openapi.util.TextRange)2