Search in sources :

Example 1 with JavaAnalysisScope

use of com.intellij.analysis.JavaAnalysisScope in project intellij-community by JetBrains.

the class CyclicDependenciesAction method getInspectionScopeImpl.

@Nullable
private static AnalysisScope getInspectionScopeImpl(DataContext dataContext) {
    //Possible scopes: package, project, module.
    Project projectContext = PlatformDataKeys.PROJECT_CONTEXT.getData(dataContext);
    if (projectContext != null) {
        return null;
    }
    Module moduleContext = LangDataKeys.MODULE_CONTEXT.getData(dataContext);
    if (moduleContext != null) {
        return null;
    }
    final Module[] modulesArray = LangDataKeys.MODULE_CONTEXT_ARRAY.getData(dataContext);
    if (modulesArray != null && modulesArray.length > 0) {
        return new AnalysisScope(modulesArray);
    }
    PsiElement psiTarget = CommonDataKeys.PSI_ELEMENT.getData(dataContext);
    if (psiTarget instanceof PsiDirectory) {
        PsiDirectory psiDirectory = (PsiDirectory) psiTarget;
        if (!psiDirectory.getManager().isInProject(psiDirectory))
            return null;
        return new AnalysisScope(psiDirectory);
    } else if (psiTarget instanceof PsiPackage) {
        PsiPackage pack = (PsiPackage) psiTarget;
        PsiDirectory[] dirs = pack.getDirectories(GlobalSearchScope.projectScope(pack.getProject()));
        if (dirs.length == 0)
            return null;
        return new JavaAnalysisScope(pack, LangDataKeys.MODULE.getData(dataContext));
    }
    return null;
}
Also used : JavaAnalysisScope(com.intellij.analysis.JavaAnalysisScope) AnalysisScope(com.intellij.analysis.AnalysisScope) Project(com.intellij.openapi.project.Project) JavaAnalysisScope(com.intellij.analysis.JavaAnalysisScope) PsiDirectory(com.intellij.psi.PsiDirectory) PsiPackage(com.intellij.psi.PsiPackage) Module(com.intellij.openapi.module.Module) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with JavaAnalysisScope

use of com.intellij.analysis.JavaAnalysisScope in project intellij-community by JetBrains.

the class UsagesInAnalyzingDependenciesTest method testForwardPackageScope.

@SuppressWarnings("ConstantConditions")
public void testForwardPackageScope() {
    PsiPackage bPackage = JavaPsiFacade.getInstance(myPsiManager.getProject()).findPackage("com.b");
    DependenciesBuilder builder = new ForwardDependenciesBuilder(myProject, new JavaAnalysisScope(bPackage, null));
    builder.analyze();
    Set<PsiFile> searchFor = new HashSet<>();
    searchFor.add(myJavaFacade.findClass("com.a.A", GlobalSearchScope.allScope(myProject)).getContainingFile());
    Set<PsiFile> searchIn = new HashSet<>();
    PsiClass bClass = myJavaFacade.findClass("com.b.B", GlobalSearchScope.allScope(myProject));
    searchIn.add(bClass.getContainingFile());
    PsiClass cClass = myJavaFacade.findClass("com.b.C", GlobalSearchScope.allScope(myProject));
    searchIn.add(cClass.getContainingFile());
    UsageInfo[] usagesInfos = FindDependencyUtil.findDependencies(builder, searchIn, searchFor);
    UsageInfo2UsageAdapter[] usages = UsageInfo2UsageAdapter.convert(usagesInfos);
    String[] psiUsages = new String[usagesInfos.length];
    for (int i = 0; i < usagesInfos.length; i++) {
        psiUsages[i] = toString(usages[i]);
    }
    checkResult(new String[] { "2 import com.a.A;", "4 A myA = new A();", "4 A myA = new A();", "6 myA.aa();", "2 import com.a.A;", "4 A myA = new A();", "4 A myA = new A();", "6 myA.aa();" }, psiUsages);
}
Also used : DependenciesBuilder(com.intellij.packageDependencies.DependenciesBuilder) ForwardDependenciesBuilder(com.intellij.packageDependencies.ForwardDependenciesBuilder) BackwardDependenciesBuilder(com.intellij.packageDependencies.BackwardDependenciesBuilder) JavaAnalysisScope(com.intellij.analysis.JavaAnalysisScope) PsiClass(com.intellij.psi.PsiClass) PsiPackage(com.intellij.psi.PsiPackage) UsageInfo2UsageAdapter(com.intellij.usages.UsageInfo2UsageAdapter) PsiFile(com.intellij.psi.PsiFile) ForwardDependenciesBuilder(com.intellij.packageDependencies.ForwardDependenciesBuilder) UsageInfo(com.intellij.usageView.UsageInfo) HashSet(java.util.HashSet)

Example 3 with JavaAnalysisScope

use of com.intellij.analysis.JavaAnalysisScope in project intellij-community by JetBrains.

the class UsagesInAnalyzingDependenciesTest method testBackwardPackageScope.

