Package org.eclipse.ltk.core.refactoring

Examples of org.eclipse.ltk.core.refactoring.Change


        RefactoringStatus result = refactoring.checkAllConditions(monitor);

        assertTrue("Refactoring is not ok: " + result.getMessageMatchingSeverity(RefactoringStatus.WARNING),
                result.isOK());

        Change change = refactoring.createChange(monitor);
        change.perform(monitor);

        assertContentsEqual(data.result, document.get());

        FileUtilsFileBuffer.IN_TESTS = false;
    }
View Full Code Here


   * @return A list of changed files or a map containing a list of errors.
   */
  private Object apply(ICompilationUnit src, ChangeCorrectionProposal proposal)
    throws Exception
  {
    Change change = null;
    try {
      NullProgressMonitor monitor = new NullProgressMonitor();
      change = proposal.getChange();
      change.initializeValidationData(monitor);
      RefactoringStatus status = change.isValid(monitor);
      if (status.hasFatalError()){
        List<String> errors = new ArrayList<String>();
        for (RefactoringStatusEntry entry : status.getEntries()){
          String message = entry.getMessage();
          if (!errors.contains(message) &&
              !message.startsWith("Found potential matches"))
          {
            errors.add(message);
          }
        }
        HashMap<String,List<String>> result = new HashMap<String,List<String>>();
        result.put("errors", errors);
        return result;
      }

      ResourceChangeListener rcl = new ResourceChangeListener();
      IWorkspace workspace = ResourcesPlugin.getWorkspace();
      workspace.addResourceChangeListener(rcl);
      try{
        TextEdit[] edits = new TextEdit[0];
        if (change instanceof TextFileChange){
          TextFileChange fileChange = (TextFileChange)change;
          fileChange.setSaveMode(TextFileChange.FORCE_SAVE);
          TextEdit edit = fileChange.getEdit();
          if (edit instanceof MultiTextEdit){
            edits = ((MultiTextEdit)edit).getChildren();
          }else{
            edits = new TextEdit[]{edit};
          }
        }

        PerformChangeOperation changeOperation = new PerformChangeOperation(change);
        changeOperation.setUndoManager(
            RefactoringCore.getUndoManager(), proposal.getName());
        changeOperation.run(monitor);

        if (edits.length > 0 &&
            change instanceof CompilationUnitChange &&
            src.equals(((CompilationUnitChange)change).getCompilationUnit()))
        {
          for (TextEdit edit : edits){
            int offset = edit.getOffset();
            int length = edit.getLength();

            // for "Create field" and "Create local" the edit length includes
            // additional existing code that we don't want to reformat.
            if (proposal instanceof NewVariableCorrectionProposal) {
              String text = src.getBuffer()
                .getText(edit.getOffset(), edit.getLength());
              int index = text.indexOf('\n');
              if (index != -1){
                length = index;
                // include the white space up to the next bit of code
                while(length < text.length()){
                  char next = text.charAt(length);
                  if (next == '\t' || next == '\n' || next == ' '){
                    length += 1;
                    continue;
                  }
                  break;
                }
              }
            }

            JavaUtils.format(
                src, CodeFormatter.K_COMPILATION_UNIT, offset, length);
          }
        }

        // if the proposal change touched the imports, then run our import
        // grouping edit after it.
        if (proposal instanceof ASTRewriteCorrectionProposal){
          ASTRewriteCorrectionProposal astProposal =
            (ASTRewriteCorrectionProposal)proposal;
          if (astProposal.getImportRewrite() != null){
            TextEdit groupingEdit =
              ImportUtils.importGroupingEdit(src, getPreferences());
            if (groupingEdit != null){
              JavaModelUtil.applyEdit(src, groupingEdit, true, null);
              if (src.isWorkingCopy()) {
                src.commitWorkingCopy(false, null);
              }
            }
          }
        }

        return rcl.getChangedFiles();
      }finally{
        workspace.removeResourceChangeListener(rcl);
      }
    }finally{
      if (change != null) {
        change.dispose();
      }
    }
  }
