Skip to content

Commit

Permalink
fix: handle empty modifers in CodeFactory#createCatchVariable correct…
Browse files Browse the repository at this point in the history
…ly (#4957)
  • Loading branch information
MartinWitt authored Oct 14, 2022
1 parent 74ee133 commit 0da6e33
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
9 changes: 7 additions & 2 deletions src/main/java/spoon/reflect/factory/CodeFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -354,8 +354,13 @@ public <T> CtLocalVariableReference<T> createLocalVariableReference(CtTypeRefere
* Modifiers of the catch variable
* @return a new catch variable declaration
*/
public <T> CtCatchVariable<T> createCatchVariable(CtTypeReference<T> type, String name, ModifierKind...modifierKinds) {
return factory.Core().<T>createCatchVariable().<CtCatchVariable<T>>setSimpleName(name).<CtCatchVariable<T>>setType(type).setModifiers(EnumSet.copyOf(Arrays.asList(modifierKinds)));
public <T> CtCatchVariable<T> createCatchVariable(CtTypeReference<T> type, String name, ModifierKind... modifierKinds) {
EnumSet<ModifierKind> modifiers = EnumSet.noneOf(ModifierKind.class);
modifiers.addAll(Arrays.asList(modifierKinds));
return factory.Core().<T>createCatchVariable()
.<CtCatchVariable<T>>setSimpleName(name)
.<CtCatchVariable<T>>setType(type)
.setModifiers(modifiers);
}

/**
Expand Down
12 changes: 10 additions & 2 deletions src/test/java/spoon/test/factory/CodeFactoryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@
import spoon.reflect.declaration.ModifierKind;
import spoon.reflect.factory.Factory;
import spoon.reflect.reference.CtTypeReference;

import spoon.test.GitHubIssue;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static spoon.testing.utils.ModelUtils.createFactory;

public class CodeFactoryTest {
int i;

@Test
public void testThisAccess() {
Expand All @@ -58,4 +58,12 @@ public void testCreateVariableAssignement() {
assertTrue(va.getAssigned() instanceof CtVariableWrite);
assertEquals(f.getReference(), ((CtVariableWrite) va.getAssigned()).getVariable());
}

@GitHubIssue(issueNumber= 4956, fixed = true)
void createCtCatcVariablehWithoutModifiers() {
// contract: CtCatchVariable without modifiers is created. This a test for the regression of #4940
Factory factory = createFactory();
CtTypeReference<Exception> exceptionType = factory.Type().createReference(Exception.class);
assertDoesNotThrow(() -> factory.Code().createCatchVariable(exceptionType, "e"));
}
}

0 comments on commit 0da6e33

Please sign in to comment.