@SuppressWarnings("ConstantConditions")
public void testBackwardPackageScope() {
    PsiPackage bPackage = JavaPsiFacade.getInstance(myPsiManager.getProject()).findPackage("com.a");
    DependenciesBuilder builder = new BackwardDependenciesBuilder(myProject, new JavaAnalysisScope(bPackage, null));
    builder.analyze();
    Set<PsiFile> searchFor = new HashSet<>();
    searchFor.add(myJavaFacade.findClass("com.a.A", GlobalSearchScope.allScope(myProject)).getContainingFile());
    Set<PsiFile> searchIn = new HashSet<>();
    PsiClass bClass = myJavaFacade.findClass("com.b.B", GlobalSearchScope.allScope(myProject));
    searchIn.add(bClass.getContainingFile());
    PsiClass cClass = myJavaFacade.findClass("com.a.C", GlobalSearchScope.allScope(myProject));
    searchFor.add(cClass.getContainingFile());
    UsageInfo[] usagesInfos = FindDependencyUtil.findBackwardDependencies(builder, searchIn, searchFor);
    UsageInfo2UsageAdapter[] usages = UsageInfo2UsageAdapter.convert(usagesInfos);
    String[] psiUsages = new String[usagesInfos.length];
    for (int i = 0; i < usagesInfos.length; i++) {
        psiUsages[i] = toString(usages[i]);
    }
    checkResult(new String[] { "4 A myA = new A();", "4 A myA = new A();", "5 C myC = new C();", "5 C myC = new C();", "7 myA.aa();", "8 myC.cc();" }, psiUsages);
}
Also used : DependenciesBuilder(com.intellij.packageDependencies.DependenciesBuilder) ForwardDependenciesBuilder(com.intellij.packageDependencies.ForwardDependenciesBuilder) BackwardDependenciesBuilder(com.intellij.packageDependencies.BackwardDependenciesBuilder) JavaAnalysisScope(com.intellij.analysis.JavaAnalysisScope) PsiClass(com.intellij.psi.PsiClass) PsiPackage(com.intellij.psi.PsiPackage) UsageInfo2UsageAdapter(com.intellij.usages.UsageInfo2UsageAdapter) BackwardDependenciesBuilder(com.intellij.packageDependencies.BackwardDependenciesBuilder) PsiFile(com.intellij.psi.PsiFile) UsageInfo(com.intellij.usageView.UsageInfo) HashSet(java.util.HashSet)

Example 4 with JavaAnalysisScope

use of com.intellij.analysis.JavaAnalysisScope in project intellij-community by JetBrains.

the class CyclicDependenciesTest method testPackageScope2.

public void testPackageScope2() {
    //com.b<->com.a  - find
    //com.c<->com.d  - not in scope
    final CyclicDependenciesBuilder builder = new CyclicDependenciesBuilder(myProject, new JavaAnalysisScope(JavaPsiFacade.getInstance(myPsiManager.getProject()).findPackage("com.subscope1"), null));
    builder.analyze();
    final HashMap<PsiPackage, Set<List<PsiPackage>>> cyclicDependencies = builder.getCyclicDependencies();
    HashMap<String, String[][]> expected = new HashMap<>();
    expected.put("com.subscope1.b", new String[][] { { "com.subscope1.a", "com.subscope1.b" } });
    expected.put("com.subscope1.a", new String[][] { { "com.subscope1.b", "com.subscope1.a" } });
    checkResult(expected, cyclicDependencies);
}
Also used : JavaAnalysisScope(com.intellij.analysis.JavaAnalysisScope) CyclicDependenciesBuilder(com.intellij.cyclicDependencies.CyclicDependenciesBuilder) PsiPackage(com.intellij.psi.PsiPackage)

Example 5 with JavaAnalysisScope

use of com.intellij.analysis.JavaAnalysisScope in project intellij-community by JetBrains.

the class CyclicDependenciesTest method testPackageScope1.

public void testPackageScope1() {
    // com.a<->com.b
    final CyclicDependenciesBuilder builder = new CyclicDependenciesBuilder(myProject, new JavaAnalysisScope(JavaPsiFacade.getInstance(myPsiManager.getProject()).findPackage("com"), null));
    builder.analyze();
    final HashMap<PsiPackage, Set<List<PsiPackage>>> cyclicDependencies = builder.getCyclicDependencies();
    HashMap<String, String[][]> expected = new HashMap<>();
    expected.put("com.b", new String[][] { { "com.a", "com.b" } });
    expected.put("com.a", new String[][] { { "com.b", "com.a" } });
    checkResult(expected, cyclicDependencies);
}
Also used : JavaAnalysisScope(com.intellij.analysis.JavaAnalysisScope) CyclicDependenciesBuilder(com.intellij.cyclicDependencies.CyclicDependenciesBuilder) PsiPackage(com.intellij.psi.PsiPackage)

Aggregations

JavaAnalysisScope (com.intellij.analysis.JavaAnalysisScope)5 PsiPackage (com.intellij.psi.PsiPackage)5 CyclicDependenciesBuilder (com.intellij.cyclicDependencies.CyclicDependenciesBuilder)2 BackwardDependenciesBuilder (com.intellij.packageDependencies.BackwardDependenciesBuilder)2 DependenciesBuilder (com.intellij.packageDependencies.DependenciesBuilder)2 ForwardDependenciesBuilder (com.intellij.packageDependencies.ForwardDependenciesBuilder)2 PsiClass (com.intellij.psi.PsiClass)2 PsiFile (com.intellij.psi.PsiFile)2 UsageInfo (com.intellij.usageView.UsageInfo)2 UsageInfo2UsageAdapter (com.intellij.usages.UsageInfo2UsageAdapter)2 HashSet (java.util.HashSet)2 AnalysisScope (com.intellij.analysis.AnalysisScope)1 Module (com.intellij.openapi.module.Module)1 Project (com.intellij.openapi.project.Project)1 PsiDirectory (com.intellij.psi.PsiDirectory)1 PsiElement (com.intellij.psi.PsiElement)1 Nullable (org.jetbrains.annotations.Nullable)1