View Full Code Here

      int stopSeverity = RefactoringCore.getConditionCheckingFailedSeverity();
      if (status.getSeverity() >= stopSeverity) {
        throw new RefactorException(status);
      }

      Change change = refactoring.createChange(new SubProgressMonitor(monitor, 2));
      change.initializeValidationData(new SubProgressMonitor(monitor, 1));

      // preview
      if (commandLine.hasOption(PREVIEW_OPTION)){
        // preview a specific file
        if (commandLine.hasOption(DIFF_OPTION)){
          return previewChange(change, commandLine.getValue("d"));
        }

        HashMap<String,Object> preview = new HashMap<String,Object>();
        String previewOpt = "-" + PREVIEW_OPTION;
        String[] args = commandLine.getArgs();
        StringBuffer apply = new StringBuffer();
        for (String arg : args){
          if (arg.equals(previewOpt)){
            continue;
          }
          if (apply.length() > 0){
            apply.append(' ');
          }
          if (arg.startsWith("-")){
            apply.append(arg);
          }else{
            apply.append('"').append(arg).append('"');
          }
        }
        // the command to apply the change minus the editor + pretty options.
        preview.put("apply", apply.toString()
            .replaceFirst("-" + Options.EDITOR_OPTION + "\\s\"\\w+\" ", "")
            .replaceFirst("-" + Options.PRETTY_OPTION + ' ', ""));
        preview.put("changes", previewChanges(change));
        return preview;
      }

      IWorkspace workspace = ResourcesPlugin.getWorkspace();
      ResourceChangeListener rcl = new ResourceChangeListener();
      workspace.addResourceChangeListener(rcl);
      try{
        PerformChangeOperation changeOperation = new PerformChangeOperation(change);
        // passing in refactor.name to the change op doesn't seem to do the
        // trick, so lets force our name on the change since the undo manager
        // will use the change's name if label is null (which it shouldn't be,
        // but is, hence this hack).
        if (change instanceof CompositeChange){
          try{
            Field fName = CompositeChange.class.getDeclaredField("fName");
            fName.setAccessible(true);
            fName.set(change, refactor.name);
          }catch(NoSuchFieldException nsfe){
            // change doesn't have the expected fName field.
          }
        }

        changeOperation.setUndoManager(
            RefactoringCore.getUndoManager(), change.getName());

        changeOperation.run(new SubProgressMonitor(monitor, 4));
        return rcl.getChangedFiles();
      }finally{
        workspace.removeResourceChangeListener(rcl);
View Full Code Here

        composite
            .merge(new CompositeChange(
                RefactoringCoreMessages.MoveRefactoring_reorganize_elements,
                fChangeManager.getAllChanges()));

        Change fileMove = createSimpleMoveChange(new SubProgressMonitor(
            pm, 1));
        if (fileMove instanceof CompositeChange) {
          composite.merge(((CompositeChange) fileMove));
        } else {
          composite.add(fileMove);
View Full Code Here

              (IContainer) ResourceUtil.getResource(dest),
              nameProposer, copyQueries);
      }

      String newName = nameProposer.createNewName(cu, dest);
      Change simpleCopy = new CopySourceModuleChange(cu, dest,
          copyQueries.createStaticQuery(newName));
      if (newName == null || newName.equals(cu.getElementName()))
        return simpleCopy;

      try {
View Full Code Here

    Map grouped = ReorgUtils
        .groupBySourceModule(getElementsSmallerThanCu(modelElements));
    if (grouped.size() != 0) {
      Assert.isNotNull(manager);
      for (Entry entry : (Set<Entry>) grouped.entrySet()) {
        Change change = createDeleteChange(
            (ISourceModule) entry.getKey(),
            (List) entry.getValue(), manager);
        if (change != null) {
          result.add(change);
        }
View Full Code Here

   */
  public final Change perform(IProgressMonitor pm) throws CoreException {
    try {
      pm.beginTask(RefactoringCoreMessages.AbstractDeleteChange_deleting,
          1);
      Change undo = doDelete(pm);
      return undo;
    } finally {
      pm.done();
    }
  }
View Full Code Here

        URL rtLocation = coopRTBundle.getEntry("/coopRT.jar");
        try {
      String jarPathString = FileLocator.resolve(rtLocation).getFile();
      newClassPath[oldClassPath.length] = JavaCore.newLibraryEntry(new Path(jarPathString), null, null, false);

      Change newClasspathChange =
                      ClasspathFixProposal.newClasspathChange(javaProject, newClassPath, javaProject.getOutputLocation());
      new PerformChangeOperation(newClasspathChange).run(null);
    } catch (IOException e) {
      CoopIIIActivator.getInstance().getLog().log(new Status(Status.ERROR, "lang.coopIII.ui", "Could not configure Co-op RT to project.", e));
    }
View Full Code Here

    // apply changes
    if (!coopRTPresent || !srcGenPresent) {
      IClasspathEntry[] newClassPath = new IClasspathEntry[newClasspathAsList.size()];
      newClassPath = newClasspathAsList.toArray(newClassPath);
     
      Change newClasspathChange = ClasspathFixProposal.newClasspathChange(
          javaProject, newClassPath, javaProject.getOutputLocation());
      new PerformChangeOperation(newClasspathChange).run(null);
    }
  }
View Full Code Here

    if (newClassPath.size() != oldClassPath.length) {

      IClasspathEntry[] newClassPathArray = new IClasspathEntry[newClassPath
          .size()];
      newClassPathArray = newClassPath.toArray(newClassPathArray);
      Change newClasspathChange = ClasspathFixProposal
          .newClasspathChange(javaProject, newClassPathArray,
              javaProject.getOutputLocation());
      new PerformChangeOperation(newClasspathChange).run(null);
    }
  }
View Full Code Here

TOP

Related Classes of org.eclipse.ltk.core.refactoring.Change

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.