diff --git a/src/main/java/spoon/support/reflect/code/CtCaseImpl.java b/src/main/java/spoon/support/reflect/code/CtCaseImpl.java index 58b8cff238b..aa4f8297571 100644 --- a/src/main/java/spoon/support/reflect/code/CtCaseImpl.java +++ b/src/main/java/spoon/support/reflect/code/CtCaseImpl.java @@ -27,9 +27,6 @@ public class CtCaseImpl extends CtStatementImpl implements CtCase { private static final long serialVersionUID = 1L; - @MetamodelPropertyField(role = CtRole.EXPRESSION) - CtExpression caseExpression; - @MetamodelPropertyField(role = CtRole.EXPRESSION) List> caseExpressions = emptyList(); @@ -46,7 +43,10 @@ public void accept(CtVisitor visitor) { @Override public CtExpression getCaseExpression() { - return caseExpression; + if (caseExpressions.isEmpty()) { + return null; + } + return caseExpressions.get(0); } @Override @@ -59,8 +59,9 @@ public > T setCaseExpression(CtExpression caseExpression) if (caseExpression != null) { caseExpression.setParent(this); } - getFactory().getEnvironment().getModelChangeListener().onObjectUpdate(this, CtRole.CASE, caseExpression, this.caseExpression); - this.caseExpression = caseExpression; + this.caseExpressions = CtElementImpl.emptyList(); + getFactory().getEnvironment().getModelChangeListener().onObjectUpdate(this, CtRole.CASE, caseExpression, this.caseExpressions); + addCaseExpression(caseExpression); return (T) this; } @@ -89,9 +90,6 @@ public > T addCaseExpression(CtExpression caseExpression) return (T) this; } this.ensureModifiableCaseExpressionsList(); - if (getCaseExpression() == null) { - setCaseExpression(caseExpression); - } getFactory().getEnvironment().getModelChangeListener().onObjectUpdate(this, CtRole.CASE, caseExpressions, this.caseExpressions); caseExpression.setParent(this); this.caseExpressions.add(caseExpression); diff --git a/src/test/java/spoon/test/model/SwitchCaseTest.java b/src/test/java/spoon/test/model/SwitchCaseTest.java index 69cb63db14a..9dd035d194a 100644 --- a/src/test/java/spoon/test/model/SwitchCaseTest.java +++ b/src/test/java/spoon/test/model/SwitchCaseTest.java @@ -8,6 +8,8 @@ package spoon.test.model; +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; import static org.junit.jupiter.api.Assertions.assertAll; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; @@ -46,6 +48,7 @@ import spoon.reflect.factory.Factory; import spoon.reflect.visitor.filter.TypeFilter; import spoon.support.compiler.VirtualFile; + @DisplayName("Switchcase Tests") public class SwitchCaseTest { @@ -365,4 +368,55 @@ public void test_addCaseAt_throwsIndexOutOfBoundsException_whenPositionIsOutOfBo assertThrows(IndexOutOfBoundsException.class, () -> switchExpression.addCaseAt(3, onlyCase)); } } + + @Nested + class CaseExpressionInSwitchCasesBeforeJava12 { + @Test + public void test_getCaseExpression_shouldReturnTheFirstCaseExpression() { + // contract: `getCaseExpression` should return the first element in the list of case expressions stored in + // each instance of `CtCase` + Factory factory = new Launcher().getFactory(); + + CtCase ctCase = factory.createCase(); + CtExpression first = factory.createLiteral(1); + CtExpression second = factory.createLiteral(2); + CtExpression third = factory.createLiteral(3); + + ctCase.setCaseExpressions(Arrays.asList(first, second, third)); + + assertEquals(first, ctCase.getCaseExpression()); + } + + @Test + public void test_setCaseExpression_removeExistingCaseExpressionsAndInsertTheSpecified() { + // contract: `setCaseExpression` should clear the list of case expressions and insert the specified case + // expression + Factory factory = new Launcher().getFactory(); + + CtCase ctCase = factory.createCase(); + CtExpression first = factory.createLiteral(1); + CtExpression second = factory.createLiteral(2); + CtExpression third = factory.createLiteral(3); + CtExpression fourth = factory.createLiteral(4); + ctCase.setCaseExpressions(Arrays.asList(first, second, third)); + + ctCase.setCaseExpression(fourth); + + assertEquals(1, ctCase.getCaseExpressions().size()); + assertEquals(fourth, ctCase.getCaseExpression()); + } + + @Test + public void test_setCaseExpression_removeAllCaseExpressions() { + // contract: `setCaseExpression` should clear the list of case expressions when `null` is passed as + // argument + String code = "class A { public void f(int i) { switch(i) { case 1,2,3: break; } } }"; + CtModel model = createModelFromString(code); + CtCase ctCase = model.getElements(new TypeFilter<>(CtCase.class)).get(0); + + ctCase.setCaseExpression(null); + + assertThat(ctCase.getCaseExpressions().size(), equalTo(0)); + } + } }