From ebd12bdabb26c7158bbf0052b0a8956a9c976218 Mon Sep 17 00:00:00 2001 From: Martin Wittlinger Date: Wed, 12 Oct 2022 13:49:28 +0200 Subject: [PATCH] fix: handle empty modifers in CodeFactory#createCatchVariable correctly --- src/main/java/spoon/reflect/factory/CodeFactory.java | 9 +++++++-- .../java/spoon/test/factory/CodeFactoryTest.java | 12 ++++++++++-- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/main/java/spoon/reflect/factory/CodeFactory.java b/src/main/java/spoon/reflect/factory/CodeFactory.java index 1a471397c58..e9ed1e5eab9 100644 --- a/src/main/java/spoon/reflect/factory/CodeFactory.java +++ b/src/main/java/spoon/reflect/factory/CodeFactory.java @@ -354,8 +354,13 @@ public CtLocalVariableReference createLocalVariableReference(CtTypeRefere * Modifiers of the catch variable * @return a new catch variable declaration */ - public CtCatchVariable createCatchVariable(CtTypeReference type, String name, ModifierKind...modifierKinds) { - return factory.Core().createCatchVariable().>setSimpleName(name).>setType(type).setModifiers(EnumSet.copyOf(Arrays.asList(modifierKinds))); + public CtCatchVariable createCatchVariable(CtTypeReference type, String name, ModifierKind... modifierKinds) { + EnumSet modifiers = EnumSet.noneOf(ModifierKind.class); + modifiers.addAll(Arrays.asList(modifierKinds)); + return factory.Core().createCatchVariable() + .>setSimpleName(name) + .>setType(type) + .setModifiers(modifiers); } /** diff --git a/src/test/java/spoon/test/factory/CodeFactoryTest.java b/src/test/java/spoon/test/factory/CodeFactoryTest.java index 7f447566786..39da34cfd4e 100644 --- a/src/test/java/spoon/test/factory/CodeFactoryTest.java +++ b/src/test/java/spoon/test/factory/CodeFactoryTest.java @@ -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() { @@ -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 exceptionType = factory.Type().createReference(Exception.class); + assertDoesNotThrow(() -> factory.Code().createCatchVariable(exceptionType, "e")); + } }