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

test(AstCheckerTest): Refactor isSurcharged #4119

Merged
merged 4 commits into from
Sep 20, 2021
Merged
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
62 changes: 39 additions & 23 deletions src/test/java/spoon/reflect/ast/AstCheckerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import org.junit.jupiter.api.Test;
import spoon.Launcher;
import spoon.reflect.code.CtStatement;
import spoon.reflect.declaration.CtClass;
import spoon.reflect.reference.CtExecutableReference;
import spoon.support.modelobs.FineModelChangeListener;
Expand Down Expand Up @@ -45,6 +46,7 @@
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -203,32 +205,46 @@ private boolean isNotCandidate(CtMethod<?> candidate) {
}

private boolean isSurcharged(CtMethod<?> candidate) {
CtBlock<?> block = candidate.getBody();
if (block.getStatements().isEmpty()) {
return false;
return !extractPotentialSurchargeDelegateDeclaration(candidate)
.filter(this::isToBeProcessed)
.isPresent();
}

private Optional<CtMethod<?>> extractPotentialSurchargeDelegateDeclaration(CtMethod<?> candidate) {
Optional<CtInvocation<?>> maybePotentialDelegate = extractPotentialSurchargeDelegate(candidate);
return maybePotentialDelegate
.map(CtInvocation::getExecutable)
.map(CtExecutableReference::getDeclaration)
.filter(CtMethod.class::isInstance)
.map(ref -> (CtMethod<?>) ref);
}

private Optional<CtInvocation<?>> extractPotentialSurchargeDelegate(CtMethod<?> candidate) {
final CtBlock<?> body = candidate.getBody();
if (body.getStatements().isEmpty()) {
return Optional.empty();
}
CtInvocation potentialDelegate;
if (block.getLastStatement() instanceof CtReturn) {
if (!(((CtReturn) block.getLastStatement()).getReturnedExpression() instanceof CtInvocation)) {
if (block.getStatement(0) instanceof CtInvocation) {
potentialDelegate = block.getStatement(0);
} else {
return false;
}
} else {
potentialDelegate = (CtInvocation) ((CtReturn) block.getLastStatement()).getReturnedExpression();
}
} else if (block.getStatement(0) instanceof CtInvocation && block.getStatements().size() == 1) {
potentialDelegate = block.getStatement(0);

final CtStatement firstStatement = body.getStatement(0);
final CtStatement lastStatement = body.getLastStatement();
if (firstStatement instanceof CtInvocation &&
(body.getStatements().size() == 1 || isReturnWithoutInvocation(lastStatement))) {
return Optional.of((CtInvocation<?>) firstStatement);
} else if (isReturnWithInvocation(lastStatement)) {
CtReturn<?> lastStatementReturn = (CtReturn<?>) lastStatement;
return Optional.of((CtInvocation<?>) lastStatementReturn.getReturnedExpression());
} else {
return false;
}
CtExecutable declaration = potentialDelegate.getExecutable().getDeclaration();
if (!(declaration instanceof CtMethod)) {
return false;
return Optional.empty();
}
// check if the invocation has a model change listener
return !isToBeProcessed((CtMethod<?>) declaration);
}

private boolean isReturnWithoutInvocation(CtStatement statement) {
return statement instanceof CtReturn && !isReturnWithInvocation(statement);
}

private boolean isReturnWithInvocation(CtStatement statement) {
return statement instanceof CtReturn
&& ((CtReturn<?>) statement).getReturnedExpression() instanceof CtInvocation;
}

private boolean isDelegateMethod(CtMethod<?> candidate) {
Expand Down