private static PsiDirectory[] suggestMostAppropriateDirectories(PsiPackage psiPackage) {
    final Project project = psiPackage.getProject();
    PsiDirectory[] directories = null;
    final Editor editor = FileEditorManager.getInstance(project).getSelectedTextEditor();
    if (editor != null) {
      final Document document = editor.getDocument();
      final PsiFile psiFile = PsiDocumentManager.getInstance(project).getPsiFile(document);
      if (psiFile != null) {
        final Module module = ModuleUtil.findModuleForPsiElement(psiFile);
        if (module != null) {
          directories =
              psiPackage.getDirectories(GlobalSearchScope.moduleWithDependenciesScope(module));
        } else {
          directories =
              psiPackage.getDirectories(
                  GlobalSearchScope.notScope(GlobalSearchScope.projectScope(project)));
        }
      }
    }

    if (directories == null || directories.length == 0) {
      directories = psiPackage.getDirectories();
    }
    return directories;
  }
 @NotNull
 private DefaultMutableTreeNode addPackage(PsiPackage aPackage) {
   final String qualifiedPackageName = aPackage.getQualifiedName();
   final PsiPackage parentPackage = aPackage.getParentPackage();
   if (parentPackage == null) {
     final DefaultMutableTreeNode rootNode = (DefaultMutableTreeNode) myModel.getRoot();
     if (qualifiedPackageName.length() == 0) {
       rootNode.setUserObject(aPackage);
       return rootNode;
     } else {
       DefaultMutableTreeNode packageNode = findPackageNode(rootNode, qualifiedPackageName);
       if (packageNode != null) return packageNode;
       packageNode = new DefaultMutableTreeNode(aPackage);
       rootNode.add(packageNode);
       return packageNode;
     }
   } else {
     final DefaultMutableTreeNode parentNode = addPackage(parentPackage);
     DefaultMutableTreeNode packageNode = findPackageNode(parentNode, qualifiedPackageName);
     if (packageNode != null) {
       return packageNode;
     }
     packageNode = new DefaultMutableTreeNode(aPackage);
     parentNode.add(packageNode);
     return packageNode;
   }
 }
  protected void performRefactoring(
      Project project,
      PsiDirectory directory,
      PsiPackage aPackage,
      boolean searchInComments,
      boolean searchForTextOccurences) {
    final VirtualFile sourceRoot =
        ProjectRootManager.getInstance(project)
            .getFileIndex()
            .getSourceRootForFile(directory.getVirtualFile());
    if (sourceRoot == null) {
      Messages.showErrorDialog(
          project,
          RefactoringBundle.message("destination.directory.does.not.correspond.to.any.package"),
          RefactoringBundle.message("cannot.move"));
      return;
    }
    final JavaRefactoringFactory factory = JavaRefactoringFactory.getInstance(project);
    final MoveDestination destination =
        myPreserveSourceRoot.isSelected() && myPreserveSourceRoot.isVisible()
            ? factory.createSourceFolderPreservingMoveDestination(aPackage.getQualifiedName())
            : factory.createSourceRootMoveDestination(aPackage.getQualifiedName(), sourceRoot);

    MoveClassesOrPackagesProcessor processor =
        new MoveClassesOrPackagesProcessor(
            myDirectory.getProject(),
            myElementsToMove,
            destination,
            searchInComments,
            searchForTextOccurences,
            myMoveCallback);
    if (processor.verifyValidPackageName()) {
      processor.run();
    }
  }
