Search in sources :

Example 1 with RefClass

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

the class ClassWithTooManyTransitiveDependenciesInspection method checkElement.

@Override
@Nullable
public CommonProblemDescriptor[] checkElement(@NotNull RefEntity refEntity, @NotNull AnalysisScope analysisScope, @NotNull InspectionManager inspectionManager, @NotNull GlobalInspectionContext globalInspectionContext) {
    if (!(refEntity instanceof RefClass)) {
        return null;
    }
    final RefClass refClass = (RefClass) refEntity;
    final PsiClass aClass = refClass.getElement();
    if (ClassUtils.isInnerClass(aClass)) {
        return null;
    }
    final Set<RefClass> dependencies = DependencyUtils.calculateTransitiveDependenciesForClass(refClass);
    final int numDependencies = dependencies.size();
    if (numDependencies <= limit) {
        return null;
    }
    final String errorString = InspectionGadgetsBundle.message("class.with.too.many.transitive.dependencies.problem.descriptor", refEntity.getName(), numDependencies, limit);
    return new CommonProblemDescriptor[] { inspectionManager.createProblemDescriptor(errorString) };
}
Also used : RefClass(com.intellij.codeInspection.reference.RefClass) CommonProblemDescriptor(com.intellij.codeInspection.CommonProblemDescriptor) PsiClass(com.intellij.psi.PsiClass) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with RefClass

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

the class ClassWithTooManyTransitiveDependentsInspection method checkElement.

@Override
@Nullable
public CommonProblemDescriptor[] checkElement(@NotNull RefEntity refEntity, @NotNull AnalysisScope analysisScope, @NotNull InspectionManager inspectionManager, @NotNull GlobalInspectionContext globalInspectionContext) {
    if (!(refEntity instanceof RefClass)) {
        return null;
    }
    final RefClass refClass = (RefClass) refEntity;
    final PsiClass aClass = refClass.getElement();
    if (ClassUtils.isInnerClass(aClass)) {
        return null;
    }
    final Set<RefClass> dependencies = DependencyUtils.calculateTransitiveDependentsForClass(refClass);
    final int numDependents = dependencies.size();
    if (numDependents <= limit) {
        return null;
    }
    final String errorString = InspectionGadgetsBundle.message("class.with.too.many.transitive.dependents.problem.descriptor", refEntity.getName(), numDependents, limit);
    return new CommonProblemDescriptor[] { inspectionManager.createProblemDescriptor(errorString) };
}
Also used : RefClass(com.intellij.codeInspection.reference.RefClass) CommonProblemDescriptor(com.intellij.codeInspection.CommonProblemDescriptor) PsiClass(com.intellij.psi.PsiClass) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with RefClass

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

the class CyclicClassDependencyInspection method checkElement.

@Override
@Nullable
public CommonProblemDescriptor[] checkElement(@NotNull RefEntity refEntity, @NotNull AnalysisScope analysisScope, @NotNull InspectionManager inspectionManager, @NotNull GlobalInspectionContext globalInspectionContext) {
    if (!(refEntity instanceof RefClass)) {
        return null;
    }
    final RefClass refClass = (RefClass) refEntity;
    if (refClass.isAnonymous() || refClass.isLocalClass() || refClass.isSyntheticJSP()) {
        return null;
    }
    final Set<RefClass> dependencies = DependencyUtils.calculateTransitiveDependenciesForClass(refClass);
    final Set<RefClass> dependents = DependencyUtils.calculateTransitiveDependentsForClass(refClass);
    final Set<RefClass> mutualDependents = new HashSet<>(dependencies);
    mutualDependents.retainAll(dependents);
    final int numMutualDependents = mutualDependents.size();
    if (numMutualDependents == 0) {
        return null;
    }
    final String errorString;
    if (numMutualDependents == 1) {
        final RefClass[] classes = mutualDependents.toArray(new RefClass[1]);
        errorString = InspectionGadgetsBundle.message("cyclic.class.dependency.1.problem.descriptor", refEntity.getName(), classes[0].getExternalName());
    } else if (numMutualDependents == 2) {
        final RefClass[] classes = mutualDependents.toArray(new RefClass[2]);
        Arrays.sort(classes, RefEntityAlphabeticalComparator.getInstance());
        errorString = InspectionGadgetsBundle.message("cyclic.class.dependency.2.problem.descriptor", refEntity.getName(), classes[0].getExternalName(), classes[1].getExternalName());
    } else {
        errorString = InspectionGadgetsBundle.message("cyclic.class.dependency.problem.descriptor", refEntity.getName(), Integer.valueOf(numMutualDependents));
    }
    final PsiClass aClass = refClass.getElement();
    if (aClass == null) {
        return null;
    }
    final PsiElement anchor = aClass.getNameIdentifier();
    if (anchor == null)
        return null;
    return new CommonProblemDescriptor[] { inspectionManager.createProblemDescriptor(anchor, errorString, (LocalQuickFix) null, ProblemHighlightType.GENERIC_ERROR_OR_WARNING, false) };
}
Also used : RefClass(com.intellij.codeInspection.reference.RefClass) PsiClass(com.intellij.psi.PsiClass) PsiElement(com.intellij.psi.PsiElement) HashSet(java.util.HashSet) Nullable(org.jetbrains.annotations.Nullable)

