Search in sources :

Example 1 with DependencyRule

use of com.intellij.packageDependencies.DependencyRule in project intellij-community by JetBrains.

the class ImportClassFixBase method reduceSuggestedClassesBasedOnDependencyRuleViolation.

private static void reduceSuggestedClassesBasedOnDependencyRuleViolation(PsiFile file, List<PsiClass> availableClasses) {
    final Project project = file.getProject();
    final DependencyValidationManager validationManager = DependencyValidationManager.getInstance(project);
    for (int i = availableClasses.size() - 1; i >= 0; i--) {
        PsiClass psiClass = availableClasses.get(i);
        PsiFile targetFile = psiClass.getContainingFile();
        if (targetFile == null)
            continue;
        final DependencyRule[] violated = validationManager.getViolatorDependencyRules(file, targetFile);
        if (violated.length != 0) {
            availableClasses.remove(i);
            if (availableClasses.size() == 1)
                break;
        }
    }
}
Also used : Project(com.intellij.openapi.project.Project) DependencyValidationManager(com.intellij.packageDependencies.DependencyValidationManager) DependencyRule(com.intellij.packageDependencies.DependencyRule)

Example 2 with DependencyRule

use of com.intellij.packageDependencies.DependencyRule in project intellij-community by JetBrains.

the class DependencyInspectionBase method checkFile.

@Override
@Nullable
public ProblemDescriptor[] checkFile(@NotNull final PsiFile file, @NotNull final InspectionManager manager, final boolean isOnTheFly) {
    if (file.getViewProvider().getPsi(JavaLanguage.INSTANCE) == null) {
        return null;
    }
    final DependencyValidationManager validationManager = DependencyValidationManager.getInstance(file.getProject());
    if (!validationManager.hasRules() || validationManager.getApplicableRules(file).length == 0) {
        return null;
    }
    final List<ProblemDescriptor> problems = ContainerUtil.newSmartList();
    DependenciesBuilder.analyzeFileDependencies(file, new DependenciesBuilder.DependencyProcessor() {

        @SuppressWarnings("MismatchedQueryAndUpdateOfCollection")
        private final Map<PsiFile, DependencyRule[]> violations = new FactoryMap<PsiFile, DependencyRule[]>() {

            @Nullable
            @Override
            protected DependencyRule[] create(PsiFile dependencyFile) {
                return validationManager.getViolatorDependencyRules(file, dependencyFile);
            }
        };

        @Override
        public void process(PsiElement place, PsiElement dependency) {
            PsiFile dependencyFile = dependency.getContainingFile();
            if (dependencyFile != null && dependencyFile.isPhysical() && dependencyFile.getVirtualFile() != null) {
                for (DependencyRule dependencyRule : violations.get(dependencyFile)) {
                    String message = InspectionsBundle.message("inspection.dependency.violator.problem.descriptor", dependencyRule.getDisplayText());
                    LocalQuickFix[] fixes = createEditDependencyFixes(dependencyRule);
                    problems.add(manager.createProblemDescriptor(place, message, isOnTheFly, fixes, ProblemHighlightType.GENERIC_ERROR_OR_WARNING));
                }
            }
        }
    });
    return problems.isEmpty() ? null : problems.toArray(new ProblemDescriptor[problems.size()]);
}
Also used : DependenciesBuilder(com.intellij.packageDependencies.DependenciesBuilder) FactoryMap(com.intellij.util.containers.FactoryMap) DependencyValidationManager(com.intellij.packageDependencies.DependencyValidationManager) PsiFile(com.intellij.psi.PsiFile) DependencyRule(com.intellij.packageDependencies.DependencyRule) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with DependencyRule

use of com.intellij.packageDependencies.DependencyRule in project intellij-community by JetBrains.

the class DependencyConfigurable method isModified.

@Override
public boolean isModified() {
    final DependencyValidationManager validationManager = DependencyValidationManager.getInstance(myProject);
    if (validationManager.skipImportStatements() != mySkipImports.isSelected())
        return true;
    final List<DependencyRule> rules = new ArrayList<>();
    rules.addAll(myDenyRulesModel.getItems());
    rules.addAll(myAllowRulesModel.getItems());
    return !Arrays.asList(validationManager.getAllRules()).equals(rules);
}
Also used : DependencyValidationManager(com.intellij.packageDependencies.DependencyValidationManager) ArrayList(java.util.ArrayList) DependencyRule(com.intellij.packageDependencies.DependencyRule)

