Search in sources :

Example 21 with CommonProblemDescriptor

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

the class InspectionTree method getSelectedDescriptors.

@NotNull
public CommonProblemDescriptor[] getSelectedDescriptors(boolean sortedByPosition, @Nullable Set<VirtualFile> readOnlyFilesSink, boolean allowResolved, boolean allowSuppressed) {
    final TreePath[] paths = getSelectionPaths();
    if (paths == null)
        return CommonProblemDescriptor.EMPTY_ARRAY;
    final TreePath[] selectionPaths = TreeUtil.selectMaximals(paths);
    final List<CommonProblemDescriptor> descriptors = new ArrayList<>();
    MultiMap<Object, ProblemDescriptionNode> parentToChildNode = new MultiMap<>();
    final List<InspectionTreeNode> nonDescriptorNodes = new SmartList<>();
    for (TreePath path : selectionPaths) {
        final Object[] pathAsArray = path.getPath();
        final int length = pathAsArray.length;
        final Object node = pathAsArray[length - 1];
        if (node instanceof ProblemDescriptionNode) {
            if (isNodeValidAndIncluded((ProblemDescriptionNode) node, allowResolved, allowSuppressed)) {
                if (length >= 2) {
                    parentToChildNode.putValue(pathAsArray[length - 2], (ProblemDescriptionNode) node);
                } else {
                    parentToChildNode.putValue(node, (ProblemDescriptionNode) node);
                }
            }
        } else {
            nonDescriptorNodes.add((InspectionTreeNode) node);
        }
    }
    for (InspectionTreeNode node : nonDescriptorNodes) {
        processChildDescriptorsDeep(node, descriptors, sortedByPosition, allowResolved, allowSuppressed, readOnlyFilesSink);
    }
    for (Map.Entry<Object, Collection<ProblemDescriptionNode>> entry : parentToChildNode.entrySet()) {
        final Collection<ProblemDescriptionNode> siblings = entry.getValue();
        if (siblings.size() == 1) {
            final ProblemDescriptionNode descriptorNode = ContainerUtil.getFirstItem(siblings);
            LOG.assertTrue(descriptorNode != null);
            CommonProblemDescriptor descriptor = descriptorNode.getDescriptor();
            if (descriptor != null) {
                descriptors.add(descriptor);
                if (readOnlyFilesSink != null) {
                    collectReadOnlyFiles(descriptor, readOnlyFilesSink);
                }
            }
        } else {
            List<CommonProblemDescriptor> currentDescriptors = new ArrayList<>();
            for (ProblemDescriptionNode sibling : siblings) {
                final CommonProblemDescriptor descriptor = sibling.getDescriptor();
                if (descriptor != null) {
                    if (readOnlyFilesSink != null) {
                        collectReadOnlyFiles(descriptor, readOnlyFilesSink);
                    }
                    currentDescriptors.add(descriptor);
                }
            }
            if (sortedByPosition) {
                Collections.sort(currentDescriptors, DESCRIPTOR_COMPARATOR);
            }
            descriptors.addAll(currentDescriptors);
        }
    }
    return descriptors.toArray(new CommonProblemDescriptor[descriptors.size()]);
}
Also used : MultiMap(com.intellij.util.containers.MultiMap) TreePath(javax.swing.tree.TreePath) CommonProblemDescriptor(com.intellij.codeInspection.CommonProblemDescriptor) SmartList(com.intellij.util.SmartList) MultiMap(com.intellij.util.containers.MultiMap) NotNull(org.jetbrains.annotations.NotNull)

Example 22 with CommonProblemDescriptor

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

the class ClassOnlyUsedInOnePackageInspection method checkElement.

