]> git.uio.no Git - ifi-stolz-refaktor.git/commitdiff
TextSelectionsGenerator: now handling switch statements
authorErlend Kristiansen <erlenkr@ifi.uio.no>
Fri, 28 Feb 2014 11:53:51 +0000 (12:53 +0100)
committerErlend Kristiansen <erlenkr@ifi.uio.no>
Fri, 28 Feb 2014 11:53:51 +0000 (12:53 +0100)
software/no.uio.ifi.refaktor.examples/examples/src/selectionsGenerator/Main.java
software/no.uio.ifi.refaktor.tests/src/no/uio/ifi/refaktor/tests/TextSelectionsGeneratorTest.java
software/no.uio.ifi.refaktor/src/no/uio/ifi/refaktor/analyze/analyzers/StatementListsCreator.java

index 2487370614ee79c592ef3830b7074689dee3e1af..e400d56d6cc08e2b8a8e7959817bc7d2e463f706 100644 (file)
@@ -84,6 +84,8 @@ public class Main {
                case 42:
                        System.out.println("Correct!");
                        break;
+               case 666:
+                       System.out.println("This is not the answer!");
                default:
                        System.out.println("Wrong...");
                }
index 0a992b3e665eb07b7d958c71bdde49776fe78680..f6794ee2037b7724ee85165f38b360404a6f713d 100644 (file)
@@ -73,6 +73,11 @@ public class TextSelectionsGeneratorTest {
                assertThatMethodContainsSelections("containingLabel", new S(53, 28), new S(84, 16), new S(53, 47), new S(65, 12), new S(92, 8));
        }
 
+       @Test
+       public void testContainingSwitch() throws Exception {
+               assertThatMethodContainsSelections("containingSwitch", new S(54, 188), new S(89, 41), new S(89, 31), new S(146, 46), new S(207, 31), new S(124, 6));
+       }
+
        @Test
        public void testContainingWhile() throws Exception {
                assertThatMethodContainsSelections("containingWhile", new S(53, 34), new S(91, 28), new S(53, 66), new S(71, 12), new S(107, 12));
index 48840e70a1772a64b8b5f287d57b70c36230e585..2d83a15b46a2b55f78bc9d0f859dd50a7d48f9c6 100644 (file)
@@ -22,6 +22,8 @@ import org.eclipse.jdt.core.dom.IfStatement;
 import org.eclipse.jdt.core.dom.LabeledStatement;
 import org.eclipse.jdt.core.dom.MethodDeclaration;
 import org.eclipse.jdt.core.dom.Statement;
+import org.eclipse.jdt.core.dom.SwitchCase;
+import org.eclipse.jdt.core.dom.SwitchStatement;
 import org.eclipse.jdt.core.dom.WhileStatement;
 import org.eclipse.jdt.internal.corext.refactoring.structure.ASTNodeSearchUtil;
 
@@ -76,55 +78,77 @@ public class StatementListsCreator extends ASTVisitor {
                        size += list.size();
                return size;
        }
-       
+
        @Override
        public boolean visit(Block node) {
                statementLists.add(new LinkedList<Statement>(node.statements()));
                return true;
        }
 
-       private void addIfNotBlockOrNull(Statement body) {
-               if (!(body instanceof Block) && body != null)
-                       statementLists.add(new LinkedList<Statement>(Arrays.asList(body)));
-       }
-       
        @Override
        public boolean visit(DoStatement node) {
                addIfNotBlockOrNull(node.getBody());
                return true;
        }
-       
+
        @Override
        public boolean visit(EnhancedForStatement node) {
                addIfNotBlockOrNull(node.getBody());
                return true;
        }
-       
+
        @Override
        public boolean visit(ForStatement node) {
                addIfNotBlockOrNull(node.getBody());
                return true;
        }
-       
+
        @Override
        public boolean visit(IfStatement node) {
                addIfNotBlockOrNull(node.getThenStatement());
                addIfNotBlockOrNull(node.getElseStatement());
                return true;
        }
-       
+
        @Override
        public boolean visit(LabeledStatement node) {
                addIfNotBlockOrNull(node.getBody());
                return true;
        }
-       
-       //TODO: switch statements
-       
+
+       @Override
+       public boolean visit(SwitchStatement node) {
+               List<Statement> statementList = new LinkedList<Statement>();
+
+               for (Object stmtObject: node.statements()) {
+                       assert stmtObject instanceof Statement;
+                       Statement statement = (Statement) stmtObject;
+
+                       if (statement instanceof SwitchCase) {
+                               if (!statementList.isEmpty()) {
+                                       statementLists.add(statementList);
+                                       statementList = new LinkedList<Statement>();
+                               }
+                       } else {
+                               statementList.add(statement);
+                       }
+               }
+               
+               if (!statementList.isEmpty())
+                       statementLists.add(statementList);
+
+               return true;
+       }
+
        @Override
        public boolean visit(WhileStatement node) {
                addIfNotBlockOrNull(node.getBody());
                return true;
        }
-       
+
+       private void addIfNotBlockOrNull(Statement body) {
+               if (!(body instanceof Block) && body != null)
+                       statementLists.add(new LinkedList<Statement>(Arrays.asList(body)));
+       }
+
 }
\ No newline at end of file