Beispiel #4
0
  public static boolean processImplicitImports(
      @NotNull PsiScopeProcessor processor,
      ResolveState state,
      PsiElement lastParent,
      PsiElement place,
      @NotNull GroovyFile file) {
    JavaPsiFacade facade = JavaPsiFacade.getInstance(file.getProject());

    final DelegatingScopeProcessor packageSkipper =
        new DelegatingScopeProcessor(processor) {
          @Override
          public boolean execute(@NotNull PsiElement element, ResolveState state) {
            if (element instanceof PsiPackage) return true;
            return super.execute(element, state);
          }
        };

    for (final String implicitlyImported : getImplicitlyImportedPackages(file)) {
      PsiPackage aPackage = facade.findPackage(implicitlyImported);
      if (aPackage == null) continue;

      if (!aPackage.processDeclarations(packageSkipper, state, lastParent, place)) {
        return false;
      }
    }

    GroovyPsiManager groovyPsiManager = GroovyPsiManager.getInstance(file.getProject());
    for (String implicitlyImportedClass : GroovyFileBase.IMPLICITLY_IMPORTED_CLASSES) {
      PsiClass clazz =
          groovyPsiManager.findClassWithCache(implicitlyImportedClass, file.getResolveScope());
      if (clazz != null && !ResolveUtil.processElement(processor, clazz, state)) return false;
    }
    return true;
  }
  public PsiElement bindToElement(@NotNull PsiElement element) throws IncorrectOperationException {
    if (isReferenceTo(element)) return getElement();

    final String newName;
    if (element instanceof PsiClass) {
      PsiClass psiClass = (PsiClass) element;
      final boolean jvmFormat =
          Boolean.TRUE.equals(JavaClassReferenceProvider.JVM_FORMAT.getValue(getOptions()));
      newName = jvmFormat ? ClassUtil.getJVMClassName(psiClass) : psiClass.getQualifiedName();
    } else if (element instanceof PsiPackage) {
      PsiPackage psiPackage = (PsiPackage) element;
      newName = psiPackage.getQualifiedName();
    } else {
      throw new IncorrectOperationException("Cannot bind to " + element);
    }
    assert newName != null;

    TextRange range =
        new TextRange(
            myJavaClassReferenceSet.getReference(0).getRangeInElement().getStartOffset(),
            getRangeInElement().getEndOffset());
    final ElementManipulator<PsiElement> manipulator = getManipulator(getElement());
    if (manipulator != null) {
      final PsiElement finalElement = manipulator.handleContentChange(getElement(), range, newName);
      range = new TextRange(range.getStartOffset(), range.getStartOffset() + newName.length());
      myJavaClassReferenceSet.reparse(finalElement, range);
      return finalElement;
    }
    return element;
  }
 @Override
 public boolean value(PsiPackage aPackage) {
   return JavaPsiFacade.getInstance(aPackage.getProject())
           .getNameHelper()
           .isQualifiedName(aPackage.getQualifiedName())
       && Character.isLowerCase(aPackage.getName().charAt(0));
 }
