@Override
 public void replayRefactoring(RefactoringDescriptor refactoringDescriptor) throws CoreException {
   try {
     // FIXME: This is a temporary hack. It is required to overcome the problem that sometimes
     // Eclipse does not finish updating
     // program's structure yet, and thus, the refactoring can not be properly initialized (i.e.
     // the refactored element is not found).
     // Find a better solution, e.g. listen for some Eclipse "refreshing" process to complete.
     Thread.sleep(1500);
   } catch (InterruptedException e) {
     // do nothing
   }
   RefactoringStatus initializationStatus = new RefactoringStatus();
   Refactoring refactoring = refactoringDescriptor.createRefactoring(initializationStatus);
   if (!initializationStatus.isOK()) {
     Debugger.debugWarning(
         "Failed to initialize a refactoring from its descriptor: " + refactoringDescriptor);
     unperformedRefactorings.add(getTime());
     return;
   }
   // This remove is needed to ensure that multiple replays in the same run do not overlap
   unperformedRefactorings.remove(getTime());
   PerformRefactoringOperation performRefactoringOperation =
       new PerformRefactoringOperation(refactoring, CheckConditionsOperation.ALL_CONDITIONS);
   performRefactoringOperation.run(null);
   if (performRefactoringOperation.getConditionStatus().hasFatalError()) {
     throw new RuntimeException(
         "Failed to check preconditions of refactoring: " + refactoring.getName());
   }
   if (performRefactoringOperation.getValidationStatus().hasFatalError()) {
     throw new RuntimeException("Failed to validate refactoring: " + refactoring.getName());
   }
 }
 @Override
 protected TextChange createTextChange() throws CoreException {
   init(fRefactoring);
   fRefactoringStatus = fRefactoring.checkFinalConditions(new NullProgressMonitor());
   if (fRefactoringStatus.hasFatalError()) {
     TextFileChange dummyChange =
         new TextFileChange(
             "fatal error", (IFile) getCompilationUnit().getResource()); // $NON-NLS-1$
     dummyChange.setEdit(new InsertEdit(0, "")); // $NON-NLS-1$
     return dummyChange;
   }
   return (TextChange) fRefactoring.createChange(new NullProgressMonitor());
 }
Пример #3
0
 private void updateForcePreview() {
   boolean forcePreview = false;
   Refactoring refactoring = getRefactoring();
   ITextUpdating tu = (ITextUpdating) refactoring.getAdapter(ITextUpdating.class);
   IQualifiedNameUpdating qu =
       (IQualifiedNameUpdating) refactoring.getAdapter(IQualifiedNameUpdating.class);
   if (tu != null) {
     forcePreview = tu.getUpdateTextualMatches();
   }
   if (qu != null) {
     forcePreview |= qu.getUpdateQualifiedNames();
   }
   getRefactoringWizard().setForcePreviewReview(forcePreview);
 }
