示例#1
0
    @NotNull
    @Override
    public GroovyResolveResult[] getCandidates() {
      if (!hasCandidates()) return GroovyResolveResult.EMPTY_ARRAY;
      final GroovyResolveResult[] results =
          ResolveUtil.filterSameSignatureCandidates(getCandidatesInternal());
      List<GroovyResolveResult> list = new ArrayList<GroovyResolveResult>(results.length);
      myPropertyNames.removeAll(myPreferredFieldNames);

      Set<String> usedFields = ContainerUtil.newHashSet();
      for (GroovyResolveResult result : results) {
        final PsiElement element = result.getElement();
        if (element instanceof PsiField) {
          final String name = ((PsiField) element).getName();
          if (myPropertyNames.contains(name)
              || myLocalVars.contains(name)
              || usedFields.contains(name)) {
            continue;
          } else {
            usedFields.add(name);
          }
        }

        list.add(result);
      }
      return list.toArray(new GroovyResolveResult[list.size()]);
    }
 public ExtractClassProcessor(
     PsiClass sourceClass,
     List<PsiField> fields,
     List<PsiMethod> methods,
     List<PsiClass> classes,
     String packageName,
     MoveDestination moveDestination,
     String newClassName,
     String newVisibility,
     boolean generateAccessors,
     List<MemberInfo> enumConstants) {
   super(sourceClass.getProject());
   this.sourceClass = sourceClass;
   this.newPackageName = packageName;
   myMoveDestination = moveDestination;
   myNewVisibility = newVisibility;
   myGenerateAccessors = generateAccessors;
   this.enumConstants = new ArrayList<PsiField>();
   for (MemberInfo constant : enumConstants) {
     if (constant.isChecked()) {
       this.enumConstants.add((PsiField) constant.getMember());
     }
   }
   this.fields = new ArrayList<PsiField>(fields);
   this.methods = new ArrayList<PsiMethod>(methods);
   this.innerClasses = new ArrayList<PsiClass>(classes);
   this.newClassName = newClassName;
   delegateFieldName = calculateDelegateFieldName();
   requiresBackpointer =
       new BackpointerUsageVisitor(fields, innerClasses, methods, sourceClass)
           .backpointerRequired();
   if (requiresBackpointer) {
     ContainerUtil.addAll(typeParams, sourceClass.getTypeParameters());
   } else {
     final Set<PsiTypeParameter> typeParamSet = new HashSet<PsiTypeParameter>();
     final TypeParametersVisitor visitor = new TypeParametersVisitor(typeParamSet);
     for (PsiField field : fields) {
       field.accept(visitor);
     }
     for (PsiMethod method : methods) {
       method.accept(visitor);
       // do not include method's type parameters in class signature
       typeParamSet.removeAll(Arrays.asList(method.getTypeParameters()));
     }
     typeParams.addAll(typeParamSet);
   }
   myClass =
       new WriteCommandAction<PsiClass>(myProject, getCommandName()) {
         @Override
         protected void run(@NotNull Result<PsiClass> result) throws Throwable {
           result.setResult(buildClass());
         }
       }.execute().getResultObject();
   myExtractEnumProcessor = new ExtractEnumProcessor(myProject, this.enumConstants, myClass);
 }
