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;
}
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);
}
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;
}
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());
}
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;
}
Aggregations