Skip to content

Commit

Permalink
Fix false error on extraction of for loop contents (#1415)
Browse files Browse the repository at this point in the history
- fix ExtractMethodAnalyzer.canHandleBranches() to not flag a
  non-labelled break statement if the for loop is included
- add new test to ExtractMethodTests
- fixes #1291
  • Loading branch information
jjohnstn committed Jun 6, 2024
1 parent 1cc6fff commit cb81f1f
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2000, 2023 IBM Corporation and others.
* Copyright (c) 2000, 2024 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
Expand All @@ -19,7 +19,9 @@
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.eclipse.core.runtime.CoreException;
Expand Down Expand Up @@ -469,7 +471,7 @@ private String canHandleBranches() {
return RefactoringCoreMessages.ExtractMethodAnalyzer_branch_mismatch;
}

final String continueMatchesLoopProblem[]= { null };
final Map<ASTNode, String> pendingProblems= new LinkedHashMap<>();
for (ASTNode astNode : selectedNodes) {
astNode.accept(new ASTVisitor() {
ArrayList<String> fLocalLoopLabels= new ArrayList<>();
Expand All @@ -479,14 +481,14 @@ private String canHandleBranches() {
public boolean visit(BreakStatement node) {
SimpleName label= node.getLabel();
if (label != null && !fLocalLoopLabels.contains(label.getIdentifier())) {
continueMatchesLoopProblem[0]= Messages.format(
pendingProblems.put(label, Messages.format(
RefactoringCoreMessages.ExtractMethodAnalyzer_branch_break_mismatch,
new Object[] { ("break " + label.getIdentifier()) }); //$NON-NLS-1$
new Object[] { ("break " + label.getIdentifier()) })); //$NON-NLS-1$
} else if (label == null) {
ASTNode parentStatement= ASTNodes.getFirstAncestorOrNull(node, WhileStatement.class, ForStatement.class,
DoStatement.class, SwitchStatement.class, EnhancedForStatement.class);
if (parentStatement != null && !fBreakTargets.contains(parentStatement)) {
continueMatchesLoopProblem[0]= RefactoringCoreMessages.ExtractMethodAnalyzer_break_parent_missing;
pendingProblems.put(parentStatement, RefactoringCoreMessages.ExtractMethodAnalyzer_break_parent_missing);
}
}
return false;
Expand All @@ -495,33 +497,39 @@ public boolean visit(BreakStatement node) {
@Override
public boolean visit(LabeledStatement node) {
SimpleName label= node.getLabel();
if (label != null)
if (label != null) {
fLocalLoopLabels.add(label.getIdentifier());
}
return true;
}

@Override
public void endVisit(ForStatement node) {
pendingProblems.remove(node);
fBreakTargets.add(node);
}

@Override
public void endVisit(EnhancedForStatement node) {
pendingProblems.remove(node);
fBreakTargets.add(node);
}

@Override
public void endVisit(DoStatement node) {
pendingProblems.remove(node);
fBreakTargets.add(node);
}

@Override
public void endVisit(SwitchStatement node) {
pendingProblems.remove(node);
fBreakTargets.add(node);
}

@Override
public void endVisit(WhileStatement node) {
pendingProblems.remove(node);
fBreakTargets.add(node);
}

Expand All @@ -530,15 +538,18 @@ public void endVisit(ContinueStatement node) {
SimpleName label= node.getLabel();
if (label != null && !fLocalLoopLabels.contains(label.getIdentifier())) {
if (fEnclosingLoopLabel == null || ! label.getIdentifier().equals(fEnclosingLoopLabel.getIdentifier())) {
continueMatchesLoopProblem[0]= Messages.format(
pendingProblems.put(node, Messages.format(
RefactoringCoreMessages.ExtractMethodAnalyzer_branch_continue_mismatch,
new Object[] { "continue " + label.getIdentifier() }); //$NON-NLS-1$
new Object[] { "continue " + label.getIdentifier() })); //$NON-NLS-1$
}
}
}
});
}
return continueMatchesLoopProblem[0];
if (!pendingProblems.isEmpty()) {
return pendingProblems.values().iterator().next();
}
return null;
}

private Statement getParentLoopBody(ASTNode node) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package branch_in;

import java.util.List;

public class A_test770 {

public static void foo(List<List<String>> defs) {
for (List<String> def : defs) {
/*]*/
boolean isLeftRecursive= false;
for (String rule : def) {
if (!rule.isEmpty()) {
break;
}
}

if (!isLeftRecursive) {
continue;
}
/*[*/
}
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package branch_out;

import java.util.List;

public class A_test770 {

public static void foo(List<List<String>> defs) {
for (List<String> def : defs) {
/*]*/
extracted(def);
/*[*/
}
}

protected static void extracted(List<String> def) {
boolean isLeftRecursive= false;
for (String rule : def) {
if (!rule.isEmpty()) {
break;
}
}

if (!isLeftRecursive) {
return;
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -2125,6 +2125,11 @@ public void test769() throws Exception {
branchTest();
}

@Test
public void test770() throws Exception {
branchTest();
}

//---- Test for CUs with compiler errors

@Test
Expand Down

0 comments on commit cb81f1f

Please sign in to comment.