Beispiel #7
0
 @Nullable
 protected String getContainerText(final PsiElement element, final String name) {
   final PsiDirectory parent = ((PsiFile) element).getParent();
   if (parent == null) return null;
   final PsiPackage psiPackage = JavaDirectoryService.getInstance().getPackage(parent);
   if (psiPackage == null) return null;
   return "(" + psiPackage.getQualifiedName() + ")";
 }
 public static PsiDirectory[] getIntentionDescriptionsDirs(Module module) {
   final PsiPackage aPackage =
       JavaPsiFacade.getInstance(module.getProject()).findPackage(INSPECTION_DESCRIPTIONS);
   if (aPackage != null) {
     return aPackage.getDirectories(GlobalSearchScope.moduleWithDependenciesScope(module));
   } else {
     return PsiDirectory.EMPTY_ARRAY;
   }
 }
  @Override
  public boolean processDeclarations(
      @NotNull PsiScopeProcessor processor,
      @NotNull ResolveState state,
      PsiElement lastParent,
      @NotNull PsiElement place) {
    GlobalSearchScope scope = place.getResolveScope();

    processor.handleEvent(PsiScopeProcessor.Event.SET_DECLARATION_HOLDER, this);
    ElementClassHint classHint = processor.getHint(ElementClassHint.KEY);

    final JavaPsiFacade facade = getFacade();
    final Condition<String> prefixMatcher = processor.getHint(JavaCompletionHints.NAME_FILTER);

    if (classHint == null || classHint.shouldProcess(ElementClassHint.DeclarationKind.CLASS)) {
      NameHint nameHint = processor.getHint(NameHint.KEY);
      if (nameHint != null) {
        final String shortName = nameHint.getName(state);
        if (containsClassNamed(shortName)
            && processClassesByName(processor, state, scope, shortName)) return false;
      } else if (prefixMatcher != null) {
        for (String className : getClassNamesCache()) {
          if (prefixMatcher.value(className)) {
            if (processClassesByName(processor, state, scope, className)) return false;
          }
        }
      } else {
        PsiClass[] classes = getClasses(scope);
        if (!processClasses(processor, state, classes)) return false;
      }
    }
    if (classHint == null || classHint.shouldProcess(ElementClassHint.DeclarationKind.PACKAGE)) {
      NameHint nameHint = processor.getHint(NameHint.KEY);
      if (nameHint != null) {
        PsiPackage aPackage = findSubPackageByName(nameHint.getName(state));
        if (aPackage != null) {
          if (!processor.execute(aPackage, state)) return false;
        }
      } else {
        PsiPackage[] packs = getSubPackages(scope);
        for (PsiPackage pack : packs) {
          final String packageName = pack.getName();
          if (packageName == null) continue;
          if (!facade.getNameHelper().isIdentifier(packageName, PsiUtil.getLanguageLevel(this))) {
            continue;
          }
          if (!processor.execute(pack, state)) {
            return false;
          }
        }
      }
    }
    return true;
  }
  @Override
  @NotNull
  public Object[] getVariants() {
    Set<PsiPackage> subPackages = new HashSet<>();
    for (PsiPackage psiPackage : getContext()) {
      subPackages.addAll(
          Arrays.asList(psiPackage.getSubPackages(myReferenceSet.getResolveScope())));
    }

    return subPackages.toArray();
  }
 @Nullable
 private static DefaultMutableTreeNode findPackageNode(
     DefaultMutableTreeNode rootNode, String qualifiedName) {
   for (int i = 0; i < rootNode.getChildCount(); i++) {
     final DefaultMutableTreeNode child = (DefaultMutableTreeNode) rootNode.getChildAt(i);
     final PsiPackage nodePackage = (PsiPackage) child.getUserObject();
     if (nodePackage != null) {
       if (Comparing.equal(nodePackage.getQualifiedName(), qualifiedName)) return child;
     }
   }
   return null;
 }
