Search in sources :

Example 21 with ProblemDescriptor

use of com.intellij.codeInspection.ProblemDescriptor in project android by JetBrains.

the class ExternalAnnotationsSupport method checkAnnotationsJarAttached.

// Based on similar code in MagicConstantInspection
@SuppressWarnings("ALL")
private static void checkAnnotationsJarAttached(@NotNull PsiFile file, @NotNull ProblemsHolder holder) {
    // Not yet used
    if (false) {
        final Project project = file.getProject();
        PsiClass actionBar = JavaPsiFacade.getInstance(project).findClass("android.app.ActionBar", GlobalSearchScope.allScope(project));
        if (actionBar == null) {
            // no sdk to attach
            return;
        }
        PsiMethod[] methods = actionBar.findMethodsByName("getNavigationMode", false);
        if (methods.length != 1) {
            // no sdk to attach
            return;
        }
        PsiMethod getModifiers = methods[0];
        ExternalAnnotationsManager annotationsManager = ExternalAnnotationsManager.getInstance(project);
        PsiAnnotation annotation = annotationsManager.findExternalAnnotation(getModifiers, MagicConstant.class.getName());
        if (annotation != null) {
            return;
        }
        final VirtualFile virtualFile = PsiUtilCore.getVirtualFile(getModifiers);
        if (virtualFile == null) {
            // no sdk to attach
            return;
        }
        final List<OrderEntry> entries = ProjectRootManager.getInstance(project).getFileIndex().getOrderEntriesForFile(virtualFile);
        Sdk sdk = null;
        for (OrderEntry orderEntry : entries) {
            if (orderEntry instanceof JdkOrderEntry) {
                sdk = ((JdkOrderEntry) orderEntry).getJdk();
                if (sdk != null) {
                    break;
                }
            }
        }
        if (sdk == null) {
            // no sdk to attach
            return;
        }
        final Sdk finalSdk = sdk;
        String path = finalSdk.getHomePath();
        String text = "No IDEA annotations attached to the Android SDK " + finalSdk.getName() + (path == null ? "" : " (" + FileUtil.toSystemDependentName(path) + ")") + ", some issues will not be found";
        holder.registerProblem(file, text, ProblemHighlightType.GENERIC_ERROR_OR_WARNING, new LocalQuickFix() {

            @NotNull
            @Override
            public String getName() {
                return "Attach annotations";
            }

            @NotNull
            @Override
            public String getFamilyName() {
                return getName();
            }

            @Override
            public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
                ApplicationManager.getApplication().runWriteAction(new Runnable() {

                    @Override
                    public void run() {
                        SdkModificator modifier = finalSdk.getSdkModificator();
                        attachJdkAnnotations(modifier);
                        modifier.commitChanges();
                    }
                });
            }
        });
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) MagicConstant(org.intellij.lang.annotations.MagicConstant) ProblemDescriptor(com.intellij.codeInspection.ProblemDescriptor) LocalQuickFix(com.intellij.codeInspection.LocalQuickFix) SdkModificator(com.intellij.openapi.projectRoots.SdkModificator) NotNull(org.jetbrains.annotations.NotNull) Project(com.intellij.openapi.project.Project) Sdk(com.intellij.openapi.projectRoots.Sdk) ExternalAnnotationsManager(com.intellij.codeInsight.ExternalAnnotationsManager)

Example 22 with ProblemDescriptor

use of com.intellij.codeInspection.ProblemDescriptor in project intellij-plugins by JetBrains.

the class JSImplicitlyInternalDeclarationInspection method process.