@Nullable
@Override
public CommonProblemDescriptor[] checkElement(@NotNull RefEntity refEntity, @NotNull AnalysisScope scope, @NotNull InspectionManager manager, @NotNull GlobalInspectionContext globalContext) {
    if (!(refEntity instanceof RefClass)) {
        return null;
    }
    final RefClass refClass = (RefClass) refEntity;
    final RefEntity owner = refClass.getOwner();
    if (!(owner instanceof RefPackage)) {
        return null;
    }
    final Set<RefClass> dependencies = DependencyUtils.calculateDependenciesForClass(refClass);
    RefPackage otherPackage = null;
    for (RefClass dependency : dependencies) {
        final RefPackage refPackage = RefJavaUtil.getPackage(dependency);
        if (owner == refPackage) {
            return null;
        }
        if (otherPackage != refPackage) {
            if (otherPackage == null) {
                otherPackage = refPackage;
            } else {
                return null;
            }
        }
    }
    final Set<RefClass> dependents = DependencyUtils.calculateDependentsForClass(refClass);
    for (RefClass dependent : dependents) {
        final RefPackage refPackage = RefJavaUtil.getPackage(dependent);
        if (owner == refPackage) {
            return null;
        }
        if (otherPackage != refPackage) {
            if (otherPackage == null) {
                otherPackage = refPackage;
            } else {
                return null;
            }
        }
    }
    if (otherPackage == null) {
        return null;
    }
    final PsiClass aClass = refClass.getElement();
    final PsiIdentifier identifier = aClass.getNameIdentifier();
    if (identifier == null) {
        return null;
    }
    final String packageName = otherPackage.getName();
    return new CommonProblemDescriptor[] { manager.createProblemDescriptor(identifier, InspectionGadgetsBundle.message("class.only.used.in.one.package.problem.descriptor", packageName), true, ProblemHighlightType.GENERIC_ERROR_OR_WARNING, false) };
}
Also used : CommonProblemDescriptor(com.intellij.codeInspection.CommonProblemDescriptor) PsiClass(com.intellij.psi.PsiClass) PsiIdentifier(com.intellij.psi.PsiIdentifier) Nullable(org.jetbrains.annotations.Nullable)

Example 23 with CommonProblemDescriptor

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

the class DisjointPackageInspection method checkElement.

@Override
@Nullable
public CommonProblemDescriptor[] checkElement(@NotNull RefEntity refEntity, @NotNull AnalysisScope analysisScope, @NotNull InspectionManager inspectionManager, @NotNull GlobalInspectionContext globalInspectionContext) {
    if (!(refEntity instanceof RefPackage)) {
        return null;
    }
    final RefPackage refPackage = (RefPackage) refEntity;
    final List<RefEntity> children = refPackage.getChildren();
    final Set<RefClass> childClasses = new HashSet<>();
    for (RefEntity child : children) {
        if (!(child instanceof RefClass)) {
            continue;
        }
        final PsiClass psiClass = ((RefClass) child).getElement();
        if (ClassUtils.isInnerClass(psiClass)) {
            continue;
        }
        childClasses.add((RefClass) child);
    }
    if (childClasses.isEmpty()) {
        return null;
    }
    final Set<Set<RefClass>> components = createComponents(refPackage, childClasses);
    if (components.size() == 1) {
        return null;
    }
    final String errorString = InspectionGadgetsBundle.message("disjoint.package.problem.descriptor", refPackage.getQualifiedName(), Integer.valueOf(components.size()));
    return new CommonProblemDescriptor[] { inspectionManager.createProblemDescriptor(errorString) };
}
Also used : RefClass(com.intellij.codeInspection.reference.RefClass) Set(java.util.Set) HashSet(java.util.HashSet) CommonProblemDescriptor(com.intellij.codeInspection.CommonProblemDescriptor) RefEntity(com.intellij.codeInspection.reference.RefEntity) RefPackage(com.intellij.codeInspection.reference.RefPackage) PsiClass(com.intellij.psi.PsiClass) HashSet(java.util.HashSet) Nullable(org.jetbrains.annotations.Nullable)

Example 24 with CommonProblemDescriptor

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

the class ClassOnlyUsedInOneModuleInspection method checkElement.