Beispiel #12
0
  @Override
  public boolean processDeclarations(
      @NotNull PsiScopeProcessor processor,
      @NotNull ResolveState state,
      PsiElement lastParent,
      @NotNull PsiElement place) {
    GlobalSearchScope scope = place.getResolveScope();

    processor.handleEvent(PsiScopeProcessor.Event.SET_DECLARATION_HOLDER, this);
    ElementClassHint classHint = processor.getHint(ElementClassHint.KEY);

    final Condition<String> nameCondition = processor.getHint(JavaCompletionHints.NAME_FILTER);

    if (classHint == null || classHint.shouldProcess(ElementClassHint.DeclarationKind.CLASS)) {
      NameHint nameHint = processor.getHint(NameHint.KEY);
      if (nameHint != null) {
        final String shortName = nameHint.getName(state);
        final PsiClass[] classes = findClassByShortName(shortName, scope);
        if (!processClasses(processor, state, classes, Conditions.<String>alwaysTrue()))
          return false;
      } else {
        PsiClass[] classes = getClasses(scope);
        if (!processClasses(
            processor,
            state,
            classes,
            nameCondition != null ? nameCondition : Conditions.<String>alwaysTrue())) return false;
      }
    }
    if (classHint == null || classHint.shouldProcess(ElementClassHint.DeclarationKind.PACKAGE)) {
      NameHint nameHint = processor.getHint(NameHint.KEY);
      if (nameHint != null) {
        PsiPackage aPackage = findSubPackageByName(nameHint.getName(state));
        if (aPackage != null) {
          if (!processor.execute(aPackage, state)) return false;
        }
      } else {
        PsiPackage[] packs = getSubPackages(scope);
        for (PsiPackage pack : packs) {
          final String packageName = pack.getName();
          if (packageName == null) continue;
          if (!PsiNameHelper.getInstance(myManager.getProject())
              .isIdentifier(packageName, PsiUtil.getLanguageLevel(this))) {
            continue;
          }
          if (!processor.execute(pack, state)) {
            return false;
          }
        }
      }
    }
    return true;
  }
 @Override
 protected void notifyByBalloon(
     JUnitRunningModel model, boolean started, final JUnitConsoleProperties consoleProperties) {
   if (myFoundTests) {
     super.notifyByBalloon(model, started, consoleProperties);
   } else {
     final String packageName = myConfiguration.getPackage();
     if (packageName == null) return;
     final Project project = myConfiguration.getProject();
     final PsiPackage aPackage = JavaPsiFacade.getInstance(project).findPackage(packageName);
     if (aPackage == null) return;
     final Module module = myConfiguration.getConfigurationModule().getModule();
     if (module == null) return;
     final Set<Module> modulesWithPackage = new HashSet<Module>();
     final PsiDirectory[] directories = aPackage.getDirectories();
     for (PsiDirectory directory : directories) {
       final Module currentModule =
           ModuleUtil.findModuleForFile(directory.getVirtualFile(), project);
       if (module != currentModule && currentModule != null) {
         modulesWithPackage.add(currentModule);
       }
     }
     if (!modulesWithPackage.isEmpty()) {
       final String testRunDebugId =
           consoleProperties.isDebug() ? ToolWindowId.DEBUG : ToolWindowId.RUN;
       final ToolWindowManager toolWindowManager = ToolWindowManager.getInstance(project);
       final Function<Module, String> moduleNameRef =
           new Function<Module, String>() {
             @Override
             public String fun(Module module) {
               final String moduleName = module.getName();
               return "<a href=\"" + moduleName + "\">" + moduleName + "</a>";
             }
           };
       String message = "Tests were not found in module \"" + module.getName() + "\".\n" + "Use ";
       if (modulesWithPackage.size() == 1) {
         message += "module \"" + moduleNameRef.fun(modulesWithPackage.iterator().next()) + "\" ";
       } else {
         message += "one of\n" + StringUtil.join(modulesWithPackage, moduleNameRef, "\n") + "\n";
       }
       message += "instead";
       toolWindowManager.notifyByBalloon(
           testRunDebugId,
           MessageType.WARNING,
           message,
           null,
           new ResetConfigurationModuleAdapter(
               project, consoleProperties, toolWindowManager, testRunDebugId));
     }
   }
 }
Beispiel #14
0
 public static boolean hasJavaLangImportConflict(String fqName, PsiJavaFile file) {
   final String shortName = ClassUtil.extractClassName(fqName);
   final String packageName = ClassUtil.extractPackageName(fqName);
   if (HardcodedMethodConstants.JAVA_LANG.equals(packageName)) {
     return false;
   }
   final Project project = file.getProject();
   final JavaPsiFacade psiFacade = JavaPsiFacade.getInstance(project);
   final PsiPackage javaLangPackage = psiFacade.findPackage(HardcodedMethodConstants.JAVA_LANG);
   if (javaLangPackage == null) {
     return false;
   }
   return javaLangPackage.containsClassNamed(shortName);
 }