private static void process(final JSNamedElement node, final ProblemsHolder holder) {
    if (!DialectDetector.isActionScript(node))
        return;
    JSFunction fun = PsiTreeUtil.getParentOfType(node, JSFunction.class);
    if (fun != null)
        return;
    ASTNode nameIdentifier = node.findNameIdentifier();
    if (nameIdentifier == null)
        return;
    JSClass clazz = JSResolveUtil.getClassOfContext(node);
    if (clazz == null) {
        PsiElement parent = JSResolveUtil.findParent(node);
        if (!(parent instanceof JSPackageStatement))
            return;
    }
    JSAttributeList attributeList = ((JSAttributeListOwner) node).getAttributeList();
    JSAttributeList.AccessType accessType = attributeList != null ? attributeList.getAccessType() : null;
    if (accessType == JSAttributeList.AccessType.PACKAGE_LOCAL && attributeList.findAccessTypeElement() == null && attributeList.getNamespaceElement() == null && !JSResolveUtil.isConstructorFunction(node)) {
        holder.registerProblem(nameIdentifier.getPsi(), FlexBundle.message("js.implicitly.internal.declaration.problem"), new LocalQuickFix() {

            @NotNull
            @Override
            public String getFamilyName() {
                return FlexBundle.message("js.implicitly.internal.declaration.problem.add.internal.fix");
            }

            @Override
            public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
                PsiElement anchor = descriptor.getPsiElement();
                JSChangeVisibilityUtil.setVisibility((JSAttributeListOwner) anchor.getParent(), JSAttributeList.AccessType.PACKAGE_LOCAL);
            }
        });
    }
}
Also used : JSAttributeList(com.intellij.lang.javascript.psi.ecmal4.JSAttributeList) JSPackageStatement(com.intellij.lang.javascript.psi.ecmal4.JSPackageStatement) ProblemDescriptor(com.intellij.codeInspection.ProblemDescriptor) LocalQuickFix(com.intellij.codeInspection.LocalQuickFix) NotNull(org.jetbrains.annotations.NotNull) Project(com.intellij.openapi.project.Project) ASTNode(com.intellij.lang.ASTNode) JSAttributeListOwner(com.intellij.lang.javascript.psi.ecmal4.JSAttributeListOwner) JSClass(com.intellij.lang.javascript.psi.ecmal4.JSClass) PsiElement(com.intellij.psi.PsiElement)

Example 23 with ProblemDescriptor

use of com.intellij.codeInspection.ProblemDescriptor in project qi4j-sdk by Qi4j.

the class MixinImplementsMixinType method checkClass.

@Override
public final ProblemDescriptor[] checkClass(@NotNull PsiClass psiClass, @NotNull InspectionManager manager, boolean isOnTheFly) {
    // If psiClass is not an interface, ignore
    if (!psiClass.isInterface()) {
        return null;
    }
    // If @Mixins annotation is empty, ignore
    List<PsiAnnotationMemberValue> mixinAnnotationValues = getMixinsAnnotationValue(psiClass);
    if (mixinAnnotationValues.isEmpty()) {
        return null;
    }
    // Get all valid mixin type
    Set<PsiClass> validMixinsType = getAllValidMixinTypes(psiClass);
    if (validMixinsType.isEmpty()) {
        return null;
    }
    // For each mixin
    List<ProblemDescriptor> problems = new LinkedList<ProblemDescriptor>();
    for (PsiAnnotationMemberValue mixinAnnotationValue : mixinAnnotationValues) {
        PsiJavaCodeReferenceElement mixinClassReference = getMixinClassReference(mixinAnnotationValue);
        // If it's not a class reference, ignore
        if (mixinClassReference == null) {
            continue;
        }
        // If class reference can't be resolved, ignore
        PsiClass mixinClass = (PsiClass) mixinClassReference.resolve();
        if (mixinClass == null) {
            continue;
        }
        String mixinQualifiedName = mixinClass.getQualifiedName();
        boolean isMixinsDeclarationValid = false;
        String message = "";
        if (mixinClass.isInterface()) {
            // Mixin can't be an interface
            message = message("mixin.implements.mixin.type.error.mixin.is.an.interface", mixinQualifiedName);
        } else if (isAConcern(mixinClass)) {
            // Mixin can't be a concern
            message = message("mixin.implements.mixin.type.error.mixin.is.a.concern", mixinQualifiedName);
        } else if (isASideEffect(mixinClass)) {
            // Mixin can't be a side effect
            message = message("mixin.implements.mixin.type.error.mixin.is.a.side.effect", mixinQualifiedName);
        } else {
            // If doesn't implement any mixin type, it's a problem
            if (!isImplementValidMixinType(mixinClass, validMixinsType)) {
                message = message("mixin.implements.mixin.type.error.does.not.implement.any.mixin.type", mixinQualifiedName, psiClass.getQualifiedName());
            } else {
                isMixinsDeclarationValid = true;
            }
        }
        if (!isMixinsDeclarationValid) {
            ProblemDescriptor problemDescriptor = createProblemDescriptor(manager, mixinAnnotationValue, mixinClassReference, message);
            problems.add(problemDescriptor);
        }
    }
    return problems.toArray(new ProblemDescriptor[problems.size()]);
}
Also used : ProblemDescriptor(com.intellij.codeInspection.ProblemDescriptor) PsiJavaCodeReferenceElement(com.intellij.psi.PsiJavaCodeReferenceElement) PsiClass(com.intellij.psi.PsiClass) LinkedList(java.util.LinkedList) PsiAnnotationMemberValue(com.intellij.psi.PsiAnnotationMemberValue)

