Search in sources :

Example 96 with UsageInfo

use of com.intellij.usageView.UsageInfo 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 97 with UsageInfo

use of com.intellij.usageView.UsageInfo in project intellij-community by JetBrains.

the class UsagesInAnalyzingDependenciesTest method testForwardJdkClasses.

@SuppressWarnings("ConstantConditions")
public void testForwardJdkClasses() {
    DependenciesBuilder builder = new ForwardDependenciesBuilder(myProject, new AnalysisScope(myProject));
    builder.analyze();
    Set<PsiFile> searchIn = new HashSet<>();
    PsiClass aClass = myJavaFacade.findClass("A", GlobalSearchScope.allScope(myProject));
    searchIn.add(aClass.getContainingFile());
    Set<PsiFile> searchFor = new HashSet<>();
    PsiClass stringClass = myJavaFacade.findClass("java.lang.String", GlobalSearchScope.allScope(myProject));
    searchFor.add((PsiFile) stringClass.getContainingFile().getNavigationElement());
    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 String myName;" }, psiUsages);
}
Also used : DependenciesBuilder(com.intellij.packageDependencies.DependenciesBuilder) ForwardDependenciesBuilder(com.intellij.packageDependencies.ForwardDependenciesBuilder) BackwardDependenciesBuilder(com.intellij.packageDependencies.BackwardDependenciesBuilder) PsiClass(com.intellij.psi.PsiClass) JavaAnalysisScope(com.intellij.analysis.JavaAnalysisScope) AnalysisScope(com.intellij.analysis.AnalysisScope) 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 98 with UsageInfo

use of com.intellij.usageView.UsageInfo in project intellij-community by JetBrains.

the class FindUsagesTest method testNonCodeClassUsages.

public void testNonCodeClassUsages() throws Exception {
    final TempDirTestFixture tdf = IdeaTestFixtureFactory.getFixtureFactory().createTempDirTestFixture();
    tdf.setUp();
    try {
        new WriteCommandAction(getProject()) {

            @Override
            protected void run(@NotNull Result result) throws Throwable {
                final ModifiableModuleModel moduleModel = ModuleManager.getInstance(getProject()).getModifiableModel();
                moduleModel.newModule("independent/independent.iml", StdModuleTypes.JAVA.getId());
                moduleModel.commit();
                tdf.createFile("plugin.xml", "<document>\n" + "  <action class=\"com.Foo\" />\n" + "  <action class=\"com.Foo.Bar\" />\n" + "  <action class=\"com.Foo$Bar\" />\n" + "</document>");
                PsiTestUtil.addContentRoot(ModuleManager.getInstance(getProject()).findModuleByName("independent"), tdf.getFile(""));
            }
        }.execute();
        GlobalSearchScope scope = GlobalSearchScope.allScope(getProject());
        PsiClass foo = myJavaFacade.findClass("com.Foo", scope);
        PsiClass bar = myJavaFacade.findClass("com.Foo.Bar", scope);
        final int[] count = { 0 };
        Processor<UsageInfo> processor = usageInfo -> {
            int navigationOffset = usageInfo.getNavigationOffset();
            assertTrue(navigationOffset > 0);
            String textAfter = usageInfo.getFile().getText().substring(navigationOffset);
            assertTrue(textAfter, textAfter.startsWith("Foo") || textAfter.startsWith("Bar") || textAfter.startsWith("com.Foo.Bar"));
            count[0]++;
            return true;
        };
        JavaFindUsagesHandler handler = new JavaFindUsagesHandler(bar, JavaFindUsagesHandlerFactory.getInstance(getProject()));
        count[0] = 0;
        handler.processUsagesInText(foo, processor, scope);
        assertEquals(3, count[0]);
        count[0] = 0;
        handler.processUsagesInText(bar, processor, scope);
        assertEquals(2, count[0]);
    } finally {
        tdf.tearDown();
    }
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) TempDirTestFixture(com.intellij.testFramework.fixtures.TempDirTestFixture) ModuleManager(com.intellij.openapi.module.ModuleManager) JavaFindUsagesHandlerFactory(com.intellij.find.findUsages.JavaFindUsagesHandlerFactory) UsageInfo(com.intellij.usageView.UsageInfo) IntArrayList(com.intellij.util.containers.IntArrayList) ArrayList(java.util.ArrayList) MethodReferencesSearch(com.intellij.psi.search.searches.MethodReferencesSearch) WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) IdeaTestUtil(com.intellij.testFramework.IdeaTestUtil) PsiTestCase(com.intellij.testFramework.PsiTestCase) ReferencesSearch(com.intellij.psi.search.searches.ReferencesSearch) StdModuleTypes(com.intellij.openapi.module.StdModuleTypes) StdFileTypes(com.intellij.openapi.fileTypes.StdFileTypes) PsiTestUtil(com.intellij.testFramework.PsiTestUtil) JavaTestUtil(com.intellij.JavaTestUtil) OverridingMethodsSearch(com.intellij.psi.search.searches.OverridingMethodsSearch) Collection(java.util.Collection) TextRange(com.intellij.openapi.util.TextRange) ModifiableModuleModel(com.intellij.openapi.module.ModifiableModuleModel) JavaFindUsagesHandler(com.intellij.find.findUsages.JavaFindUsagesHandler) List(java.util.List) Result(com.intellij.openapi.application.Result) Processor(com.intellij.util.Processor) com.intellij.psi(com.intellij.psi) NotNull(org.jetbrains.annotations.NotNull) IdeaTestFixtureFactory(com.intellij.testFramework.fixtures.IdeaTestFixtureFactory) Collections(java.util.Collections) TempDirTestFixture(com.intellij.testFramework.fixtures.TempDirTestFixture) JavaFindUsagesHandler(com.intellij.find.findUsages.JavaFindUsagesHandler) Result(com.intellij.openapi.application.Result) ModifiableModuleModel(com.intellij.openapi.module.ModifiableModuleModel) UsageInfo(com.intellij.usageView.UsageInfo)

