@Nullable
  private String verifyInnerClassDestination() {
    PsiClass targetClass = findTargetClass();
    if (targetClass == null) return null;

    for (PsiElement element : myElementsToMove) {
      if (PsiTreeUtil.isAncestor(element, targetClass, false)) {
        return RefactoringBundle.message("move.class.to.inner.move.to.self.error");
      }
      final Language targetClassLanguage = targetClass.getLanguage();
      if (!element.getLanguage().equals(targetClassLanguage)) {
        return RefactoringBundle.message(
            "move.to.different.language",
            UsageViewUtil.getType(element),
            ((PsiClass) element).getQualifiedName(),
            targetClass.getQualifiedName());
      }
      if (element.getLanguage().equals(Language.findLanguageByID("Groovy"))) {
        return RefactoringBundle.message("dont.support.inner.classes", "Groovy");
      }
    }

    while (targetClass != null) {
      if (targetClass.getContainingClass() != null
          && !targetClass.hasModifierProperty(PsiModifier.STATIC)) {
        return RefactoringBundle.message("move.class.to.inner.nonstatic.error");
      }
      targetClass = targetClass.getContainingClass();
    }

    return null;
  }
  @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);
    }
  }
 @Override
 public void visitClass(@NotNull PsiClass aClass) {
   // no call to super, so that it doesn't drill down to inner classes
   if (aClass.hasModifierProperty(PsiModifier.ABSTRACT)) {
     return;
   }
   if (!UtilityClassUtil.isUtilityClass(aClass)) {
     return;
   }
   if (ignoreClassesWithOnlyMain && hasOnlyMain(aClass)) {
     return;
   }
   if (hasPrivateConstructor(aClass)) {
     return;
   }
   final SearchScope scope = GlobalSearchScope.projectScope(aClass.getProject());
   final Query<PsiClass> query = ClassInheritorsSearch.search(aClass, scope, true, true);
   final PsiClass subclass = query.findFirst();
   if (subclass != null) {
     return;
   }
   registerClassError(aClass, aClass);
 }
    private void checkExpression(PsiExpression expression) {
      if (expression == null) return;

      final PsiType type = expression.getType();
      if (!(type instanceof PsiClassType)) return;

      if (IGNORE_TOSTRING
          && MethodUtils.isToString(PsiTreeUtil.getParentOfType(expression, PsiMethod.class)))
        return;

      if (IGNORE_EXCEPTION
          && (ExceptionUtils.isExceptionArgument(expression)
              || PsiTreeUtil.getParentOfType(
                      expression, PsiThrowStatement.class, true, PsiCodeBlock.class, PsiClass.class)
                  != null)) return;

      if (IGNORE_ASSERT
          && PsiTreeUtil.getParentOfType(
                  expression, PsiAssertStatement.class, true, PsiCodeBlock.class, PsiClass.class)
              != null) {
        return;
      }

      if (IGNORE_NONNLS && NonNlsUtils.isNonNlsAnnotatedUse(expression)) return;

      final PsiClassType classType = (PsiClassType) type;
      if (type.equalsToText(CommonClassNames.JAVA_LANG_OBJECT)) return;

      final PsiClass referencedClass = classType.resolve();
      if (referencedClass == null || referencedClass instanceof PsiTypeParameter) return;
      if (referencedClass.isEnum() || referencedClass.isInterface()) return;
      if (referencedClass.hasModifierProperty(PsiModifier.ABSTRACT)
          && !(expression instanceof PsiSuperExpression)) return;
      if (hasGoodToString(referencedClass)) return;

      registerError(expression);
    }
Ejemplo n.º 5
0
  public boolean runMethodInAbstractClass(
      final ConfigurationContext context,
      final Runnable performRunnable,
      final PsiMethod psiMethod,
      final PsiClass containingClass,
      final Condition<PsiClass> acceptAbstractCondition) {
    if (containingClass != null
        && containingClass.hasModifierProperty(PsiModifier.ABSTRACT)
        && acceptAbstractCondition.value(containingClass)) {
      final Location location = context.getLocation();
      if (location instanceof MethodLocation) {
        final PsiClass aClass = ((MethodLocation) location).getContainingClass();
        if (aClass != null && !aClass.hasModifierProperty(PsiModifier.ABSTRACT)) {
          return false;
        }
      } else if (location instanceof PsiMemberParameterizedLocation) {
        return false;
      }

      final List<PsiClass> classes = new ArrayList<PsiClass>();
      if (!ProgressManager.getInstance()
          .runProcessWithProgressSynchronously(
              new Runnable() {
                @Override
                public void run() {
                  ClassInheritorsSearch.search(containingClass)
                      .forEach(
                          new Processor<PsiClass>() {
                            @Override
                            public boolean process(PsiClass aClass) {
                              if (PsiClassUtil.isRunnableClass(aClass, true, true)) {
                                classes.add(aClass);
                              }
                              return true;
                            }
                          });
                }
              },
              "Search for " + containingClass.getQualifiedName() + " inheritors",
              true,
              containingClass.getProject())) {
        return true;
      }

      if (classes.size() == 1) {
        runForClass(classes.get(0), psiMethod, context, performRunnable);
        return true;
      }
      if (classes.isEmpty()) return false;
      final FileEditor fileEditor = PlatformDataKeys.FILE_EDITOR.getData(context.getDataContext());
      if (fileEditor instanceof TextEditor) {
        final Document document = ((TextEditor) fileEditor).getEditor().getDocument();
        final PsiFile containingFile =
            PsiDocumentManager.getInstance(context.getProject()).getPsiFile(document);
        if (containingFile instanceof PsiClassOwner) {
          final List<PsiClass> psiClasses =
              new ArrayList<PsiClass>(Arrays.asList(((PsiClassOwner) containingFile).getClasses()));
          psiClasses.retainAll(classes);
          if (psiClasses.size() == 1) {
            runForClass(psiClasses.get(0), psiMethod, context, performRunnable);
            return true;
          }
        }
      }
      final PsiClassListCellRenderer renderer =
          new PsiClassListCellRenderer() {
            @Override
            protected boolean customizeNonPsiElementLeftRenderer(
                ColoredListCellRenderer renderer,
                JList list,
                Object value,
                int index,
                boolean selected,
                boolean hasFocus) {
              if (value == null) {
                renderer.append("All");
                return true;
              }
              return super.customizeNonPsiElementLeftRenderer(
                  renderer, list, value, index, selected, hasFocus);
            }
          };
      Collections.sort(classes, renderer.getComparator());

      // suggest to run all inherited tests
      classes.add(0, null);
      final JBList list = new JBList(classes);
      list.setCellRenderer(renderer);
      JBPopupFactory.getInstance()
          .createListPopupBuilder(list)
          .setTitle(
              "Choose executable classes to run "
                  + (psiMethod != null ? psiMethod.getName() : containingClass.getName()))
          .setMovable(false)
          .setResizable(false)
          .setRequestFocus(true)
          .setItemChoosenCallback(
              new Runnable() {
                public void run() {
                  final Object[] values = list.getSelectedValues();
                  if (values == null) return;
                  chooseAndPerform(values, psiMethod, context, performRunnable, classes);
                }
              })
          .createPopup()
          .showInBestPositionFor(context.getDataContext());
      return true;
    }
    return false;
  }