Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNoopur Gupta2019-04-16 10:05:32 +0000
committerNoopur Gupta2019-04-16 10:05:32 +0000
commitb938bef6791070b69196456372befc51ea8388b9 (patch)
tree58a1535cf5e1b278485be7d655be90d3ce9d8b2c
parentf14ed757d58a610e6a9702ed7849da22563f6258 (diff)
downloadeclipse.jdt.ui-b938bef6791070b69196456372befc51ea8388b9.tar.gz
eclipse.jdt.ui-b938bef6791070b69196456372befc51ea8388b9.tar.xz
eclipse.jdt.ui-b938bef6791070b69196456372befc51ea8388b9.zip
Bug 545112: [12][extract method] Incorrect extract of switch case node
-rw-r--r--org.eclipse.jdt.core.manipulation/common/org/eclipse/jdt/internal/core/manipulation/JavaManipulationMessages.java3
-rw-r--r--org.eclipse.jdt.core.manipulation/common/org/eclipse/jdt/internal/core/manipulation/JavaManipulationMessages.properties3
-rw-r--r--org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/refactoring/util/StatementAnalyzer.java29
3 files changed, 30 insertions, 5 deletions
diff --git a/org.eclipse.jdt.core.manipulation/common/org/eclipse/jdt/internal/core/manipulation/JavaManipulationMessages.java b/org.eclipse.jdt.core.manipulation/common/org/eclipse/jdt/internal/core/manipulation/JavaManipulationMessages.java
index 7c3f00cefe..77ea419177 100644
--- a/org.eclipse.jdt.core.manipulation/common/org/eclipse/jdt/internal/core/manipulation/JavaManipulationMessages.java
+++ b/org.eclipse.jdt.core.manipulation/common/org/eclipse/jdt/internal/core/manipulation/JavaManipulationMessages.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2000, 2018 IBM Corporation and others.
+ * Copyright (c) 2000, 2019 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
@@ -48,6 +48,7 @@ public class JavaManipulationMessages extends NLS {
public static String StatementAnalyzer_for_expression_updater;
public static String StatementAnalyzer_for_updater_body;
public static String StatementAnalyzer_switch_statement;
+ public static String StatementAnalyzer_switch_expression;
public static String StatementAnalyzer_synchronized_statement;
public static String StatementAnalyzer_try_statement;
public static String StatementAnalyzer_catch_argument;
diff --git a/org.eclipse.jdt.core.manipulation/common/org/eclipse/jdt/internal/core/manipulation/JavaManipulationMessages.properties b/org.eclipse.jdt.core.manipulation/common/org/eclipse/jdt/internal/core/manipulation/JavaManipulationMessages.properties
index 5314fc883f..e2d882b1bf 100644
--- a/org.eclipse.jdt.core.manipulation/common/org/eclipse/jdt/internal/core/manipulation/JavaManipulationMessages.properties
+++ b/org.eclipse.jdt.core.manipulation/common/org/eclipse/jdt/internal/core/manipulation/JavaManipulationMessages.properties
@@ -1,5 +1,5 @@
###############################################################################
-# Copyright (c) 2000, 2018 IBM Corporation and others.
+# Copyright (c) 2000, 2019 IBM Corporation and others.
#
# This program and the accompanying materials
# are made available under the terms of the Eclipse Public License 2.0
@@ -35,6 +35,7 @@ StatementAnalyzer_catch_argument=Operation is not applicable to a catch block's
StatementAnalyzer_while_expression_body=Operation not applicable to a while statement's expression and body.
StatementAnalyzer_try_statement=Selection must either cover whole try statement or parts of try, catch, or finally block.
StatementAnalyzer_switch_statement=Selection must either cover whole switch statement or parts of a single case block.
+StatementAnalyzer_switch_expression=Selection must either cover whole switch expression or parts of a single case block.
StatementAnalyzer_synchronized_statement=Selection must either cover whole synchronized statement or parts of the synchronized block.
SurroundWithTryCatchAnalyzer_doesNotCover=Selection does not cover a set of statements. Extend selection to a valid range using the \'Expand Selection To\' actions from the \'Edit\' menu.
diff --git a/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/refactoring/util/StatementAnalyzer.java b/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/refactoring/util/StatementAnalyzer.java
index 4395da868d..3ca9ffef33 100644
--- a/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/refactoring/util/StatementAnalyzer.java
+++ b/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/refactoring/util/StatementAnalyzer.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2000, 2018 IBM Corporation and others.
+ * Copyright (c) 2000, 2019 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
@@ -35,6 +35,7 @@ import org.eclipse.jdt.core.dom.Expression;
import org.eclipse.jdt.core.dom.ForStatement;
import org.eclipse.jdt.core.dom.Statement;
import org.eclipse.jdt.core.dom.SwitchCase;
+import org.eclipse.jdt.core.dom.SwitchExpression;
import org.eclipse.jdt.core.dom.SwitchStatement;
import org.eclipse.jdt.core.dom.SynchronizedStatement;
import org.eclipse.jdt.core.dom.TryStatement;
@@ -174,6 +175,22 @@ public class StatementAnalyzer extends SelectionAnalyzer {
}
@Override
+ public void endVisit(SwitchExpression node) {
+ ASTNode[] selectedNodes= getSelectedNodes();
+ if (doAfterValidation(node, selectedNodes)) {
+ List<SwitchCase> cases= getSwitchCases(node);
+ for (int i= 0; i < selectedNodes.length; i++) {
+ ASTNode topNode= selectedNodes[i];
+ if (cases.contains(topNode)) {
+ invalidSelection(JavaManipulationMessages.StatementAnalyzer_switch_expression);
+ break;
+ }
+ }
+ }
+ super.endVisit(node);
+ }
+
+ @Override
public void endVisit(SynchronizedStatement node) {
ASTNode firstSelectedNode= getFirstSelectedNode();
if (getSelection().getEndVisitSelectionMode(node) == Selection.SELECTED) {
@@ -230,9 +247,15 @@ public class StatementAnalyzer extends SelectionAnalyzer {
reset();
}
- private static List<SwitchCase> getSwitchCases(SwitchStatement node) {
+ private static List<SwitchCase> getSwitchCases(ASTNode node) {
List<SwitchCase> result= new ArrayList<>();
- for (Iterator<Statement> iter= node.statements().iterator(); iter.hasNext(); ) {
+ List<Statement> statements= new ArrayList<>();
+ if (node instanceof SwitchStatement) {
+ statements= ((SwitchStatement) node).statements();
+ } else if (node instanceof SwitchExpression) {
+ statements= ((SwitchExpression) node).statements();
+ }
+ for (Iterator<Statement> iter= statements.iterator(); iter.hasNext();) {
Object element= iter.next();
if (element instanceof SwitchCase)
result.add((SwitchCase) element);

Back to the top