Пример #4
0
 public UserInterfaceStarter getStarter(Refactoring refactoring) {
   RefactoringProcessor processor =
       (RefactoringProcessor) refactoring.getAdapter(RefactoringProcessor.class);
   if (processor == null) return null;
   Tuple tuple = fMap.get(processor.getClass());
   if (tuple == null) return null;
   try {
     UserInterfaceStarter starter = tuple.starter.newInstance();
     Class<? extends RefactoringWizard> wizardClass = tuple.wizard;
     Constructor<? extends RefactoringWizard> constructor =
         wizardClass.getConstructor(new Class[] {Refactoring.class});
     RefactoringWizard wizard = constructor.newInstance(new Object[] {refactoring});
     starter.initialize(wizard);
     return starter;
   } catch (NoSuchMethodException e) {
     return null;
   } catch (IllegalAccessException e) {
     return null;
   } catch (InstantiationException e) {
     return null;
   } catch (IllegalArgumentException e) {
     return null;
   } catch (InvocationTargetException e) {
     return null;
   }
 }
 protected final Change performChange(Refactoring refactoring, boolean storeUndo)
     throws Exception {
   CreateChangeOperation create = new CreateChangeOperation(refactoring);
   PerformChangeOperation perform = new PerformChangeOperation(create);
   if (storeUndo) {
     perform.setUndoManager(getUndoManager(), refactoring.getName());
   }
   ResourcesPlugin.getWorkspace().run(perform, new NullProgressMonitor());
   assertTrue("Change wasn't executed", perform.changeExecuted());
   return perform.getUndoChange();
 }
  private void helper1(
      String[] methodNames,
      String[][] signatures,
      boolean deleteAllInSourceType,
      boolean deleteAllMatchingMethods,
      boolean replaceOccurences)
      throws Exception {
    ICompilationUnit cu = createCUfromTestFile(getPackageP(), "A");
    IType type = getType(cu, "B");
    IMethod[] methods = getMethods(type, methodNames, signatures);

    ExtractSupertypeProcessor processor = createRefactoringProcessor(methods);
    Refactoring refactoring = processor.getRefactoring();
    processor.setMembersToMove(methods);

    assertTrue("activation", refactoring.checkInitialConditions(new NullProgressMonitor()).isOK());

    processor.setTypesToExtract(new IType[] {type});
    processor.setTypeName("Z");
    processor.setCreateMethodStubs(true);
    processor.setInstanceOf(false);
    processor.setReplace(replaceOccurences);
    if (deleteAllInSourceType) processor.setDeletedMethods(methods);
    if (deleteAllMatchingMethods)
      processor.setDeletedMethods(
          getMethods(processor.getMatchingElements(new NullProgressMonitor(), false)));

    RefactoringStatus status = refactoring.checkFinalConditions(new NullProgressMonitor());
    assertTrue("precondition was supposed to pass", !status.hasError());
    performChange(refactoring, false);

    String expected = getFileContents(getOutputTestFileName("A"));
    String actual = cu.getSource();
    assertEqualLines(expected, actual);

    expected = getFileContents(getOutputTestFileName("Z"));
    ICompilationUnit unit = getPackageP().getCompilationUnit("Z.java");
    if (!unit.exists()) assertTrue("extracted compilation unit does not exist", false);
    actual = unit.getBuffer().getContents();
    assertEqualLines(expected, actual);
  }
 protected final RefactoringStatus performRefactoring(Refactoring ref, boolean providesUndo)
     throws Exception {
   performDummySearch();
   IUndoManager undoManager = getUndoManager();
   if (DESCRIPTOR_TEST) {
     final CreateChangeOperation create =
         new CreateChangeOperation(
             new CheckConditionsOperation(ref, CheckConditionsOperation.ALL_CONDITIONS),
             RefactoringStatus.FATAL);
     create.run(new NullProgressMonitor());
     RefactoringStatus checkingStatus = create.getConditionCheckingStatus();
     if (!checkingStatus.isOK()) return checkingStatus;
     Change change = create.getChange();
     ChangeDescriptor descriptor = change.getDescriptor();
     if (descriptor instanceof RefactoringChangeDescriptor) {
       RefactoringChangeDescriptor rcd = (RefactoringChangeDescriptor) descriptor;
       RefactoringDescriptor refactoringDescriptor = rcd.getRefactoringDescriptor();
       if (refactoringDescriptor instanceof JavaRefactoringDescriptor) {
         JavaRefactoringDescriptor jrd = (JavaRefactoringDescriptor) refactoringDescriptor;
         RefactoringStatus validation = jrd.validateDescriptor();
         if (!validation.isOK()) return validation;
         RefactoringStatus refactoringStatus = new RefactoringStatus();
         Class expected = jrd.getClass();
         RefactoringContribution contribution =
             RefactoringCore.getRefactoringContribution(jrd.getID());
         jrd =
             (JavaRefactoringDescriptor)
                 contribution.createDescriptor(
                     jrd.getID(),
                     jrd.getProject(),
                     jrd.getDescription(),
                     jrd.getComment(),
                     contribution.retrieveArgumentMap(jrd),
                     jrd.getFlags());
         assertEquals(expected, jrd.getClass());
         ref = jrd.createRefactoring(refactoringStatus);
         if (!refactoringStatus.isOK()) return refactoringStatus;
         TestRenameParticipantSingle.reset();
         TestCreateParticipantSingle.reset();
         TestMoveParticipantSingle.reset();
         TestDeleteParticipantSingle.reset();
       }
     }
   }
   final CreateChangeOperation create =
       new CreateChangeOperation(
           new CheckConditionsOperation(ref, CheckConditionsOperation.ALL_CONDITIONS),
           RefactoringStatus.FATAL);
   final PerformChangeOperation perform = new PerformChangeOperation(create);
   perform.setUndoManager(undoManager, ref.getName());
   IWorkspace workspace = ResourcesPlugin.getWorkspace();
   if (fIsPreDeltaTest) {
     IResourceChangeListener listener =
         new IResourceChangeListener() {
           public void resourceChanged(IResourceChangeEvent event) {
             if (create.getConditionCheckingStatus().isOK() && perform.changeExecuted()) {
               TestModelProvider.assertTrue(event.getDelta());
             }
           }
         };
     try {
       TestModelProvider.clearDelta();
       workspace.checkpoint(false);
       workspace.addResourceChangeListener(listener);
       executePerformOperation(perform, workspace);
     } finally {
       workspace.removeResourceChangeListener(listener);
     }
   } else {
     executePerformOperation(perform, workspace);
   }
   RefactoringStatus status = create.getConditionCheckingStatus();
   if (!status.isOK()) return status;
   assertTrue("Change wasn't executed", perform.changeExecuted());
   Change undo = perform.getUndoChange();
   if (providesUndo) {
     assertNotNull("Undo doesn't exist", undo);
     assertTrue("Undo manager is empty", undoManager.anythingToUndo());
   } else {
     assertNull("Undo manager contains undo but shouldn't", undo);
   }
   return null;
 }