Example 24 with ProblemDescriptor

use of com.intellij.codeInspection.ProblemDescriptor in project qi4j-sdk by Qi4j.

the class AbstractInjectionAnnotationDeclarationOnFieldAndConstructorInspection method checkMethod.

@Override
public final ProblemDescriptor[] checkMethod(@NotNull PsiMethod method, @NotNull InspectionManager manager, boolean isOnTheFly) {
    PsiParameterList parameterList = method.getParameterList();
    PsiParameter[] parameters = parameterList.getParameters();
    if (method.isConstructor()) {
        List<ProblemDescriptor> problems = new LinkedList<ProblemDescriptor>();
        for (PsiParameter parameter : parameters) {
            PsiAnnotation annotation = getAnnotationToCheck(parameter);
            if (annotation != null) {
                ProblemDescriptor[] descriptors = verifyAnnotationDeclaredCorrectly(parameter, annotation, manager);
                if (descriptors != null) {
                    problems.addAll(asList(descriptors));
                }
            }
        }
        return problems.toArray(new ProblemDescriptor[problems.size()]);
    } else {
        List<ProblemDescriptor> problems = new LinkedList<ProblemDescriptor>();
        for (PsiParameter parameter : parameters) {
            PsiAnnotation annotationToCheck = getAnnotationToCheck(parameter);
            if (annotationToCheck != null) {
                String message = getInjectionAnnotationValidDeclarationMessage();
                AbstractFix removeAnnotationFix = createRemoveAnnotationFix(annotationToCheck);
                ProblemDescriptor problemDescriptor = manager.createProblemDescriptor(annotationToCheck, message, removeAnnotationFix, GENERIC_ERROR_OR_WARNING);
                problems.add(problemDescriptor);
            }
        }
        return problems.toArray(new ProblemDescriptor[problems.size()]);
    }
}
Also used : PsiParameter(com.intellij.psi.PsiParameter) ProblemDescriptor(com.intellij.codeInspection.ProblemDescriptor) AbstractFix(org.qi4j.ide.plugin.idea.common.inspections.AbstractFix) PsiParameterList(com.intellij.psi.PsiParameterList) PsiAnnotation(com.intellij.psi.PsiAnnotation) LinkedList(java.util.LinkedList)

Example 25 with ProblemDescriptor

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

the class JavaDocCommentFixer method fixCommonProblems.

/**
   * This fixer is based on existing javadoc inspections - there are two of them. One detects invalid references (to nonexistent
   * method parameter or non-declared checked exception). Another one handles all other cases (parameter documentation is missing;
   * parameter doesn't have a description etc). This method handles result of the second exception
   * 
   * @param problems  detected problems
   * @param comment   target comment to fix
   * @param document  target document which contains text of the comment being fixed
   * @param project   current project
   */
