Skip to content

Commit

Permalink
fix(CtCase): add fix for missing parents (#3695)
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinWitt authored Nov 14, 2020
1 parent 55bdd96 commit c970571
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/main/java/spoon/support/reflect/code/CtCaseImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ public <T extends CtCase<E>> T addCaseExpression(CtExpression<E> caseExpression)
setCaseExpression(caseExpression);
}
getFactory().getEnvironment().getModelChangeListener().onObjectUpdate(this, CtRole.CASE, caseExpressions, this.caseExpressions);
caseExpression.setParent(this);
this.caseExpressions.add(caseExpression);
return (T) this;
}
Expand Down
11 changes: 11 additions & 0 deletions src/test/java/spoon/test/model/SwitchCaseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,17 @@ public void testSwitchStatementOnAString() throws Exception {
assertEquals(String.class.getName(), aCase.getCaseExpression().getType().getTypeDeclaration().getQualifiedName());
}
}
@DisplayName("Parent is set")
@Test
public void testParentInCaseExpressions() {
// contract: for all expressions in a case, the parent is set.
Launcher launcher = new Launcher();
launcher.getEnvironment().setComplianceLevel(14);
launcher.addInputResource(new VirtualFile("class A { { int x = switch(1) { case 1, 3 -> 0; default -> 1}; } }"));
launcher.buildModel();
List<CtLiteral<?>> caseStatement = launcher.getModel().getElements(new TypeFilter<>(CtLiteral.class));
assertTrue(caseStatement.stream().allMatch(CtLiteral::isParentInitialized));
}
}


Expand Down

0 comments on commit c970571

Please sign in to comment.