@Nullable
@Override
public CommonProblemDescriptor[] checkElement(@NotNull RefEntity refEntity, @NotNull AnalysisScope scope, @NotNull InspectionManager manager, @NotNull GlobalInspectionContext globalContext) {
    if (!(refEntity instanceof RefClass)) {
        return null;
    }
    final RefClass refClass = (RefClass) refEntity;
    final RefEntity owner = refClass.getOwner();
    if (!(owner instanceof RefPackage)) {
        return null;
    }
    final Set<RefClass> dependencies = DependencyUtils.calculateDependenciesForClass(refClass);
    RefModule otherModule = null;
    for (RefClass dependency : dependencies) {
        final RefModule module = dependency.getModule();
        if (refClass.getModule() == module) {
            return null;
        }
        if (otherModule != module) {
            if (otherModule == null) {
                otherModule = module;
            } else {
                return null;
            }
        }
    }
    final Set<RefClass> dependents = DependencyUtils.calculateDependentsForClass(refClass);
    for (RefClass dependent : dependents) {
        final RefModule module = dependent.getModule();
        if (refClass.getModule() == module) {
            return null;
        }
        if (otherModule != module) {
            if (otherModule == null) {
                otherModule = module;
            } else {
                return null;
            }
        }
    }
    if (otherModule == null) {
        return null;
    }
    final PsiClass aClass = refClass.getElement();
    final PsiIdentifier identifier = aClass.getNameIdentifier();
    if (identifier == null) {
        return null;
    }
    final String moduleName = otherModule.getName();
    return new CommonProblemDescriptor[] { manager.createProblemDescriptor(identifier, InspectionGadgetsBundle.message("class.only.used.in.one.module.problem.descriptor", moduleName), true, ProblemHighlightType.GENERIC_ERROR_OR_WARNING, false) };
}
Also used : RefModule(com.intellij.codeInspection.reference.RefModule) RefClass(com.intellij.codeInspection.reference.RefClass) CommonProblemDescriptor(com.intellij.codeInspection.CommonProblemDescriptor) RefEntity(com.intellij.codeInspection.reference.RefEntity) RefPackage(com.intellij.codeInspection.reference.RefPackage) PsiClass(com.intellij.psi.PsiClass) PsiIdentifier(com.intellij.psi.PsiIdentifier) Nullable(org.jetbrains.annotations.Nullable)

Example 25 with CommonProblemDescriptor

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

the class ModuleWithTooFewClassesInspection method checkElement.

@Override
@Nullable
public CommonProblemDescriptor[] checkElement(@NotNull RefEntity refEntity, @NotNull AnalysisScope analysisScope, @NotNull InspectionManager inspectionManager, @NotNull GlobalInspectionContext globalInspectionContext) {
    if (!(refEntity instanceof RefModule)) {
        return null;
    }
    final RefModule refModule = (RefModule) refEntity;
    final List<RefEntity> children = refModule.getChildren();
    int numClasses = 0;
    for (RefEntity child : children) {
        if (child instanceof RefClass) {
            numClasses++;
        }
    }
    if (numClasses >= limit || numClasses == 0) {
        return null;
    }
    final Project project = globalInspectionContext.getProject();
    final Module[] modules = ModuleManager.getInstance(project).getModules();
    if (modules.length == 1) {
        return null;
    }
    final String errorString = InspectionGadgetsBundle.message("module.with.too.few.classes.problem.descriptor", refModule.getName(), Integer.valueOf(numClasses), Integer.valueOf(limit));
    return new CommonProblemDescriptor[] { inspectionManager.createProblemDescriptor(errorString) };
}
Also used : RefModule(com.intellij.codeInspection.reference.RefModule) Project(com.intellij.openapi.project.Project) RefClass(com.intellij.codeInspection.reference.RefClass) CommonProblemDescriptor(com.intellij.codeInspection.CommonProblemDescriptor) RefEntity(com.intellij.codeInspection.reference.RefEntity) RefModule(com.intellij.codeInspection.reference.RefModule) Module(com.intellij.openapi.module.Module) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

CommonProblemDescriptor (com.intellij.codeInspection.CommonProblemDescriptor)30 Nullable (org.jetbrains.annotations.Nullable)20 RefClass (com.intellij.codeInspection.reference.RefClass)13 RefEntity (com.intellij.codeInspection.reference.RefEntity)12 RefPackage (com.intellij.codeInspection.reference.RefPackage)9 PsiClass (com.intellij.psi.PsiClass)9 PsiElement (com.intellij.psi.PsiElement)7 HashSet (java.util.HashSet)6 ProblemDescriptor (com.intellij.codeInspection.ProblemDescriptor)5 RefModule (com.intellij.codeInspection.reference.RefModule)4 PsiIdentifier (com.intellij.psi.PsiIdentifier)4 NotNull (org.jetbrains.annotations.NotNull)4 QuickFix (com.intellij.codeInspection.QuickFix)2 Project (com.intellij.openapi.project.Project)2 TextRange (com.intellij.openapi.util.TextRange)2 PsiField (com.intellij.psi.PsiField)2 PsiType (com.intellij.psi.PsiType)2 TreePath (javax.swing.tree.TreePath)2 BatchQuickFix (com.intellij.codeInspection.BatchQuickFix)1 ProblemDescriptorBase (com.intellij.codeInspection.ProblemDescriptorBase)1