Skip to content

Commit 58a7eaa

Browse files
committed
Putting in try block as to have finally block that restores file
1 parent c784eef commit 58a7eaa

1 file changed

Lines changed: 59 additions & 48 deletions

File tree

src/main/java/edu/illinois/cs/dt/tools/fixer/CleanerFixerPlugin.java

Lines changed: 59 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -653,61 +653,72 @@ private JavaMethod addHelperMethod(JavaMethod cleanerMethod, JavaMethod methodTo
653653
}
654654

655655
private FixStatus checkCleanerRemoval(List<String> failingOrder, JavaMethod cleanerMethod, NodeList<Statement> cleanerStmts) throws Exception {
656-
// Try to modify the cleanerMethod to remove the cleaner statements
657-
NodeList<Statement> allStatements = cleanerMethod.body().getStatements();
658-
NodeList<Statement> strippedStatements = NodeList.nodeList();
659-
NodeList<Statement> otherCleanerStmts = NodeList.nodeList(cleanerStmts);
660-
int j = 0;
661-
for (int i = 0; i < allStatements.size(); i++) {
662-
// Do not include the statement if we see it from the cleaner statements
663-
if (otherCleanerStmts.contains(allStatements.get(i))) {
664-
otherCleanerStmts.remove(allStatements.get(i));
665-
} else {
666-
strippedStatements.add(allStatements.get(i));
656+
try {
657+
// Try to modify the cleanerMethod to remove the cleaner statements
658+
NodeList<Statement> allStatements = cleanerMethod.body().getStatements();
659+
NodeList<Statement> strippedStatements = NodeList.nodeList();
660+
NodeList<Statement> otherCleanerStmts = NodeList.nodeList(cleanerStmts);
661+
int j = 0;
662+
for (int i = 0; i < allStatements.size(); i++) {
663+
// Do not include the statement if we see it from the cleaner statements
664+
if (otherCleanerStmts.contains(allStatements.get(i))) {
665+
otherCleanerStmts.remove(allStatements.get(i));
666+
} else {
667+
strippedStatements.add(allStatements.get(i));
668+
}
667669
}
668-
}
669670

670-
// If the stripped statements is still the same as all statements, then the cleaner statements must all be in @Before/After
671-
if (strippedStatements.equals(allStatements)) {
672-
TestPluginPlugin.info("All cleaner statements must be in setup/teardown.");
673-
return FixStatus.FIX_INLINE_SETUPTEARDOWN; // Indicating statements were in setup/teardown
674-
}
671+
// If the stripped statements is still the same as all statements, then the cleaner statements must all be in @Before/After
672+
if (strippedStatements.equals(allStatements)) {
673+
TestPluginPlugin.info("All cleaner statements must be in setup/teardown.");
674+
return FixStatus.FIX_INLINE_SETUPTEARDOWN; // Indicating statements were in setup/teardown
675+
}
675676

676-
// Set the cleaner method body to be the stripped version
677-
restore(cleanerMethod.javaFile());
678-
cleanerMethod = JavaMethod.find(cleanerMethod.methodName(), testSources(), classpath()).get(); // Reload, just in case
679-
cleanerMethod.method().setBody(new BlockStmt(strippedStatements));
680-
cleanerMethod.javaFile().writeAndReloadCompilationUnit();
681-
try {
682-
MvnCommands.runMvnInstall(this.project, false);
683-
} catch (Exception ex) {
684-
TestPluginPlugin.debug("Error building the code after stripping statements, does not compile");
685-
// Restore the state
677+
// Set the cleaner method body to be the stripped version
686678
restore(cleanerMethod.javaFile());
687-
return FixStatus.NOD; // Indicating did not work (TODO: Make it more clear)
688-
}
689-
// First try running in isolation
690-
List<String> isolationOrder = Collections.singletonList(cleanerMethod.methodName());
691-
if (!testOrderPasses(isolationOrder)) {
692-
TestPluginPlugin.info("Running cleaner by itself after removing statements does not pass.");
693-
// Restore the state
694-
restore(cleanerMethod.javaFile());
695-
return FixStatus.NOD; // Indicating did not work (TODO: Make it more clear)
696-
}
697-
// Then try running with the failing order, replacing the last test with this one
698-
List<String> newFailingOrder = new ArrayList<>(failingOrder);
699-
newFailingOrder.remove(newFailingOrder.size() - 1);
700-
newFailingOrder.add(cleanerMethod.methodName());
701-
if (testOrderPasses(newFailingOrder)) {
702-
TestPluginPlugin.info("Running cleaner in failing order after polluter still passes.");
679+
cleanerMethod = JavaMethod.find(cleanerMethod.methodName(), testSources(), classpath()).get(); // Reload, just in case
680+
cleanerMethod.method().setBody(new BlockStmt(strippedStatements));
681+
cleanerMethod.javaFile().writeAndReloadCompilationUnit();
682+
try {
683+
MvnCommands.runMvnInstall(this.project, false);
684+
} catch (Exception ex) {
685+
TestPluginPlugin.debug("Error building the code after stripping statements, does not compile");
686+
//// Restore the state
687+
//restore(cleanerMethod.javaFile());
688+
//cleanerMethod = JavaMethod.find(cleanerMethod.methodName(), testSources(), classpath()).get(); // Reload, just in case
689+
return FixStatus.NOD; // Indicating did not work (TODO: Make it more clear)
690+
}
691+
// First try running in isolation
692+
List<String> isolationOrder = Collections.singletonList(cleanerMethod.methodName());
693+
if (!testOrderPasses(isolationOrder)) {
694+
TestPluginPlugin.info("Running cleaner by itself after removing statements does not pass.");
695+
//// Restore the state
696+
//restore(cleanerMethod.javaFile());
697+
//cleanerMethod = JavaMethod.find(cleanerMethod.methodName(), testSources(), classpath()).get(); // Reload, just in case
698+
return FixStatus.NOD; // Indicating did not work (TODO: Make it more clear)
699+
}
700+
// Then try running with the failing order, replacing the last test with this one
701+
List<String> newFailingOrder = new ArrayList<>(failingOrder);
702+
newFailingOrder.remove(newFailingOrder.size() - 1);
703+
newFailingOrder.add(cleanerMethod.methodName());
704+
if (testOrderPasses(newFailingOrder)) {
705+
TestPluginPlugin.info("Running cleaner in failing order after polluter still passes.");
706+
//// Restore the state
707+
//restore(cleanerMethod.javaFile());
708+
//cleanerMethod = JavaMethod.find(cleanerMethod.methodName(), testSources(), classpath()).get(); // Reload, just in case
709+
return FixStatus.NOD; // Indicating did not work (TODO: Make it more clear)
710+
}
711+
712+
//// Restore the state
713+
//restore(cleanerMethod.javaFile());
714+
//cleanerMethod = JavaMethod.find(cleanerMethod.methodName(), testSources(), classpath()).get(); // Reload, just in case
715+
return FixStatus.FIX_INLINE_CANREMOVE; // Indicating statements can be removed
716+
} finally {
703717
// Restore the state
704718
restore(cleanerMethod.javaFile());
705-
return FixStatus.NOD; // Indicating did not work (TODO: Make it more clear)
719+
cleanerMethod = JavaMethod.find(cleanerMethod.methodName(), testSources(), classpath()).get(); // Reload, just in case
720+
MvnCommands.runMvnInstall(this.project, true);
706721
}
707-
708-
// Restore the state
709-
restore(cleanerMethod.javaFile());
710-
return FixStatus.FIX_INLINE_CANREMOVE; // Indicating statements can be removed
711722
}
712723

713724
// Make the cleaner statements based on the cleaner method and what method needs to be modified in the process

0 commit comments

Comments
 (0)