@SuppressWarnings("unchecked")
private static void fixCommonProblems(@NotNull ProblemDescriptor[] problems, @NotNull PsiComment comment, @NotNull final Document document, @NotNull Project project) {
    List<PsiElement> toRemove = new ArrayList<>();
    for (ProblemDescriptor problem : problems) {
        PsiElement element = problem.getPsiElement();
        if (element == null) {
            continue;
        }
        if ((!(element instanceof PsiDocToken) || !JavaDocTokenType.DOC_COMMENT_START.equals(((PsiDocToken) element).getTokenType())) && comment.getTextRange().contains(element.getTextRange())) {
            // Unnecessary element like '@return' at the void method's javadoc.
            for (PsiElement e = element; e != null; e = e.getParent()) {
                if (e instanceof PsiDocTag) {
                    toRemove.add(e);
                    break;
                }
            }
        } else {
            // Problems like 'missing @param'.
            QuickFix[] fixes = problem.getFixes();
            if (fixes != null && fixes.length > 0) {
                fixes[0].applyFix(project, problem);
            }
        }
    }
    if (toRemove.isEmpty()) {
        return;
    }
    if (toRemove.size() > 1) {
        Collections.sort(toRemove, COMPARATOR);
    }
    PsiDocumentManager psiDocumentManager = PsiDocumentManager.getInstance(project);
    psiDocumentManager.doPostponedOperationsAndUnblockDocument(document);
    CharSequence text = document.getCharsSequence();
    for (PsiElement element : toRemove) {
        int startOffset = element.getTextRange().getStartOffset();
        int startLine = document.getLineNumber(startOffset);
        int i = CharArrayUtil.shiftBackward(text, startOffset - 1, " \t");
        if (i >= 0) {
            char c = text.charAt(i);
            if (c == '*') {
                i = CharArrayUtil.shiftBackward(text, i - 1, " \t");
            }
        }
        if (i >= 0 && text.charAt(i) == '\n') {
            startOffset = Math.max(i, document.getLineStartOffset(startLine) - 1);
        }
        int endOffset = element.getTextRange().getEndOffset();
        // Javadoc PSI is awkward, it includes next line text before the next tag. That's why we need to strip it.
        i = CharArrayUtil.shiftBackward(text, endOffset - 1, " \t*");
        if (i > 0 && text.charAt(i) == '\n') {
            endOffset = i;
        }
        document.deleteString(startOffset, endOffset);
    }
    psiDocumentManager.commitDocument(document);
}
Also used : QuickFix(com.intellij.codeInspection.QuickFix) PsiDocTag(com.intellij.psi.javadoc.PsiDocTag) ProblemDescriptor(com.intellij.codeInspection.ProblemDescriptor) PsiDocToken(com.intellij.psi.javadoc.PsiDocToken)

Aggregations

ProblemDescriptor (com.intellij.codeInspection.ProblemDescriptor)40 Project (com.intellij.openapi.project.Project)15 NotNull (org.jetbrains.annotations.NotNull)13 LocalQuickFix (com.intellij.codeInspection.LocalQuickFix)11 PsiElement (com.intellij.psi.PsiElement)10 Nullable (org.jetbrains.annotations.Nullable)8 CommonProblemDescriptor (com.intellij.codeInspection.CommonProblemDescriptor)5 PsiAnnotation (com.intellij.psi.PsiAnnotation)5 LinkedList (java.util.LinkedList)5 InspectionManager (com.intellij.codeInspection.InspectionManager)4 PsiFile (com.intellij.psi.PsiFile)4 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)4 QuickFix (com.intellij.codeInspection.QuickFix)3 PsiAnnotationMemberValue (com.intellij.psi.PsiAnnotationMemberValue)3 PsiClass (com.intellij.psi.PsiClass)3 PsiJavaCodeReferenceElement (com.intellij.psi.PsiJavaCodeReferenceElement)3 GroovyFix (org.jetbrains.plugins.groovy.codeInspection.GroovyFix)3 LocalInspectionToolWrapper (com.intellij.codeInspection.ex.LocalInspectionToolWrapper)2 ASTNode (com.intellij.lang.ASTNode)2 Editor (com.intellij.openapi.editor.Editor)2