Example 4 with RefClass

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

the class CyclicClassInitializationInspection method checkElement.

@Override
@Nullable
public CommonProblemDescriptor[] checkElement(@NotNull RefEntity refEntity, @NotNull AnalysisScope analysisScope, @NotNull InspectionManager inspectionManager, @NotNull GlobalInspectionContext globalInspectionContext) {
    if (!(refEntity instanceof RefClass)) {
        return null;
    }
    final RefClass refClass = (RefClass) refEntity;
    final PsiClass aClass = refClass.getElement();
    if (aClass.getContainingClass() != null) {
        return null;
    }
    final Set<RefClass> dependencies = InitializationDependencyUtils.calculateTransitiveInitializationDependentsForClass(refClass);
    final Set<RefClass> dependents = InitializationDependencyUtils.calculateTransitiveInitializationDependenciesForClass(refClass);
    final Set<RefClass> mutualDependents = new HashSet<>(dependencies);
    mutualDependents.retainAll(dependents);
    final int numMutualDependents = mutualDependents.size();
    if (numMutualDependents == 0) {
        return null;
    }
    final String errorString = InspectionGadgetsBundle.message("cyclic.class.initialization.problem.descriptor", refEntity.getName(), numMutualDependents);
    return new CommonProblemDescriptor[] { inspectionManager.createProblemDescriptor(errorString) };
}
Also used : RefClass(com.intellij.codeInspection.reference.RefClass) CommonProblemDescriptor(com.intellij.codeInspection.CommonProblemDescriptor) PsiClass(com.intellij.psi.PsiClass) HashSet(java.util.HashSet) Nullable(org.jetbrains.annotations.Nullable)

Example 5 with RefClass

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

the class InitializationDependencyUtils method tabulateInitializationTransitiveDependentClasses.

private static void tabulateInitializationTransitiveDependentClasses(RefClass refClass, Set<RefClass> newDependents) {
    final LinkedList<RefClass> pendingClasses = new LinkedList<>();
    final Set<RefClass> processedClasses = new HashSet<>();
    pendingClasses.addLast(refClass);
    while (!pendingClasses.isEmpty()) {
        final RefClass classToProcess = pendingClasses.removeFirst();
        newDependents.add(classToProcess);
        processedClasses.add(classToProcess);
        final Set<RefClass> dependents = calculateInitializationDependentsForClass(classToProcess);
        for (RefClass dependent : dependents) {
            if (!pendingClasses.contains(dependent) && !processedClasses.contains(dependent)) {
                pendingClasses.addLast(dependent);
            }
        }
    }
    newDependents.remove(refClass);
}
Also used : RefClass(com.intellij.codeInspection.reference.RefClass)

Aggregations

RefClass (com.intellij.codeInspection.reference.RefClass)21 Nullable (org.jetbrains.annotations.Nullable)14 CommonProblemDescriptor (com.intellij.codeInspection.CommonProblemDescriptor)13 RefEntity (com.intellij.codeInspection.reference.RefEntity)11 PsiClass (com.intellij.psi.PsiClass)8 RefPackage (com.intellij.codeInspection.reference.RefPackage)7 HashSet (java.util.HashSet)6 RefModule (com.intellij.codeInspection.reference.RefModule)4 Set (java.util.Set)3 RefElement (com.intellij.codeInspection.reference.RefElement)2 RefJavaUtil (com.intellij.codeInspection.reference.RefJavaUtil)2 Project (com.intellij.openapi.project.Project)2 PsiIdentifier (com.intellij.psi.PsiIdentifier)2 RefJavaVisitor (com.intellij.codeInspection.reference.RefJavaVisitor)1 RefManager (com.intellij.codeInspection.reference.RefManager)1 Module (com.intellij.openapi.module.Module)1 PsiElement (com.intellij.psi.PsiElement)1 ArrayList (java.util.ArrayList)1