Beispiel #15
0
 public static boolean hasDefaultImportConflict(String fqName, PsiJavaFile file) {
   final String shortName = ClassUtil.extractClassName(fqName);
   final String packageName = ClassUtil.extractPackageName(fqName);
   final String filePackageName = file.getPackageName();
   if (filePackageName.equals(packageName)) {
     return false;
   }
   final Project project = file.getProject();
   final JavaPsiFacade psiFacade = JavaPsiFacade.getInstance(project);
   final PsiPackage filePackage = psiFacade.findPackage(filePackageName);
   if (filePackage == null) {
     return false;
   }
   return filePackage.containsClassNamed(shortName);
 }
 private DefaultMutableTreeNode findNodeForPackage(String qualifiedPackageName) {
   DefaultMutableTreeNode root = (DefaultMutableTreeNode) myModel.getRoot();
   Enumeration enumeration = root.depthFirstEnumeration();
   while (enumeration.hasMoreElements()) {
     Object o = enumeration.nextElement();
     if (o instanceof DefaultMutableTreeNode) {
       DefaultMutableTreeNode node = (DefaultMutableTreeNode) o;
       PsiPackage nodePackage = (PsiPackage) node.getUserObject();
       if (nodePackage != null) {
         if (Comparing.equal(nodePackage.getQualifiedName(), qualifiedPackageName)) return node;
       }
     }
   }
   return null;
 }
  public static void checkCreateClassOrInterface(@NotNull PsiDirectory directory, String name)
      throws IncorrectOperationException {
    PsiUtil.checkIsIdentifier(directory.getManager(), name);

    String fileName = name + "." + StdFileTypes.JAVA.getDefaultExtension();
    directory.checkCreateFile(fileName);

    PsiNameHelper helper = JavaPsiFacade.getInstance(directory.getProject()).getNameHelper();
    PsiPackage aPackage = JavaDirectoryService.getInstance().getPackage(directory);
    String qualifiedName = aPackage == null ? null : aPackage.getQualifiedName();
    if (!StringUtil.isEmpty(qualifiedName) && !helper.isQualifiedName(qualifiedName)) {
      throw new IncorrectOperationException(
          "Cannot create class in invalid package: '" + qualifiedName + "'");
    }
  }
 @Nullable
 @Override
 public CommonProblemDescriptor[] checkElement(
     @NotNull RefEntity refEntity,
     @NotNull AnalysisScope scope,
     @NotNull InspectionManager manager,
     @NotNull GlobalInspectionContext globalContext) {
   if (!(refEntity instanceof RefPackage)) {
     return null;
   }
   final RefPackage refPackage = (RefPackage) refEntity;
   final String packageName = refPackage.getQualifiedName();
   final Project project = globalContext.getProject();
   final PsiPackage aPackage = JavaPsiFacade.getInstance(project).findPackage(packageName);
   if (aPackage == null) {
     return null;
   }
   final List<RefEntity> children = refPackage.getChildren();
   boolean hasClasses = false;
   for (RefEntity child : children) {
     if (child instanceof RefClass) {
       hasClasses = true;
       break;
     }
   }
   if (!hasClasses) {
     return null;
   }
   final PsiDirectory[] directories = aPackage.getDirectories();
   for (PsiDirectory directory : directories) {
     final boolean packageInfoFound = directory.findFile(PsiPackage.PACKAGE_INFO_FILE) != null;
     final boolean packageDotHtmlFound = directory.findFile("package.html") != null;
     if (packageInfoFound || packageDotHtmlFound) {
       return null;
     }
   }
   if (PsiUtil.isLanguageLevel5OrHigher(aPackage)) {
     return new CommonProblemDescriptor[] {
       manager.createProblemDescriptor(
           InspectionGadgetsBundle.message("missing.package.info.problem.descriptor", packageName))
     };
   } else {
     return new CommonProblemDescriptor[] {
       manager.createProblemDescriptor(
           InspectionGadgetsBundle.message("missing.package.html.problem.descriptor", packageName))
     };
   }
 }
Beispiel #19
0
  @Override
  public boolean canFindUsagesFor(@NotNull PsiElement element) {
    if (element instanceof PsiDirectory) {
      PsiPackage psiPackage = JavaDirectoryService.getInstance().getPackage((PsiDirectory) element);
      return psiPackage != null && psiPackage.getQualifiedName().length() != 0;
    }

    return element instanceof PsiClass
        || element instanceof PsiVariable
        || element instanceof PsiMethod
        || element instanceof PsiPackage
        || element instanceof PsiJavaModule
        || element instanceof PsiLabeledStatement
        || ThrowSearchUtil.isSearchable(element)
        || element instanceof PsiMetaOwner && ((PsiMetaOwner) element).getMetaData() != null;
  }
