Search in sources :

Example 91 with GlobalSearchScope

use of com.intellij.psi.search.GlobalSearchScope in project intellij-community by JetBrains.

the class ImportUtils method addStaticImport.

/**
   * @return true, if a static import was created or already present. False, if a static import is not possible.
   */
public static boolean addStaticImport(@NotNull String qualifierClass, @NonNls @NotNull String memberName, @NotNull PsiElement context) {
    final PsiClass containingClass = PsiTreeUtil.getParentOfType(context, PsiClass.class);
    if (containingClass != null) {
        if (InheritanceUtil.isInheritor(containingClass, qualifierClass)) {
            return true;
        }
        if (hasAccessibleMemberWithName(containingClass, memberName, context)) {
            return false;
        }
    }
    final PsiFile contextFile = context.getContainingFile();
    if (!(contextFile instanceof PsiJavaFile)) {
        return false;
    }
    final PsiJavaFile javaFile = (PsiJavaFile) contextFile;
    final PsiImportList importList = javaFile.getImportList();
    if (importList == null) {
        return false;
    }
    final PsiImportStatementBase existingImportStatement = importList.findSingleImportStatement(memberName);
    if (existingImportStatement != null) {
        if (existingImportStatement instanceof PsiImportStaticStatement) {
            final PsiImportStaticStatement importStaticStatement = (PsiImportStaticStatement) existingImportStatement;
            if (!memberName.equals(importStaticStatement.getReferenceName())) {
                return false;
            }
            final PsiClass targetClass = importStaticStatement.resolveTargetClass();
            return targetClass != null && qualifierClass.equals(targetClass.getQualifiedName());
        }
        return false;
    }
    final PsiImportStaticStatement onDemandImportStatement = findOnDemandImportStaticStatement(importList, qualifierClass);
    if (onDemandImportStatement != null && !hasOnDemandImportConflict(qualifierClass + '.' + memberName, javaFile)) {
        return true;
    }
    final Project project = context.getProject();
    final GlobalSearchScope scope = context.getResolveScope();
    final JavaPsiFacade psiFacade = JavaPsiFacade.getInstance(project);
    final PsiClass aClass = psiFacade.findClass(qualifierClass, scope);
    if (aClass == null || !PsiUtil.isAccessible(aClass, contextFile, null) || !hasAccessibleMemberWithName(aClass, memberName, contextFile)) {
        return false;
    }
    final String qualifiedName = aClass.getQualifiedName();
    if (qualifiedName == null) {
        return false;
    }
    final List<PsiImportStaticStatement> imports = getMatchingImports(importList, qualifiedName);
    final int onDemandCount = JavaCodeStyleSettingsFacade.getInstance(project).getNamesCountToUseImportOnDemand();
    final PsiElementFactory elementFactory = psiFacade.getElementFactory();
    if (imports.size() + 1 < onDemandCount) {
        importList.add(elementFactory.createImportStaticStatement(aClass, memberName));
    } else {
        for (PsiImportStaticStatement importStatement : imports) {
            importStatement.delete();
        }
        importList.add(elementFactory.createImportStaticStatement(aClass, "*"));
    }
    return true;
}
Also used : Project(com.intellij.openapi.project.Project) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope)

Example 92 with GlobalSearchScope

use of com.intellij.psi.search.GlobalSearchScope in project intellij-community by JetBrains.

the class MethodCallUtils method isSimpleCallToMethod.

public static boolean isSimpleCallToMethod(@NotNull PsiMethodCallExpression expression, @NonNls @Nullable String calledOnClassName, @Nullable PsiType returnType, @NonNls @Nullable String methodName, @NonNls @Nullable String... parameterTypeStrings) {
    if (parameterTypeStrings == null) {
        return isCallToMethod(expression, calledOnClassName, returnType, methodName, (PsiType[]) null);
    }
    final JavaPsiFacade psiFacade = JavaPsiFacade.getInstance(expression.getProject());
    final PsiElementFactory factory = psiFacade.getElementFactory();
    final PsiType[] parameterTypes = PsiType.createArray(parameterTypeStrings.length);
    final GlobalSearchScope scope = expression.getResolveScope();
    for (int i = 0; i < parameterTypeStrings.length; i++) {
        final String parameterTypeString = parameterTypeStrings[i];
        parameterTypes[i] = factory.createTypeByFQClassName(parameterTypeString, scope);
    }
    return isCallToMethod(expression, calledOnClassName, returnType, methodName, parameterTypes);
}
Also used : GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope)

Example 93 with GlobalSearchScope