示例#3
0
  @NotNull
  public static Set<JetElement> findRootExpressions(
      @NotNull Collection<JetElement> unreachableElements) {
    Set<JetElement> rootElements = new HashSet<JetElement>();
    final Set<JetElement> shadowedElements = new HashSet<JetElement>();
    JetVisitorVoid shadowAllChildren =
        new JetVisitorVoid() {
          @Override
          public void visitJetElement(@NotNull JetElement element) {
            if (shadowedElements.add(element)) {
              element.acceptChildren(this);
            }
          }
        };

    for (JetElement element : unreachableElements) {
      if (shadowedElements.contains(element)) continue;
      element.acceptChildren(shadowAllChildren);

      rootElements.removeAll(shadowedElements);
      rootElements.add(element);
    }
    return rootElements;
  }
  @Nullable
  public UsageInfo[] preprocessUsages(final Project project, final UsageInfo[] usages) {
    final ArrayList<UsageInfo> result = new ArrayList<>();
    final ArrayList<UsageInfo> overridingMethods = new ArrayList<>();
    final ArrayList<SafeDeleteParameterCallHierarchyUsageInfo> delegatingParams = new ArrayList<>();
    final ArrayList<SafeDeleteMethodCalleeUsageInfo> calleesSafeToDelete = new ArrayList<>();
    for (UsageInfo usage : usages) {
      if (usage.isNonCodeUsage) {
        result.add(usage);
      } else if (usage instanceof SafeDeleteMethodCalleeUsageInfo) {
        calleesSafeToDelete.add((SafeDeleteMethodCalleeUsageInfo) usage);
      } else if (usage instanceof SafeDeleteOverridingMethodUsageInfo) {
        overridingMethods.add(usage);
      } else if (usage instanceof SafeDeleteParameterCallHierarchyUsageInfo) {
        delegatingParams.add((SafeDeleteParameterCallHierarchyUsageInfo) usage);
      } else if (usage instanceof SafeDeleteAnnotation) {
        result.add(
            new SafeDeleteAnnotation(
                (PsiAnnotation) usage.getElement(),
                ((SafeDeleteAnnotation) usage).getReferencedElement(),
                true));
      } else {
        result.add(usage);
      }
    }

    if (!overridingMethods.isEmpty()) {
      if (ApplicationManager.getApplication().isUnitTestMode()) {
        result.addAll(overridingMethods);
      } else {
        OverridingMethodsDialog dialog = new OverridingMethodsDialog(project, overridingMethods);
        if (!dialog.showAndGet()) {
          return null;
        }
        final ArrayList<UsageInfo> selected = dialog.getSelected();
        final Set<UsageInfo> unselected = new HashSet<>(overridingMethods);
        unselected.removeAll(selected);

        if (!unselected.isEmpty()) {
          final List<PsiMethod> unselectedMethods =
              ContainerUtil.map(
                  unselected,
                  info -> ((SafeDeleteOverridingMethodUsageInfo) info).getOverridingMethod());

          for (Iterator<UsageInfo> iterator = result.iterator(); iterator.hasNext(); ) {
            final UsageInfo info = iterator.next();
            if (info instanceof SafeDeleteOverrideAnnotation
                && !allSuperMethodsSelectedToDelete(
                    unselectedMethods, ((SafeDeleteOverrideAnnotation) info).getMethod())) {
              iterator.remove();
            }
          }
        }

        result.addAll(selected);
      }
    }

    if (!delegatingParams.isEmpty()) {
      final SafeDeleteParameterCallHierarchyUsageInfo parameterHierarchyUsageInfo =
          delegatingParams.get(0);
      if (ApplicationManager.getApplication().isUnitTestMode()) {
        result.addAll(delegatingParams);
      } else {
        final PsiMethod method = parameterHierarchyUsageInfo.getCalledMethod();
        final PsiParameter parameter = parameterHierarchyUsageInfo.getReferencedElement();
        final int parameterIndex = method.getParameterList().getParameterIndex(parameter);
        final JavaCallerChooser chooser =
            new SafeDeleteJavaCallerChooser(method, project, result) {
              @Override
              protected ArrayList<SafeDeleteParameterCallHierarchyUsageInfo> getTopLevelItems() {
                return delegatingParams;
              }

              @Override
              protected int getParameterIdx() {
                return parameterIndex;
              }
            };
        TreeUtil.expand(chooser.getTree(), 2);
        if (!chooser.showAndGet()) {
          return null;
        }
      }
    }

    if (!calleesSafeToDelete.isEmpty()) {
      if (ApplicationManager.getApplication().isUnitTestMode()) {
        result.addAll(calleesSafeToDelete);
      } else {
        final PsiMethod method = calleesSafeToDelete.get(0).getCallerMethod();
        final ArrayList<UsageInfo> list = new ArrayList<>();
        JavaCallerChooser chooser =
            new SafeDeleteJavaCalleeChooser(method, project, list) {
              @Override
              protected ArrayList<SafeDeleteMethodCalleeUsageInfo> getTopLevelItems() {
                return calleesSafeToDelete;
              }
            };
        TreeUtil.expand(chooser.getTree(), 2);
        if (!chooser.showAndGet()) {
          return null;
        }
        result.addAll(list);
        final List<PsiElement> methodsToDelete = new ArrayList<>();
        for (UsageInfo info : list) {
          methodsToDelete.add(info.getElement());
        }
        methodsToDelete.add(method);
        final Condition<PsiElement> insideDeletedCondition =
            getUsageInsideDeletedFilter(
                methodsToDelete.toArray(new PsiElement[methodsToDelete.size()]));
        for (UsageInfo info : list) {
          SafeDeleteProcessor.addNonCodeUsages(
              info.getElement(),
              result,
              insideDeletedCondition,
              JavaRefactoringSettings.getInstance().RENAME_SEARCH_FOR_TEXT_FOR_METHOD,
              JavaRefactoringSettings.getInstance().RENAME_SEARCH_IN_COMMENTS_FOR_METHOD);
        }
      }
    }

    return result.toArray(new UsageInfo[result.size()]);
  }