Search in sources :

Example 31 with RefEntity

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

the class OfflineInspectionRVContentProvider method getQuickFixes.

@Override
@Nullable
public QuickFixAction[] getQuickFixes(@NotNull final InspectionToolWrapper toolWrapper, @NotNull final InspectionTree tree) {
    final TreePath[] treePaths = tree.getSelectionPaths();
    if (treePaths == null)
        return QuickFixAction.EMPTY;
    final List<RefEntity> selectedElements = new ArrayList<>();
    final Map<RefEntity, CommonProblemDescriptor[]> actions = new HashMap<>();
    for (TreePath selectionPath : treePaths) {
        TreeUtil.traverseDepth((TreeNode) selectionPath.getLastPathComponent(), node -> {
            if (!((InspectionTreeNode) node).isValid())
                return true;
            if (node instanceof OfflineProblemDescriptorNode) {
                if (((OfflineProblemDescriptorNode) node).isQuickFixAppliedFromView())
                    return true;
                final OfflineProblemDescriptorNode descriptorNode = (OfflineProblemDescriptorNode) node;
                final RefEntity element = descriptorNode.getElement();
                selectedElements.add(element);
                CommonProblemDescriptor[] descriptors = actions.get(element);
                final CommonProblemDescriptor descriptor = descriptorNode.getDescriptor();
                final CommonProblemDescriptor[] descriptorAsArray = descriptor == null ? CommonProblemDescriptor.EMPTY_ARRAY : new CommonProblemDescriptor[] { descriptor };
                actions.put(element, descriptors == null ? descriptorAsArray : DefaultInspectionToolPresentation.mergeDescriptors(descriptors, descriptorAsArray));
            } else if (node instanceof RefElementNode) {
                selectedElements.add(((RefElementNode) node).getElement());
            }
            return true;
        });
    }
    if (selectedElements.isEmpty())
        return null;
    final RefEntity[] selectedRefElements = selectedElements.toArray(new RefEntity[selectedElements.size()]);
    GlobalInspectionContextImpl context = tree.getContext();
    InspectionToolPresentation presentation = context.getPresentation(toolWrapper);
    return presentation.extractActiveFixes(selectedRefElements, actions, tree.getSelectedDescriptors());
}
Also used : THashMap(gnu.trove.THashMap) TreePath(javax.swing.tree.TreePath) CommonProblemDescriptor(com.intellij.codeInspection.CommonProblemDescriptor) RefEntity(com.intellij.codeInspection.reference.RefEntity) Nullable(org.jetbrains.annotations.Nullable)

Example 32 with RefEntity

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

the class InspectionTree method addElementsInNode.

private static void addElementsInNode(InspectionTreeNode node, Set<RefEntity> out) {
    if (!node.isValid())
        return;
    if (node instanceof RefElementNode) {
        final RefEntity element = ((RefElementNode) node).getElement();
        out.add(element);
    }
    if (node instanceof ProblemDescriptionNode) {
        final RefEntity element = ((ProblemDescriptionNode) node).getElement();
        out.add(element);
    }
    final Enumeration children = node.children();
    while (children.hasMoreElements()) {
        InspectionTreeNode child = (InspectionTreeNode) children.nextElement();
        addElementsInNode(child, out);
    }
}
Also used : RefEntity(com.intellij.codeInspection.reference.RefEntity)

Example 33 with RefEntity

use of com.intellij.codeInspection.reference.RefEntity 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 34 with RefEntity

use of com.intellij.codeInspection.reference.RefEntity 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 35 with RefEntity

use of com.intellij.codeInspection.reference.RefEntity 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

RefEntity (com.intellij.codeInspection.reference.RefEntity)38 RefElement (com.intellij.codeInspection.reference.RefElement)17 Nullable (org.jetbrains.annotations.Nullable)15 CommonProblemDescriptor (com.intellij.codeInspection.CommonProblemDescriptor)12 RefClass (com.intellij.codeInspection.reference.RefClass)11 NotNull (org.jetbrains.annotations.NotNull)9 PsiElement (com.intellij.psi.PsiElement)8 RefPackage (com.intellij.codeInspection.reference.RefPackage)7 HashSet (com.intellij.util.containers.HashSet)5 RefModule (com.intellij.codeInspection.reference.RefModule)4 Project (com.intellij.openapi.project.Project)4 DefaultInspectionToolPresentation (com.intellij.codeInspection.ui.DefaultInspectionToolPresentation)3 InspectionToolPresentation (com.intellij.codeInspection.ui.InspectionToolPresentation)3 VirtualFile (com.intellij.openapi.vfs.VirtualFile)3 THashSet (gnu.trove.THashSet)3 AnalysisScope (com.intellij.analysis.AnalysisScope)2 RefJavaUtil (com.intellij.codeInspection.reference.RefJavaUtil)2 RefManagerImpl (com.intellij.codeInspection.reference.RefManagerImpl)2 RefVisitor (com.intellij.codeInspection.reference.RefVisitor)2 ProblemGroup (com.intellij.lang.annotation.ProblemGroup)2