Example #1
0
 private static boolean checkCreateClassOrPackage(
     final boolean createJavaClass, final PsiDirectory directory, final String name) {
   try {
     if (createJavaClass) {
       JavaDirectoryService.getInstance().checkCreateClass(directory, name);
     } else {
       directory.checkCreateSubdirectory(name);
     }
     return true;
   } catch (IncorrectOperationException ex) {
     return false;
   }
 }
  @Override
  protected Set<String> getStringsToSearch(final PsiElement element) {
    if (element instanceof PsiDirectory) { // normalize a directory to a corresponding package
      return getStringsToSearch(
          JavaDirectoryService.getInstance().getPackage((PsiDirectory) element));
    }

    final Set<String> result = new HashSet<String>();

    ApplicationManager.getApplication()
        .runReadAction(
            new Runnable() {
              @Override
              public void run() {
                if (element instanceof PsiPackage) {
                  ContainerUtil.addIfNotNull(result, ((PsiPackage) element).getQualifiedName());
                } else if (element instanceof PsiClass) {
                  final String qname = ((PsiClass) element).getQualifiedName();
                  if (qname != null) {
                    result.add(qname);
                    PsiClass topLevelClass = PsiUtil.getTopLevelClass(element);
                    if (topLevelClass != null) {
                      String topName = topLevelClass.getQualifiedName();
                      assert topName != null;
                      result.add(topName + qname.substring(topName.length()).replace('.', '$'));
                    }
                  }
                } else if (element instanceof PsiMethod) {
                  ContainerUtil.addIfNotNull(result, ((PsiMethod) element).getName());
                } else if (element instanceof PsiVariable) {
                  ContainerUtil.addIfNotNull(result, ((PsiVariable) element).getName());
                } else if (element instanceof PsiMetaOwner) {
                  final PsiMetaData metaData = ((PsiMetaOwner) element).getMetaData();
                  if (metaData != null) {
                    ContainerUtil.addIfNotNull(result, metaData.getName());
                  }
                } else if (element instanceof PsiNamedElement) {
                  ContainerUtil.addIfNotNull(result, ((PsiNamedElement) element).getName());
                } else if (element instanceof XmlAttributeValue) {
                  ContainerUtil.addIfNotNull(result, ((XmlAttributeValue) element).getValue());
                } else {
                  LOG.error("Unknown element type: " + element);
                }
              }
            });

    return result;
  }
  @NotNull
  public static SearchScope getClassUseScope(@NotNull PsiClass aClass) {
    if (aClass instanceof PsiAnonymousClass) {
      return new LocalSearchScope(aClass);
    }
    final GlobalSearchScope maximalUseScope = ResolveScopeManager.getElementUseScope(aClass);
    PsiFile file = aClass.getContainingFile();
    if (PsiImplUtil.isInServerPage(file)) return maximalUseScope;
    final PsiClass containingClass = aClass.getContainingClass();
    if (aClass.hasModifierProperty(PsiModifier.PUBLIC)
        || aClass.hasModifierProperty(PsiModifier.PROTECTED)) {
      return containingClass == null ? maximalUseScope : containingClass.getUseScope();
    } else if (aClass.hasModifierProperty(PsiModifier.PRIVATE)
        || aClass instanceof PsiTypeParameter) {
      PsiClass topClass = PsiUtil.getTopLevelClass(aClass);
      return new LocalSearchScope(topClass == null ? aClass.getContainingFile() : topClass);
    } else {
      PsiPackage aPackage = null;
      if (file instanceof PsiJavaFile) {
        aPackage =
            JavaPsiFacade.getInstance(aClass.getProject())
                .findPackage(((PsiJavaFile) file).getPackageName());
      }

      if (aPackage == null) {
        PsiDirectory dir = file.getContainingDirectory();
        if (dir != null) {
          aPackage = JavaDirectoryService.getInstance().getPackage(dir);
        }
      }

      if (aPackage != null) {
        SearchScope scope = PackageScope.packageScope(aPackage, false);
        scope = scope.intersectWith(maximalUseScope);
        return scope;
      }

      return new LocalSearchScope(file);
    }
  }
  static boolean isSideEffectFreeConstructor(PsiNewExpression newExpression) {
    PsiJavaCodeReferenceElement classReference = newExpression.getClassReference();
    PsiClass aClass = classReference == null ? null : (PsiClass) classReference.resolve();
    String qualifiedName = aClass == null ? null : aClass.getQualifiedName();
    if (qualifiedName == null) return false;
    if (ourSideEffectFreeClasses.contains(qualifiedName)) return true;

    PsiFile file = aClass.getContainingFile();
    PsiDirectory directory = file.getContainingDirectory();
    PsiPackage classPackage = JavaDirectoryService.getInstance().getPackage(directory);
    String packageName = classPackage == null ? null : classPackage.getQualifiedName();

    // all Throwable descendants from java.lang are side effects free
    if ("java.lang".equals(packageName) || "java.io".equals(packageName)) {
      PsiClass throwableClass =
          JavaPsiFacade.getInstance(aClass.getProject())
              .findClass("java.lang.Throwable", aClass.getResolveScope());
      if (throwableClass != null
          && InheritanceUtil.isInheritorOrSelf(aClass, throwableClass, true)) {
        return true;
      }
    }
    return false;
  }