use of com.intellij.psi.search.GlobalSearchScope in project intellij-community by JetBrains.

the class DebuggerUtils method instanceOf.

public static boolean instanceOf(@NotNull String subType, @NotNull String superType, @Nullable Project project) {
    if (project == null) {
        return subType.equals(superType);
    }
    ArrayClass nodeClass = getArrayClass(subType);
    ArrayClass rendererClass = getArrayClass(superType);
    if (nodeClass == null || rendererClass == null)
        return false;
    if (nodeClass.dims == rendererClass.dims) {
        GlobalSearchScope scope = GlobalSearchScope.allScope(project);
        PsiClass psiNodeClass = JavaPsiFacade.getInstance(project).findClass(nodeClass.className, scope);
        PsiClass psiRendererClass = JavaPsiFacade.getInstance(project).findClass(rendererClass.className, scope);
        return InheritanceUtil.isInheritorOrSelf(psiNodeClass, psiRendererClass, true);
    } else if (nodeClass.dims > rendererClass.dims) {
        return rendererClass.className.equals(CommonClassNames.JAVA_LANG_OBJECT);
    }
    return false;
}
Also used : GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope)

Example 94 with GlobalSearchScope

use of com.intellij.psi.search.GlobalSearchScope in project intellij-community by JetBrains.

the class JUnitUtil method getTestCaseClassOrNull.

@Nullable
private static PsiClass getTestCaseClassOrNull(final Location<?> location) {
    final Location<PsiClass> ancestorOrSelf = location.getAncestorOrSelf(PsiClass.class);
    if (ancestorOrSelf == null)
        return null;
    final PsiClass aClass = ancestorOrSelf.getPsiElement();
    Module module = JavaExecutionUtil.findModule(aClass);
    if (module == null)
        return null;
    GlobalSearchScope scope = GlobalSearchScope.moduleRuntimeScope(module, true);
    return getTestCaseClassOrNull(scope, module.getProject());
}
Also used : GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) Module(com.intellij.openapi.module.Module) Nullable(org.jetbrains.annotations.Nullable)

Example 95 with GlobalSearchScope

use of com.intellij.psi.search.GlobalSearchScope in project intellij-community by JetBrains.

the class LocationUtil method isJarAttached.

public static boolean isJarAttached(@NotNull Location location, final PsiDirectory[] directories, final String... fqns) {
    final JavaPsiFacade facade = JavaPsiFacade.getInstance(location.getProject());
    final Module locationModule = location.getModule();
    if (locationModule != null) {
        for (String fqn : fqns) {
            if (facade.findClass(fqn, GlobalSearchScope.moduleWithDependenciesAndLibrariesScope(locationModule, true)) != null)
                return true;
        }
    } else {
        for (PsiDirectory directory : directories) {
            final Module module = ModuleUtilCore.findModuleForFile(directory.getVirtualFile(), location.getProject());
            if (module != null) {
                GlobalSearchScope scope = GlobalSearchScope.moduleWithDependenciesAndLibrariesScope(module, true);
                for (String fqn : fqns) {
                    if (facade.findClass(fqn, scope) != null) {
                        return true;
                    }
                }
            }
        }
    }
    return false;
}
Also used : JavaPsiFacade(com.intellij.psi.JavaPsiFacade) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) PsiDirectory(com.intellij.psi.PsiDirectory) Module(com.intellij.openapi.module.Module)

Aggregations

GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)485 Project (com.intellij.openapi.project.Project)145 NotNull (org.jetbrains.annotations.NotNull)134 VirtualFile (com.intellij.openapi.vfs.VirtualFile)106 Module (com.intellij.openapi.module.Module)101 Nullable (org.jetbrains.annotations.Nullable)78 PsiClass (com.intellij.psi.PsiClass)53 ArrayList (java.util.ArrayList)37 PsiFile (com.intellij.psi.PsiFile)32 PsiElement (com.intellij.psi.PsiElement)31 THashSet (gnu.trove.THashSet)22 SearchScope (com.intellij.psi.search.SearchScope)19 PsiDirectory (com.intellij.psi.PsiDirectory)18 ContainerUtil (com.intellij.util.containers.ContainerUtil)18 JSClass (com.intellij.lang.javascript.psi.ecmal4.JSClass)17 ProjectFileIndex (com.intellij.openapi.roots.ProjectFileIndex)17 com.intellij.psi (com.intellij.psi)17 List (java.util.List)17 StringUtil (com.intellij.openapi.util.text.StringUtil)15 JavaPsiFacade (com.intellij.psi.JavaPsiFacade)15