Search in sources :

Example 6 with RefPackage

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

the class ClassUnconnectedToPackageInspection method checkElement.

@Override
@Nullable
public CommonProblemDescriptor[] checkElement(@NotNull RefEntity refEntity, @NotNull AnalysisScope analysisScope, @NotNull InspectionManager manager, @NotNull GlobalInspectionContext globalInspectionContext) {
    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);
    for (RefClass dependency : dependencies) {
        if (inSamePackage(refClass, dependency)) {
            return null;
        }
    }
    final Set<RefClass> dependents = DependencyUtils.calculateDependentsForClass(refClass);
    for (RefClass dependent : dependents) {
        if (inSamePackage(refClass, dependent)) {
            return null;
        }
    }
    final PsiClass aClass = refClass.getElement();
    final PsiIdentifier identifier = aClass.getNameIdentifier();
    if (identifier == null) {
        return null;
    }
    return new CommonProblemDescriptor[] { manager.createProblemDescriptor(identifier, InspectionGadgetsBundle.message("class.unconnected.to.package.problem.descriptor"), true, ProblemHighlightType.GENERIC_ERROR_OR_WARNING, false) };
}
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) PsiClass(com.intellij.psi.PsiClass) PsiIdentifier(com.intellij.psi.PsiIdentifier) Nullable(org.jetbrains.annotations.Nullable)

Example 7 with RefPackage

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

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

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

the class MissingPackageInfoInspectionBase method checkElement.

@Nullable
@Override
public CommonProblemDescriptor[] checkElement(@NotNull RefEntity refEntity, @NotNull AnalysisScope scope, @NotNull InspectionManager manager, @NotNull GlobalInspectionContext globalContext) {
    if (!(refEntity instanceof RefPackage)) {
        return null;
    }
    final RefPackage refPackage = (RefPackage) refEntity;
    final String packageName = refPackage.getQualifiedName();
    final Project project = globalContext.getProject();
    final PsiPackage aPackage = JavaPsiFacade.getInstance(project).findPackage(packageName);
    if (hasPackageInfoFile(aPackage)) {
        return null;
    }
    final List<RefEntity> children = refPackage.getChildren();
    boolean hasClasses = false;
    for (RefEntity child : children) {
        if (child instanceof RefClass) {
            hasClasses = true;
            break;
        }
    }
    if (!hasClasses) {
        return null;
    }
    if (PsiUtil.isLanguageLevel5OrHigher(aPackage)) {
        return new CommonProblemDescriptor[] { manager.createProblemDescriptor(InspectionGadgetsBundle.message("missing.package.info.problem.descriptor", packageName)) };
    } else {
        return new CommonProblemDescriptor[] { manager.createProblemDescriptor(InspectionGadgetsBundle.message("missing.package.html.problem.descriptor", packageName)) };
    }
}
Also used : Project(com.intellij.openapi.project.Project) 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