Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle empty modifers in CodeFactory#createCatchVariable correctly #4957

Merged
merged 1 commit into from
Oct 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"));
}
}