Search in sources :

Example 1 with RefPackage

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

the class CyclicPackageDependencyInspection 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 Set<RefPackage> dependencies = DependencyUtils.calculateTransitiveDependenciesForPackage(refPackage);
    final Set<RefPackage> dependents = DependencyUtils.calculateTransitiveDependentsForPackage(refPackage);
    final Set<RefPackage> mutualDependents = new HashSet<>(dependencies);
    mutualDependents.retainAll(dependents);
    final int numMutualDependents = mutualDependents.size();
    if (numMutualDependents == 0) {
        return null;
    }
    final String packageName = refPackage.getQualifiedName();
    final String errorString;
    if (numMutualDependents == 1) {
        final RefPackage[] packages = mutualDependents.toArray(new RefPackage[1]);
        errorString = InspectionGadgetsBundle.message("cyclic.package.dependency.1.problem.descriptor", packageName, packages[0].getQualifiedName());
    } else if (numMutualDependents == 2) {
        final RefPackage[] packages = mutualDependents.toArray(new RefPackage[2]);
        Arrays.sort(packages, RefEntityAlphabeticalComparator.getInstance());
        errorString = InspectionGadgetsBundle.message("cyclic.package.dependency.2.problem.descriptor", packageName, packages[0].getQualifiedName(), packages[1].getQualifiedName());
    } else {
        errorString = InspectionGadgetsBundle.message("cyclic.package.dependency.problem.descriptor", packageName, Integer.valueOf(numMutualDependents));
    }
    return new CommonProblemDescriptor[] { inspectionManager.createProblemDescriptor(errorString) };
}
Also used : CommonProblemDescriptor(com.intellij.codeInspection.CommonProblemDescriptor) RefPackage(com.intellij.codeInspection.reference.RefPackage) HashSet(java.util.HashSet) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with RefPackage

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

the class PackageNamingConventionInspection 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;
    }
    @NonNls final String name = refEntity.getName();
    if ("default package".equals(name)) {
        return null;
    }
    final int length = name.length();
    if (length < m_minLength) {
        final String errorString = InspectionGadgetsBundle.message("package.naming.convention.problem.descriptor.short", name);
        return new CommonProblemDescriptor[] { inspectionManager.createProblemDescriptor(errorString) };
    }
    if (length > m_maxLength) {
        final String errorString = InspectionGadgetsBundle.message("package.naming.convention.problem.descriptor.long", name);
        return new CommonProblemDescriptor[] { inspectionManager.createProblemDescriptor(errorString) };
    }
    final Matcher matcher = m_regexPattern.matcher(name);
    if (matcher.matches()) {
        return null;
    } else {
        final String errorString = InspectionGadgetsBundle.message("package.naming.convention.problem.descriptor.regex.mismatch", name, m_regex);
        return new CommonProblemDescriptor[] { inspectionManager.createProblemDescriptor(errorString) };
    }
}
Also used : NonNls(org.jetbrains.annotations.NonNls) CommonProblemDescriptor(com.intellij.codeInspection.CommonProblemDescriptor) Matcher(java.util.regex.Matcher) RefPackage(com.intellij.codeInspection.reference.RefPackage) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with RefPackage

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

the class PackageInMultipleModulesInspection 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 List<RefEntity> children = refEntity.getChildren();
    final Set<RefModule> modules = new HashSet<>();
    for (RefEntity child : children) {
        if (!(child instanceof RefClass)) {
            continue;
        }
        final RefClass refClass = (RefClass) child;
        final RefModule module = refClass.getModule();
        modules.add(module);
    }
    if (modules.size() <= 1) {
        return null;
    }
    final String errorString = InspectionGadgetsBundle.message("package.in.multiple.modules.problem.descriptor", refEntity.getQualifiedName());
    return new CommonProblemDescriptor[] { inspectionManager.createProblemDescriptor(errorString) };
}
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) HashSet(java.util.HashSet) Nullable(org.jetbrains.annotations.Nullable)

Example 4 with RefPackage

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

the class PackageWithTooFewClassesInspection 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 List<RefEntity> children = refEntity.getChildren();
    int numClasses = 0;
    boolean subpackage = false;
    for (RefEntity child : children) {
        if (child instanceof RefClass) {
            numClasses++;
        } else if (child instanceof RefPackage) {
            subpackage = true;
        }
    }
    if (numClasses >= limit || (numClasses == 0 && subpackage)) {
        return null;
    }
    final String errorString = InspectionGadgetsBundle.message("package.with.too.few.classes.problem.descriptor", refEntity.getQualifiedName(), Integer.valueOf(numClasses), Integer.valueOf(limit));
    return new CommonProblemDescriptor[] { inspectionManager.createProblemDescriptor(errorString) };
}
Also used : RefClass(com.intellij.codeInspection.reference.RefClass) CommonProblemDescriptor(com.intellij.codeInspection.CommonProblemDescriptor) RefEntity(com.intellij.codeInspection.reference.RefEntity) RefPackage(com.intellij.codeInspection.reference.RefPackage) Nullable(org.jetbrains.annotations.Nullable)

Example 5 with RefPackage

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

the class PackageWithTooManyClassesInspection 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 List<RefEntity> children = refEntity.getChildren();
    int numClasses = 0;
    for (RefEntity child : children) {
        if (child instanceof RefClass) {
            numClasses++;
        }
    }
    if (numClasses <= limit) {
        return null;
    }
    final String errorString = InspectionGadgetsBundle.message("package.with.too.many.classes.problem.descriptor", refEntity.getQualifiedName(), Integer.valueOf(numClasses), Integer.valueOf(limit));
    return new CommonProblemDescriptor[] { inspectionManager.createProblemDescriptor(errorString) };
}
Also used : RefClass(com.intellij.codeInspection.reference.RefClass) CommonProblemDescriptor(com.intellij.codeInspection.CommonProblemDescriptor) RefEntity(com.intellij.codeInspection.reference.RefEntity) RefPackage(com.intellij.codeInspection.reference.RefPackage) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

CommonProblemDescriptor (com.intellij.codeInspection.CommonProblemDescriptor)9 RefPackage (com.intellij.codeInspection.reference.RefPackage)9 Nullable (org.jetbrains.annotations.Nullable)9 RefClass (com.intellij.codeInspection.reference.RefClass)7 RefEntity (com.intellij.codeInspection.reference.RefEntity)7 PsiClass (com.intellij.psi.PsiClass)3 HashSet (java.util.HashSet)3 RefModule (com.intellij.codeInspection.reference.RefModule)2 PsiIdentifier (com.intellij.psi.PsiIdentifier)2 Project (com.intellij.openapi.project.Project)1 Set (java.util.Set)1 Matcher (java.util.regex.Matcher)1 NonNls (org.jetbrains.annotations.NonNls)1