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

review: fix: search in previous cases for variable declarations #4697

Merged
merged 1 commit into from
Apr 22, 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
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@
*/
package spoon.reflect.visitor.filter;

import spoon.reflect.code.CaseKind;
import spoon.reflect.code.CtBodyHolder;
import spoon.reflect.code.CtCase;
import spoon.reflect.code.CtCatch;
import spoon.reflect.code.CtCatchVariable;
import spoon.reflect.code.CtLocalVariable;
import spoon.reflect.code.CtStatement;
import spoon.reflect.code.CtStatementList;
import spoon.reflect.code.CtSwitch;
import spoon.reflect.declaration.CtElement;
import spoon.reflect.declaration.CtExecutable;
import spoon.reflect.declaration.CtField;
Expand All @@ -27,6 +31,8 @@
import spoon.reflect.visitor.chain.CtQuery;
import spoon.reflect.visitor.chain.CtQueryAware;

import java.util.List;

/**
* This mapping function searches for all {@link CtVariable} instances,
* which might be a declaration of an input {@link CtElement}.
Expand Down Expand Up @@ -114,6 +120,23 @@ public void apply(CtElement input, CtConsumer<Object> outputConsumer) {
if (query.isTerminated()) {
return;
}
} else if (parent instanceof CtSwitch
&& scopeElement instanceof CtCase && ((CtCase<?>) scopeElement).getCaseKind() == CaseKind.COLON) {
SiblingsFunction siblingsFunction = new SiblingsFunction().mode(SiblingsFunction.Mode.PREVIOUS);
List<CtCase<?>> list = input.getFactory().createQuery()
.map(siblingsFunction)
.setInput(scopeElement)
.filterChildren(new TypeFilter<>(CtCase.class))
.list();

for (CtCase<?> c : list) {
for (CtStatement statement : c.getStatements()) {
if (statement instanceof CtLocalVariable && ((CtLocalVariable<?>) statement).getSimpleName().equals(variableName)) {
sendToOutput((CtVariable<?>) statement, outputConsumer);
return;
}
}
}
} else if (parent instanceof CtBodyHolder || parent instanceof CtStatementList) {
//visit all previous CtVariable siblings of scopeElement element in parent BodyHolder or Statement list
siblingsQuery.setInput(scopeElement).forEach(outputConsumer);
Expand Down
33 changes: 33 additions & 0 deletions src/test/java/spoon/test/model/SwitchCaseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
Expand All @@ -41,13 +42,16 @@
import spoon.reflect.code.CtStatement;
import spoon.reflect.code.CtSwitch;
import spoon.reflect.code.CtSwitchExpression;
import spoon.reflect.code.CtVariableAccess;
import spoon.reflect.code.CtYieldStatement;
import spoon.reflect.declaration.CtClass;
import spoon.reflect.declaration.CtElement;
import spoon.reflect.declaration.CtMethod;
import spoon.reflect.declaration.CtVariable;
import spoon.reflect.factory.Factory;
import spoon.reflect.visitor.filter.TypeFilter;
import spoon.support.compiler.VirtualFile;
import spoon.test.GitHubIssue;

@DisplayName("Switchcase Tests")
public class SwitchCaseTest {
Expand Down Expand Up @@ -229,6 +233,35 @@ public void testSwitchColons() {
);
}

@GitHubIssue(issueNumber = 4696, fixed = true)
void testVariableScopeInSwitch() {
// contract: different cases do not introduce different scopes in colon-switches
String code = "public class A {\n" +
" public void function(int value) {\n" +
" switch (value) {\n" +
" case 1: { String test; }\n" + // decoy variable declaration, should not be found
" String test;\n" +
" test = \"first\";\n" +
" break;\n" +
" default:\n" +
" test = \"not first\";\n" +
" }\n" +
" }\n" +
"}";
CtModel model = createModelFromString(code);
List<CtVariableAccess<?>> accesses = model.<CtVariableAccess<?>>getElements(new TypeFilter<>(CtVariableAccess.class))
.stream()
.filter(a -> a.getVariable().getSimpleName().equals("test"))
.collect(Collectors.<CtVariableAccess<?>>toList());
assertEquals(2, accesses.size());

// access their declarations
CtVariable<?> caseOneVariableDeclaration = accesses.get(0).getVariable().getDeclaration();
CtVariable<?> defaultVariableDeclaration = accesses.get(1).getVariable().getDeclaration();

// expect the declarations to be equal (writing to the same variable)
assertEquals(caseOneVariableDeclaration, defaultVariableDeclaration);
}
}

@Nested
Expand Down