Skip to content

Commit

Permalink
Discard vacuous refactoring results
Browse files Browse the repository at this point in the history
Filtering out no-op refactoring operations avoids unnecessary build
output and file I/O.

Fixes google#1562.
  • Loading branch information
Stephan202 committed May 30, 2022
1 parent 71484cc commit b50990f
Showing 1 changed file with 12 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -160,12 +160,11 @@ public DescriptionListener getDescriptionListener(Log log, JCCompilationUnit com

RefactoringResult applyChanges(URI uri) throws Exception {
Collection<DelegatingDescriptionListener> listeners = foundSources.removeAll(uri);
if (listeners.isEmpty()) {
return RefactoringResult.create("", RefactoringResultType.NO_CHANGES);
if (doApplyProcess(fileDestination, new FsFileSource(rootPath), listeners)) {
return postProcess.apply(uri);
}

doApplyProcess(fileDestination, new FsFileSource(rootPath), listeners);
return postProcess.apply(uri);
return RefactoringResult.create("", RefactoringResultType.NO_CHANGES);
}

private static void writePatchFile(
Expand All @@ -185,22 +184,30 @@ private static void writePatchFile(
}
}

private static void doApplyProcess(
private static boolean doApplyProcess(
FileDestination fileDestination,
FileSource fileSource,
Collection<DelegatingDescriptionListener> listeners) {
boolean appliedDiff = false;
for (DelegatingDescriptionListener listener : listeners) {
if (listener.base.isEmpty()) {
continue;
}

try {
SourceFile file = fileSource.readFile(listener.base.getRelevantFileName());
listener.base.applyDifferences(file);
fileDestination.writeFile(file);
appliedDiff = true;
} catch (IOException e) {
logger.log(
Level.WARNING,
"Failed to apply diff to file " + listener.base.getRelevantFileName(),
e);
}
}

return appliedDiff;
}

private static final class DelegatingDescriptionListener implements DescriptionListener {
Expand Down

0 comments on commit b50990f

Please sign in to comment.