Example 4 with DependencyRule

use of com.intellij.packageDependencies.DependencyRule in project intellij-community by JetBrains.

the class DependencyConfigurable method apply.

@Override
public void apply() throws ConfigurationException {
    stopTableEditing();
    DependencyValidationManager validationManager = DependencyValidationManager.getInstance(myProject);
    validationManager.removeAllRules();
    final HashMap<String, PackageSet> unUsed = new HashMap<>(validationManager.getUnnamedScopes());
    List<DependencyRule> modelItems = new ArrayList<>();
    modelItems.addAll(myDenyRulesModel.getItems());
    modelItems.addAll(myAllowRulesModel.getItems());
    for (DependencyRule rule : modelItems) {
        validationManager.addRule(rule);
        final NamedScope fromScope = rule.getFromScope();
        if (fromScope instanceof NamedScope.UnnamedScope) {
            final PackageSet fromPackageSet = fromScope.getValue();
            LOG.assertTrue(fromPackageSet != null);
            unUsed.remove(fromPackageSet.getText());
        }
        final NamedScope toScope = rule.getToScope();
        if (toScope instanceof NamedScope.UnnamedScope) {
            final PackageSet toPackageSet = toScope.getValue();
            LOG.assertTrue(toPackageSet != null);
            unUsed.remove(toPackageSet.getText());
        }
    }
    for (String text : unUsed.keySet()) {
        //cleanup
        validationManager.getUnnamedScopes().remove(text);
    }
    validationManager.setSkipImportStatements(mySkipImports.isSelected());
    DaemonCodeAnalyzer.getInstance(myProject).restart();
}
Also used : DependencyValidationManager(com.intellij.packageDependencies.DependencyValidationManager) NamedScope(com.intellij.psi.search.scope.packageSet.NamedScope) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) DependencyRule(com.intellij.packageDependencies.DependencyRule) PackageSet(com.intellij.psi.search.scope.packageSet.PackageSet)

Example 5 with DependencyRule

use of com.intellij.packageDependencies.DependencyRule in project intellij-community by JetBrains.

the class DependencyConfigurable method reset.

@Override
public void reset() {
    final DependencyValidationManager validationManager = DependencyValidationManager.getInstance(myProject);
    DependencyRule[] rules = validationManager.getAllRules();
    final ArrayList<DependencyRule> denyList = new ArrayList<>();
    final ArrayList<DependencyRule> allowList = new ArrayList<>();
    for (DependencyRule rule : rules) {
        if (rule.isDenyRule()) {
            denyList.add(rule.createCopy());
        } else {
            allowList.add(rule.createCopy());
        }
    }
    myDenyRulesModel.setItems(denyList);
    myAllowRulesModel.setItems(allowList);
    mySkipImports.setSelected(validationManager.skipImportStatements());
}
Also used : DependencyValidationManager(com.intellij.packageDependencies.DependencyValidationManager) ArrayList(java.util.ArrayList) DependencyRule(com.intellij.packageDependencies.DependencyRule)

Aggregations

DependencyRule (com.intellij.packageDependencies.DependencyRule)5 DependencyValidationManager (com.intellij.packageDependencies.DependencyValidationManager)5 ArrayList (java.util.ArrayList)3 Project (com.intellij.openapi.project.Project)1 DependenciesBuilder (com.intellij.packageDependencies.DependenciesBuilder)1 PsiElement (com.intellij.psi.PsiElement)1 PsiFile (com.intellij.psi.PsiFile)1 NamedScope (com.intellij.psi.search.scope.packageSet.NamedScope)1 PackageSet (com.intellij.psi.search.scope.packageSet.PackageSet)1 FactoryMap (com.intellij.util.containers.FactoryMap)1 HashMap (java.util.HashMap)1 Nullable (org.jetbrains.annotations.Nullable)1