diff --git a/edu.cuny.citytech.refactoring.common.core/src/edu/cuny/citytech/refactoring/common/core/messages.properties b/edu.cuny.citytech.refactoring.common.core/src/edu/cuny/citytech/refactoring/common/core/messages.properties index 9e89e5d..bbfd5bb 100644 --- a/edu.cuny.citytech.refactoring.common.core/src/edu/cuny/citytech/refactoring/common/core/messages.properties +++ b/edu.cuny.citytech.refactoring.common.core/src/edu/cuny/citytech/refactoring/common/core/messages.properties @@ -1,6 +1,7 @@ -CheckingPreconditions=Checking preconditions (may take a while)... CompilingSource=Compiling source... +CheckingPreconditions=Checking preconditions (may take a while)... CreatingChange=Creating change... CUContainsCompileErrors=Member ''{0}'' has a compilation unit ''{1}'' that contains compilation errors. RefactoringNotPossible=Refactoring not possible. ClearingCaches=Clearing caches... +NoElementsToRefactor=No elements have passed preconditions. diff --git a/edu.cuny.hunter.refactoring.common.java.core/src/edu/cuny/hunter/refactoring/common/java/core/RefactoringProcessor.java b/edu.cuny.hunter.refactoring.common.java.core/src/edu/cuny/hunter/refactoring/common/java/core/RefactoringProcessor.java index 69ad495..ec0ca13 100644 --- a/edu.cuny.hunter.refactoring.common.java.core/src/edu/cuny/hunter/refactoring/common/java/core/RefactoringProcessor.java +++ b/edu.cuny.hunter.refactoring.common.java.core/src/edu/cuny/hunter/refactoring/common/java/core/RefactoringProcessor.java @@ -1,7 +1,10 @@ package edu.cuny.hunter.refactoring.common.java.core; +import static edu.cuny.citytech.refactoring.common.core.Messages.CheckingPreconditions; import static edu.cuny.citytech.refactoring.common.core.Messages.NoElementsToRefactor; +import static java.util.stream.Collectors.toSet; +import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.Map; @@ -23,6 +26,8 @@ import org.eclipse.jdt.internal.corext.refactoring.util.TextEditBasedChangeManager; import org.eclipse.ltk.core.refactoring.Change; import org.eclipse.ltk.core.refactoring.NullChange; +import org.eclipse.ltk.core.refactoring.RefactoringStatus; +import org.eclipse.ltk.core.refactoring.participants.CheckConditionsContext; import edu.cuny.citytech.refactoring.common.core.Messages; @@ -31,7 +36,6 @@ * * @author Raffi Khatchadourian */ -@SuppressWarnings("restriction") public abstract class RefactoringProcessor extends edu.cuny.citytech.refactoring.common.core.RefactoringProcessor { protected Set entities = new HashSet<>(); @@ -92,12 +96,42 @@ protected static void manageCompilationUnit(final TextEditBasedChangeManager man manager.manage(rewrite.getCu(), change); } + @Override + public RefactoringStatus checkFinalConditions(IProgressMonitor monitor, CheckConditionsContext context) + throws CoreException, OperationCanceledException { + RefactoringStatus status = new RefactoringStatus(); + SubMonitor progress = SubMonitor.convert(monitor, CheckingPreconditions, 1); + + // get the status of each stream. + RefactoringStatus collectedStatus = this.getEntities().parallelStream().map(RefactorableEntity::getStatus) + .collect(() -> new RefactoringStatus(), (a, b) -> a.merge(b), (a, b) -> a.merge(b)); + status.merge(collectedStatus); + progress.worked(1); + + // if there are no fatal errors. + if (!status.hasFatalError()) { + // these are the entities passing preconditions. + Set passingEntities = this.getPassingEntities(); + + // add a fatal error if there are no passing entities. + if (passingEntities.isEmpty()) + status.addFatalError(Messages.NoElementsToRefactor); + else { + // TODO: + // Checks.addModifiedFilesToChecker(ResourceUtil.getFiles(fChangeManager.getAllCompilationUnits()), + // context); + } + } + + return status; + } + @Override public Change createChange(IProgressMonitor monitor) throws CoreException, OperationCanceledException { try { SubMonitor progress = SubMonitor.convert(monitor, Messages.CreatingChange, 100); - Set entities = this.getEntities(); + Set entities = this.getPassingEntities(); if (entities.isEmpty()) return new NullChange(NoElementsToRefactor); @@ -107,9 +141,7 @@ public Change createChange(IProgressMonitor monitor) throws CoreException, Opera for (RefactorableEntity entity : entities) { CompilationUnitRewrite rewrite = this.getCompilationUnitRewrite(entity.getCompilationUnit(), entity.getEnclosingCompilationUnit()); - entity.transform(rewrite); - transformProgress.worked(1); } @@ -120,7 +152,6 @@ public Change createChange(IProgressMonitor monitor) throws CoreException, Opera .filter(cu -> !manager.containsChangesIn(cu)).toArray(ICompilationUnit[]::new); SubMonitor saveProgress = progress.split(30).setWorkRemaining(units.length * 2); - for (ICompilationUnit cu : units) { CompilationUnit compilationUnit = this.getCompilationUnit(cu, saveProgress.split(1)); manageCompilationUnit(manager, this.getCompilationUnitRewrite(cu, compilationUnit), saveProgress.split(1)); @@ -142,4 +173,19 @@ public Object[] getElements() { protected Set getEntities() { return entities; } + + /** + * @return True iff the {@link RefactorableEntity} {@link Set} changed as a result of this call. + * @see java.util.Set#addAll + */ + protected boolean addEntities(Set entities) { + return this.getEntities().addAll(entities); + } + + public Set getPassingEntities() { + Set entities = this.getEntities(); + + return entities == null ? Collections.emptySet() + : entities.parallelStream().filter(s -> !s.getStatus().hasError()).collect(toSet()); + } }