Example 99 with UsageInfo

use of com.intellij.usageView.UsageInfo in project intellij-community by JetBrains.

the class SearchInLibsTest method testFindInPathInLibrarySourceDirShouldSearchJustInThisDirectoryOnly.

public void testFindInPathInLibrarySourceDirShouldSearchJustInThisDirectoryOnly() throws Exception {
    FindModel model = new FindModel();
    final PsiClass aClass = myJavaFacade.findClass("x.X");
    assertNotNull(aClass);
    String classDirPath = aClass.getContainingFile().getContainingDirectory().getVirtualFile().getPath();
    String sourceDirPath = ((PsiFile) aClass.getContainingFile().getNavigationElement()).getContainingDirectory().getVirtualFile().getPath();
    assertFalse(classDirPath.equals(sourceDirPath));
    model.setDirectoryName(sourceDirPath);
    model.setCaseSensitive(true);
    model.setCustomScope(false);
    model.setStringToFind("xxx");
    model.setProjectScope(false);
    List<UsageInfo> usages = Collections.synchronizedList(new ArrayList<>());
    CommonProcessors.CollectProcessor<UsageInfo> consumer = new CommonProcessors.CollectProcessor<>(usages);
    FindUsagesProcessPresentation presentation = FindInProjectUtil.setupProcessPresentation(getProject(), false, FindInProjectUtil.setupViewPresentation(false, model));
    FindInProjectUtil.findUsages(model, getProject(), consumer, presentation);
    UsageInfo info = assertOneElement(usages);
    assertEquals("X.java", info.getFile().getName());
}
Also used : FindModel(com.intellij.find.FindModel) FindUsagesProcessPresentation(com.intellij.usages.FindUsagesProcessPresentation) PsiClass(com.intellij.psi.PsiClass) PsiFile(com.intellij.psi.PsiFile) CommonProcessors(com.intellij.util.CommonProcessors) UsageInfo(com.intellij.usageView.UsageInfo)

Example 100 with UsageInfo

use of com.intellij.usageView.UsageInfo in project intellij-community by JetBrains.

the class Util method anyFieldsWithGettersPresent.

public static boolean anyFieldsWithGettersPresent(List<UsageInfo> classMemberRefs) {
    for (UsageInfo usageInfo : classMemberRefs) {
        if (usageInfo.getElement() instanceof PsiReferenceExpression) {
            PsiElement e = ((PsiReferenceExpression) usageInfo.getElement()).resolve();
            if (e instanceof PsiField) {
                PsiField psiField = (PsiField) e;
                PsiMethod getterPrototype = GenerateMembersUtil.generateGetterPrototype(psiField);
                PsiMethod getter = psiField.getContainingClass().findMethodBySignature(getterPrototype, true);
                if (getter != null)
                    return true;
            }
        }
    }
    return false;
}
Also used : UsageInfo(com.intellij.usageView.UsageInfo)

Aggregations

UsageInfo (com.intellij.usageView.UsageInfo)283 PsiElement (com.intellij.psi.PsiElement)70 NotNull (org.jetbrains.annotations.NotNull)57 ArrayList (java.util.ArrayList)41 MoveRenameUsageInfo (com.intellij.refactoring.util.MoveRenameUsageInfo)38 IncorrectOperationException (com.intellij.util.IncorrectOperationException)34 PsiFile (com.intellij.psi.PsiFile)33 MultiMap (com.intellij.util.containers.MultiMap)30 VirtualFile (com.intellij.openapi.vfs.VirtualFile)29 Nullable (org.jetbrains.annotations.Nullable)20 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)19 DefaultConstructorImplicitUsageInfo (com.intellij.refactoring.util.usageInfo.DefaultConstructorImplicitUsageInfo)17 NoConstructorClassUsageInfo (com.intellij.refactoring.util.usageInfo.NoConstructorClassUsageInfo)17 Project (com.intellij.openapi.project.Project)16 HashSet (com.intellij.util.containers.HashSet)15 TextRange (com.intellij.openapi.util.TextRange)12 PsiReference (com.intellij.psi.PsiReference)12 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)11 PsiClass (com.intellij.psi.PsiClass)11 LocalSearchScope (com.intellij.psi.search.LocalSearchScope)11