-
-
Notifications
You must be signed in to change notification settings - Fork 351
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactors CtCase: now extends CtStatementList
one can thus write: for (CtStatement s : case) { l.add(s); }
- Loading branch information
Showing
4 changed files
with
97 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package spoon.test.model; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import org.junit.Test; | ||
|
||
import spoon.reflect.code.CtCase; | ||
import spoon.reflect.code.CtLiteral; | ||
import spoon.reflect.code.CtStatement; | ||
import spoon.reflect.code.CtSwitch; | ||
import spoon.reflect.declaration.CtClass; | ||
import spoon.reflect.declaration.CtMethod; | ||
import spoon.reflect.factory.Factory; | ||
import spoon.reflect.visitor.filter.TypeFilter; | ||
import spoon.test.TestUtils; | ||
|
||
public class SwitchCaseTest { | ||
|
||
@Test | ||
public void testIterationStatements() { | ||
Factory factory = TestUtils.createFactory(); | ||
CtClass<?> clazz = factory | ||
.Code() | ||
.createCodeSnippetStatement( | ||
"" + "class X {" + "public void foo() {" + " int x=0;" + | ||
"switch(x) {" | ||
+ "case 0: x=x+1;break;" | ||
+ "case 1: x=0;" | ||
+ "default: x=-1;" | ||
+ "}" | ||
+ "}};") | ||
.compile(); | ||
CtMethod<?> foo = (CtMethod<?>) clazz.getMethods().toArray()[0]; | ||
|
||
CtSwitch sw = foo.getElements(new TypeFilter<CtSwitch>(CtSwitch.class)).get(0); | ||
|
||
assertEquals(3, sw.getCases().size()); | ||
|
||
CtCase c = (CtCase) sw.getCases().get(0); | ||
|
||
assertEquals(0, ((CtLiteral)c.getCaseExpression()).getValue()); | ||
assertEquals(2, c.getStatements().size()); | ||
|
||
List l = new ArrayList(); | ||
|
||
// this compiles (thanks to the new CtCase extends CtStatementList) | ||
for (CtStatement s : c) { | ||
l.add(s); | ||
} | ||
assertTrue(c.getStatements().equals(l)); | ||
} | ||
|
||
} |
eab464a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This does not compile for me on Oracle JDK 1.7 (Windows), Oracle JDK 1.8 (Windows) and OpenJDK 1.7 (Debian). It seems like the compiler fails to detect the generic type of the Iterable implementation. Which compiler did you use to build this?
eab464a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The offending line is line 51: The compiler detects Iterable even though it's Iterable
eab464a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems like the only compiler that is able to handle this is the Eclipse compiler. Probably not a good idea to depend in that.
eab464a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's strange. Fixed but I don't understand the deep causes of the problem.