Beispiel #20
0
 @Nullable
 public static CreateClassOrPackageFix createFix(
     @NotNull final String qualifiedName,
     @NotNull final GlobalSearchScope scope,
     @NotNull final PsiElement context,
     @Nullable final PsiPackage basePackage,
     @Nullable ClassKind kind,
     @Nullable String superClass,
     @Nullable String templateName) {
   final List<PsiDirectory> directories =
       getWritableDirectoryListDefault(basePackage, scope, context.getManager());
   if (directories.isEmpty()) {
     return null;
   }
   final String redPart =
       basePackage == null
           ? qualifiedName
           : qualifiedName.substring(basePackage.getQualifiedName().length() + 1);
   final int dot = redPart.indexOf('.');
   final boolean fixPath = dot >= 0;
   final String firstRedName = fixPath ? redPart.substring(0, dot) : redPart;
   for (Iterator<PsiDirectory> i = directories.iterator(); i.hasNext(); ) {
     if (!checkCreateClassOrPackage(kind != null && !fixPath, i.next(), firstRedName)) {
       i.remove();
     }
   }
   return new CreateClassOrPackageFix(
       directories,
       context,
       fixPath ? qualifiedName : redPart,
       redPart,
       kind,
       superClass,
       templateName);
 }
 private static void addClassesInPackage(
     PsiPackage aPackage, boolean includeSubpackages, ArrayList<PsiClass> array) {
   PsiDirectory[] dirs = aPackage.getDirectories();
   for (PsiDirectory dir : dirs) {
     addClassesInDirectory(dir, includeSubpackages, array);
   }
 }
 @Override
 public Object[] getDirectoryCachedValueDependencies(PsiPackage psiPackage) {
   return new Object[] {
     PsiModificationTracker.OUT_OF_CODE_BLOCK_MODIFICATION_COUNT,
     ProjectRootManager.getInstance(psiPackage.getProject())
   };
 }
 public String getText() {
   if (myPackageName != null) {
     return myPackageName;
   } else {
     return myRefPackage.getQualifiedName();
   }
 }
 public String getReferenceName() {
   if (myPackageName != null) {
     return PsiNameHelper.getShortClassName(myPackageName);
   } else {
     return myRefPackage.getName();
   }
 }
 @PsiUtil.AccessLevel
 private static int getEffectiveLevel(
     @NotNull PsiElement element,
     @NotNull PsiFile file,
     @NotNull PsiFile memberFile,
     PsiClass memberClass,
     PsiPackage memberPackage) {
   PsiClass aClass = PsiTreeUtil.getParentOfType(element, PsiClass.class);
   if (memberClass != null && PsiTreeUtil.isAncestor(aClass, memberClass, false)
       || aClass != null && PsiTreeUtil.isAncestor(memberClass, aClass, false)) {
     // access from the same file can be via private
     // except when used in annotation:
     // @Ann(value = C.VAL) class C { public static final String VAL = "xx"; }
     PsiAnnotation annotation = PsiTreeUtil.getParentOfType(element, PsiAnnotation.class);
     if (annotation != null
         && annotation.getParent() instanceof PsiModifierList
         && annotation.getParent().getParent() == aClass) {
       return PsiUtil.ACCESS_LEVEL_PACKAGE_LOCAL;
     }
     return PsiUtil.ACCESS_LEVEL_PRIVATE;
   }
   // if (file == memberFile) {
   //  return PsiUtil.ACCESS_LEVEL_PACKAGE_LOCAL;
   // }
   PsiDirectory directory = file.getContainingDirectory();
   PsiPackage aPackage =
       directory == null ? null : JavaDirectoryService.getInstance().getPackage(directory);
   if (aPackage == memberPackage
       || aPackage != null
           && memberPackage != null
           && Comparing.strEqual(
               aPackage.getQualifiedName(), memberPackage.getQualifiedName())) {
     return PsiUtil.ACCESS_LEVEL_PACKAGE_LOCAL;
   }
   if (aClass != null && memberClass != null && aClass.isInheritor(memberClass, true)) {
     // access from subclass can be via protected, except for constructors
     PsiElement resolved =
         element instanceof PsiReference ? ((PsiReference) element).resolve() : null;
     boolean isConstructor =
         resolved instanceof PsiClass && element.getParent() instanceof PsiNewExpression
             || resolved instanceof PsiMethod && ((PsiMethod) resolved).isConstructor();
     if (!isConstructor) {
       return PsiUtil.ACCESS_LEVEL_PROTECTED;
     }
   }
   return PsiUtil.ACCESS_LEVEL_PUBLIC;
 }
  private boolean processDeclarationsForMultipleElements(
      @NotNull final PsiScopeProcessor processor,
      @Nullable PsiElement lastParent,
      @NotNull PsiElement place,
      @NotNull ResolveState state) {
    GrCodeReferenceElement ref = getImportReference();
    if (ref == null) return true;

    if (isStatic()) {
      final PsiElement resolved = ref.resolve();
      if (resolved instanceof PsiClass) {
        state = state.put(ClassHint.RESOLVE_CONTEXT, this);
        final PsiClass clazz = (PsiClass) resolved;
        for (final PsiScopeProcessor each : GroovyResolverProcessor.allProcessors(processor)) {
          if (!clazz.processDeclarations(
              new DelegatingScopeProcessor(each) {
                @Override
                public boolean execute(@NotNull PsiElement element, @NotNull ResolveState state) {
                  if (element instanceof PsiMember
                      && ((PsiMember) element).hasModifierProperty(PsiModifier.STATIC)) {
                    return super.execute(element, state);
                  }
                  return true;
                }
              },
              state,
              lastParent,
              place)) return false;
        }
      }
    } else {
      if (ResolveUtil.shouldProcessClasses(processor.getHint(ElementClassHint.KEY))) {
        String qName = PsiUtil.getQualifiedReferenceText(ref);
        if (qName != null) {
          PsiPackage aPackage = JavaPsiFacade.getInstance(getProject()).findPackage(qName);
          if (aPackage != null
              && !((GroovyFile) getContainingFile())
                  .getPackageName()
                  .equals(aPackage.getQualifiedName())) {
            state = state.put(ClassHint.RESOLVE_CONTEXT, this);
            if (!aPackage.processDeclarations(processor, state, lastParent, place)) return false;
          }
        }
      }
    }
    return true;
  }
  private Object[] processPackage(final PsiPackage aPackage) {
    final ArrayList<Object> list = new ArrayList<Object>();
    final int startOffset =
        StringUtil.isEmpty(aPackage.getName()) ? 0 : aPackage.getQualifiedName().length() + 1;
    final GlobalSearchScope scope = getScope();
    for (final PsiPackage subPackage : aPackage.getSubPackages(scope)) {
      final String shortName = subPackage.getQualifiedName().substring(startOffset);
      if (JavaPsiFacade.getInstance(subPackage.getProject())
          .getNameHelper()
          .isIdentifier(shortName)) {
        list.add(subPackage);
      }
    }

    final PsiClass[] classes = aPackage.getClasses(scope);
    final Map<CustomizableReferenceProvider.CustomizationKey, Object> options = getOptions();
    if (options != null) {
      final boolean instantiatable =
          JavaClassReferenceProvider.INSTANTIATABLE.getBooleanValue(options);
      final boolean concrete = JavaClassReferenceProvider.CONCRETE.getBooleanValue(options);
      final boolean notInterface =
          JavaClassReferenceProvider.NOT_INTERFACE.getBooleanValue(options);
      final boolean notEnum = JavaClassReferenceProvider.NOT_ENUM.getBooleanValue(options);
      final ClassKind classKind = getClassKind();

      for (PsiClass clazz : classes) {
        if (isClassAccepted(clazz, classKind, instantiatable, concrete, notInterface, notEnum)) {
          list.add(clazz);
        }
      }
    } else {
      ContainerUtil.addAll(list, classes);
    }
    return list.toArray();
  }
 private static boolean isWritablePackage(PsiPackage aPackage) {
   PsiDirectory[] directories = aPackage.getDirectories();
   for (PsiDirectory directory : directories) {
     if (directory.isValid() && directory.isWritable()) {
       return true;
     }
   }
   return false;
 }
 public boolean isConfiguredByElement(
     final JUnitConfiguration configuration,
     PsiClass testClass,
     PsiMethod testMethod,
     PsiPackage testPackage) {
   return testPackage != null
       && Comparing.equal(
           testPackage.getQualifiedName(), configuration.getPersistentData().getPackageName());
 }
 @NotNull
 @Override
 public PsiReferenceExpression createReferenceExpression(@NotNull final PsiPackage aPackage)
     throws IncorrectOperationException {
   if (aPackage.getQualifiedName().isEmpty()) {
     throw new IncorrectOperationException("Cannot create reference to default package.");
   }
   return new LightPackageReferenceExpression(myManager, aPackage);
 }