Esempio n. 1
0
 private OrderMap(@NotNull TObjectIntHashMap<HighlightSeverity> map) {
   super(map.size());
   map.forEachEntry(
       new TObjectIntProcedure<HighlightSeverity>() {
         @Override
         public boolean execute(HighlightSeverity key, int value) {
           OrderMap.super.put(key, value);
           return true;
         }
       });
   trimToSize();
 }
 @Override
 public void dispose() {
   ourInstances.remove(myParent);
   synchronized (this) {
     myCounts.forEachEntry(
         new TObjectIntProcedure<VirtualFilePointerImpl>() {
           @Override
           public boolean execute(VirtualFilePointerImpl pointer, int disposeCount) {
             int after = pointer.myNode.incrementUsageCount(-disposeCount + 1);
             LOG.assertTrue(after > 0, after);
             pointer.dispose();
             return true;
           }
         });
   }
 }
  @Override
  protected ValidationInfo doValidate() {
    final String text = getEnteredName();
    if (!GroovyNamesUtil.isIdentifier(text)) {
      return new ValidationInfo(
          GroovyRefactoringBundle.message("name.is.wrong", text), myNameSuggestionsField);
    }

    if (myTypeComboBox.isClosureSelected()) {
      final Ref<ValidationInfo> info = new Ref<ValidationInfo>();
      toRemoveCBs.forEachEntry(
          new TObjectIntProcedure<JCheckBox>() {
            @Override
            public boolean execute(JCheckBox checkbox, int index) {
              if (!checkbox.isSelected()) return true;

              final GrParameter param = myInfo.getToReplaceIn().getParameters()[index];
              final ParameterInfo pinfo = findParamByOldName(param.getName());
              if (pinfo == null || !pinfo.passAsParameter()) return true;

              final String message =
                  GroovyRefactoringBundle.message(
                      "you.cannot.pass.as.parameter.0.because.you.remove.1.from.base.method",
                      pinfo.getName(),
                      param.getName());
              info.set(new ValidationInfo(message));
              return false;
            }
          });
      if (info.get() != null) {
        return info.get();
      }
    }

    return null;
  }
  @Override
  protected void init() {
    super.init();

    JavaRefactoringSettings settings = JavaRefactoringSettings.getInstance();

    initReplaceFieldsWithGetters(settings);

    myDeclareFinalCheckBox.setSelected(hasFinalModifier());
    myDelegateViaOverloadingMethodCheckBox.setVisible(myInfo.getToSearchFor() != null);

    setTitle(RefactoringBundle.message("introduce.parameter.title"));

    myTable.init(myInfo);

    final GrParameter[] parameters = myInfo.getToReplaceIn().getParameters();
    toRemoveCBs.forEachEntry(
        new TObjectIntProcedure<JCheckBox>() {
          @Override
          public boolean execute(JCheckBox checkbox, int index) {
            checkbox.setSelected(true);

            final GrParameter param = parameters[index];
            final ParameterInfo pinfo = findParamByOldName(param.getName());
            if (pinfo != null) {
              pinfo.setPassAsParameter(false);
            }
            return true;
          }
        });

    updateSignature();

    if (myCanIntroduceSimpleParameter) {
      mySignaturePanel.setVisible(false);

      // action to hide signature panel if we have variants to introduce simple parameter
      myTypeComboBox.addItemListener(
          new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
              mySignaturePanel.setVisible(myTypeComboBox.isClosureSelected());
              pack();
            }
          });
    }

    final PsiType closureReturnType = inferClosureReturnType();
    if (PsiType.VOID.equals(closureReturnType)) {
      myForceReturnCheckBox.setEnabled(false);
      myForceReturnCheckBox.setSelected(false);
    } else {
      myForceReturnCheckBox.setSelected(isForceReturn());
    }

    if (myInfo.getToReplaceIn() instanceof GrClosableBlock) {
      myDelegateViaOverloadingMethodCheckBox.setEnabled(false);
      myDelegateViaOverloadingMethodCheckBox.setToolTipText(
          "Delegating is not allowed in closure context");
    }

    pack();
  }
Esempio n. 5
0
  public PsiImportList prepareOptimizeImportsResult(@NotNull final PsiJavaFile file) {
    CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(file.getProject());

    final Set<String> namesToImportStaticly = new THashSet<String>();
    String[] names =
        collectNamesToImport(
            file,
            namesToImportStaticly); // Note: this array may contain "<packageOrClassName>.*" for
                                    // unresolved imports!
    Arrays.sort(names);

    ArrayList<String> namesList = new ArrayList<String>();
    ImportLayoutTable table = mySettings.IMPORT_LAYOUT_TABLE;
    if (table != null) {
      int[] entriesForName = ArrayUtil.newIntArray(names.length);
      for (int i = 0; i < names.length; i++) {
        entriesForName[i] = findEntryIndex(names[i]);
      }

      Entry[] entries = table.getEntries();
      for (int i = 0; i < entries.length; i++) {
        Entry entry = entries[i];
        if (entry instanceof PackageEntry) {
          for (int j = 0; j < names.length; j++) {
            if (entriesForName[j] == i) {
              namesList.add(names[j]);
              names[j] = null;
            }
          }
        }
      }
    }
    for (String name : names) {
      if (name != null) namesList.add(name);
    }
    names = ArrayUtil.toStringArray(namesList);

    TObjectIntHashMap<String> packageToCountMap = new TObjectIntHashMap<String>();
    TObjectIntHashMap<String> classToCountMap = new TObjectIntHashMap<String>();
    for (String name : names) {
      String packageOrClassName = getPackageOrClassName(name);
      if (packageOrClassName.length() == 0) continue;
      if (namesToImportStaticly.contains(name)) {
        int count = classToCountMap.get(packageOrClassName);
        classToCountMap.put(packageOrClassName, count + 1);
      } else {
        int count = packageToCountMap.get(packageOrClassName);
        packageToCountMap.put(packageOrClassName, count + 1);
      }
    }

    final Set<String> classesOrPackagesToImportOnDemand = new THashSet<String>();
    class MyVisitorProcedure implements TObjectIntProcedure<String> {
      private final boolean myIsVisitingPackages;

      MyVisitorProcedure(boolean isVisitingPackages) {
        myIsVisitingPackages = isVisitingPackages;
      }

      public boolean execute(final String packageOrClassName, final int count) {
        if (isToUseImportOnDemand(packageOrClassName, count, !myIsVisitingPackages)) {
          classesOrPackagesToImportOnDemand.add(packageOrClassName);
        }
        return true;
      }
    }
    classToCountMap.forEachEntry(new MyVisitorProcedure(false));
    packageToCountMap.forEachEntry(new MyVisitorProcedure(true));

    Set<String> classesToUseSingle =
        findSingleImports(file, names, classesOrPackagesToImportOnDemand, namesToImportStaticly);

    try {
      final String text =
          buildImportListText(
              names, classesOrPackagesToImportOnDemand, classesToUseSingle, namesToImportStaticly);
      String ext = StdFileTypes.JAVA.getDefaultExtension();
      final PsiJavaFile dummyFile =
          (PsiJavaFile)
              PsiFileFactory.getInstance(file.getProject())
                  .createFileFromText("_Dummy_." + ext, StdFileTypes.JAVA, text);
      codeStyleManager.reformat(dummyFile);

      PsiImportList resultList = dummyFile.getImportList();
      PsiImportList oldList = file.getImportList();
      if (oldList.isReplaceEquivalent(resultList)) return null;
      return resultList;
    } catch (IncorrectOperationException e) {
      LOG.error(e);
      return null;
    }
  }