Skip to content

Commit

Permalink
finish splitting up SpoonUtil #524
Browse files Browse the repository at this point in the history
  • Loading branch information
Luro02 committed Jul 23, 2024
1 parent ac5a499 commit 1ced32e
Show file tree
Hide file tree
Showing 88 changed files with 1,430 additions and 1,362 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
import de.firemage.autograder.core.ProblemType;
import de.firemage.autograder.core.check.ExecutableCheck;
import de.firemage.autograder.core.integrated.CtRange;
import de.firemage.autograder.core.integrated.ExpressionUtil;
import de.firemage.autograder.core.integrated.FactoryUtil;
import de.firemage.autograder.core.integrated.IntegratedCheck;
import de.firemage.autograder.core.integrated.SpoonUtil;
import de.firemage.autograder.core.integrated.StaticAnalysis;
import de.firemage.autograder.core.integrated.TypeUtil;
import org.apache.commons.lang3.Range;
Expand Down Expand Up @@ -40,35 +41,35 @@ default CtExpression<R> suggest(CtExpression<T> ctExpression) {

private static final Map<Range<Character>, Suggester<Character, Boolean>> MAPPING = Map.of(
Range.of('a', 'z'), (factory, ctExpression, targetType) -> factory.createBinaryOperator(
SpoonUtil.createStaticInvocation(
FactoryUtil.createStaticInvocation(
targetType,
"isAlphabetic",
SpoonUtil.castExpression(int.class, ctExpression)
ExpressionUtil.castExpression(int.class, ctExpression)
),
SpoonUtil.createStaticInvocation(
FactoryUtil.createStaticInvocation(
targetType,
"isLowerCase",
SpoonUtil.castExpression(char.class, ctExpression)
ExpressionUtil.castExpression(char.class, ctExpression)
),
BinaryOperatorKind.AND
),
Range.of('A', 'Z'), (factory, ctExpression, targetType) -> factory.createBinaryOperator(
SpoonUtil.createStaticInvocation(
FactoryUtil.createStaticInvocation(
targetType,
"isAlphabetic",
SpoonUtil.castExpression(int.class, ctExpression)
ExpressionUtil.castExpression(int.class, ctExpression)
),
SpoonUtil.createStaticInvocation(
FactoryUtil.createStaticInvocation(
targetType,
"isUpperCase",
SpoonUtil.castExpression(char.class, ctExpression)
ExpressionUtil.castExpression(char.class, ctExpression)
),
BinaryOperatorKind.AND
),
Range.of('0', '9'), (factory, ctExpression, targetType) -> SpoonUtil.createStaticInvocation(
Range.of('0', '9'), (factory, ctExpression, targetType) -> FactoryUtil.createStaticInvocation(
targetType,
"isDigit",
SpoonUtil.castExpression(char.class, ctExpression)
ExpressionUtil.castExpression(char.class, ctExpression)
)
);

Expand Down Expand Up @@ -96,7 +97,7 @@ public void process(CtBinaryOperator<Boolean> ctBinaryOperator) {
CtBinaryOperator<Boolean> operator = ctBinaryOperator;
if (ctBinaryOperator.getKind() == BinaryOperatorKind.OR) {
isNegated = true;
operator = (CtBinaryOperator<Boolean>) SpoonUtil.negate(ctBinaryOperator);
operator = (CtBinaryOperator<Boolean>) ExpressionUtil.negate(ctBinaryOperator);
} else {
isNegated = false;
if (ctBinaryOperator.getKind() != BinaryOperatorKind.AND) {
Expand Down Expand Up @@ -133,7 +134,7 @@ public void process(CtBinaryOperator<Boolean> ctBinaryOperator) {

makeSuggestion(leftRange.ctExpression(), intersection).ifPresent(suggestion -> {
if (isNegated) {
suggestion = SpoonUtil.negate(suggestion);
suggestion = ExpressionUtil.negate(suggestion);
}

addLocalProblem(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import de.firemage.autograder.core.ProblemType;
import de.firemage.autograder.core.check.ExecutableCheck;
import de.firemage.autograder.core.integrated.IntegratedCheck;
import de.firemage.autograder.core.integrated.SpoonUtil;
import de.firemage.autograder.core.integrated.StatementUtil;
import de.firemage.autograder.core.integrated.StaticAnalysis;
import de.firemage.autograder.core.integrated.TypeUtil;
import spoon.processing.AbstractProcessor;
Expand Down Expand Up @@ -54,7 +54,7 @@ public void process(CtForEach ctForEach) {
return;
}

List<CtStatement> statements = SpoonUtil.getEffectiveStatements(ctForEach.getBody());
List<CtStatement> statements = StatementUtil.getEffectiveStatements(ctForEach.getBody());
if (statements.size() != 1 || !(statements.get(0) instanceof CtIf ctIf)) {
return;
}
Expand All @@ -64,7 +64,7 @@ public void process(CtForEach ctForEach) {
return;
}

List<CtStatement> ifStatements = SpoonUtil.getEffectiveStatements(ctIf.getThenStatement());
List<CtStatement> ifStatements = StatementUtil.getEffectiveStatements(ctIf.getThenStatement());
if (ifStatements.isEmpty()) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import de.firemage.autograder.core.check.ExecutableCheck;
import de.firemage.autograder.core.check.api.UseEnumValues.CtEnumFieldRead;
import de.firemage.autograder.core.integrated.IntegratedCheck;
import de.firemage.autograder.core.integrated.SpoonUtil;
import de.firemage.autograder.core.integrated.StatementUtil;
import de.firemage.autograder.core.integrated.StaticAnalysis;
import de.firemage.autograder.core.integrated.MethodUtil;
import de.firemage.autograder.core.integrated.TypeUtil;
Expand Down Expand Up @@ -138,7 +138,7 @@ private void reportProblem(CtVariable<?> ctVariable, List<? extends CtExpression
}

private void checkAddAll(CtBlock<?> ctBlock) {
List<CtStatement> statements = SpoonUtil.getEffectiveStatements(ctBlock);
List<CtStatement> statements = StatementUtil.getEffectiveStatements(ctBlock);

CtVariableReference<?> collection = null;
List<CtExpression<?>> addedValues = new ArrayList<>();
Expand Down Expand Up @@ -179,7 +179,7 @@ private void checkAddAll(CtBlock<?> ctBlock) {
}

private void checkAddAll(CtForEach ctFor) {
List<CtStatement> statements = SpoonUtil.getEffectiveStatements(ctFor.getBody());
List<CtStatement> statements = StatementUtil.getEffectiveStatements(ctFor.getBody());
if (statements.size() != 1) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import de.firemage.autograder.core.check.ExecutableCheck;
import de.firemage.autograder.core.integrated.ForLoopRange;
import de.firemage.autograder.core.integrated.IntegratedCheck;
import de.firemage.autograder.core.integrated.SpoonUtil;
import de.firemage.autograder.core.integrated.StatementUtil;
import de.firemage.autograder.core.integrated.StaticAnalysis;
import de.firemage.autograder.core.integrated.TypeUtil;
import spoon.processing.AbstractProcessor;
Expand All @@ -32,7 +32,7 @@ public void process(CtFor ctFor) {

ForLoopRange forLoopRange = ForLoopRange.fromCtFor(ctFor).orElse(null);

List<CtStatement> statements = SpoonUtil.getEffectiveStatements(ctFor.getBody());
List<CtStatement> statements = StatementUtil.getEffectiveStatements(ctFor.getBody());

if (statements.size() != 1
|| forLoopRange == null
Expand All @@ -50,7 +50,7 @@ public void process(CtFor ctFor) {
}

CtExpression<?> rhs = ctInvocation.getArguments().get(0);
if (!SpoonUtil.isImmutable(rhs.getType())) {
if (!TypeUtil.isImmutable(rhs.getType())) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import de.firemage.autograder.core.LocalizedMessage;
import de.firemage.autograder.core.ProblemType;
import de.firemage.autograder.core.check.ExecutableCheck;
import de.firemage.autograder.core.integrated.ExpressionUtil;
import de.firemage.autograder.core.integrated.IntegratedCheck;
import de.firemage.autograder.core.integrated.SpoonUtil;
import de.firemage.autograder.core.integrated.StaticAnalysis;
import de.firemage.autograder.core.integrated.MethodUtil;
import de.firemage.autograder.core.integrated.TypeUtil;
Expand Down Expand Up @@ -71,7 +71,7 @@ private static CtExpression<?> buildIsEmptySuggestion(CtExpression<?> target) {
private void checkIsEmptyReimplementation(CtExpression<?> target, CtBinaryOperator<?> ctBinaryOperator, ProblemType problemType) {
Predicate<? super CtExpression<?>> isLiteral = expr -> expr instanceof CtLiteral<?>;

if (!SpoonUtil.isBoolean(ctBinaryOperator)) {
if (!ExpressionUtil.isBoolean(ctBinaryOperator)) {
return;
}

Expand Down Expand Up @@ -117,12 +117,12 @@ private void checkIsEmptyReimplementation(CtExpression<?> target, CtBinaryOperat
// f() == 0 : isEmpty


CtBinaryOperator<?> result = SpoonUtil.normalizeBy(
(left, right) -> isLiteral.test(SpoonUtil.resolveConstant(left)) && !isLiteral.test(SpoonUtil.resolveConstant(right)),
CtBinaryOperator<?> result = ExpressionUtil.normalizeBy(
(left, right) -> isLiteral.test(ExpressionUtil.resolveConstant(left)) && !isLiteral.test(ExpressionUtil.resolveConstant(right)),
ctBinaryOperator
);

if (!(result.getRightHandOperand() instanceof CtLiteral<?> ctLiteral) || !(ctLiteral.getValue() instanceof Number number) || SpoonUtil.isNullLiteral(ctLiteral)) {
if (!(result.getRightHandOperand() instanceof CtLiteral<?> ctLiteral) || !(ctLiteral.getValue() instanceof Number number) || ExpressionUtil.isNullLiteral(ctLiteral)) {
return;
}

Expand All @@ -142,13 +142,13 @@ private void checkIsEmptyReimplementation(CtExpression<?> target, CtBinaryOperat
// f() != 0 : !isEmpty
case NE -> {
if (isZero) {
this.reportProblem(ctBinaryOperator, ctBinaryOperator.toString(), SpoonUtil.negate(suggestion).toString(), problemType);
this.reportProblem(ctBinaryOperator, ctBinaryOperator.toString(), ExpressionUtil.negate(suggestion).toString(), problemType);
}
}
// f() >= 1 : !isEmpty
case GE -> {
if (isOne) {
this.reportProblem(ctBinaryOperator, ctBinaryOperator.toString(), SpoonUtil.negate(suggestion).toString(), problemType);
this.reportProblem(ctBinaryOperator, ctBinaryOperator.toString(), ExpressionUtil.negate(suggestion).toString(), problemType);
}
}
}
Expand All @@ -157,17 +157,17 @@ private void checkIsEmptyReimplementation(CtExpression<?> target, CtBinaryOperat
private void checkEqualsCall(CtExpression<?> target, CtInvocation<?> ctInvocation, ProblemType problemType) {
CtExpression<?> argument = ctInvocation.getArguments().get(0);

if (SpoonUtil.isNullLiteral(argument)) {
if (ExpressionUtil.isNullLiteral(argument)) {
return;
}

if (SpoonUtil.isStringLiteral(SpoonUtil.resolveConstant(argument), "")) {
if (ExpressionUtil.isStringLiteral(ExpressionUtil.resolveConstant(argument), "")) {
this.reportProblem(ctInvocation, ctInvocation.toString(), buildIsEmptySuggestion(target).toString(), problemType);
return;
}

// detect "".equals(s)
if (SpoonUtil.isStringLiteral(SpoonUtil.resolveConstant(target), "")) {
if (ExpressionUtil.isStringLiteral(ExpressionUtil.resolveConstant(target), "")) {
this.reportProblem(ctInvocation, ctInvocation.toString(), buildIsEmptySuggestion(argument).toString(), problemType);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
import de.firemage.autograder.core.LocalizedMessage;
import de.firemage.autograder.core.ProblemType;
import de.firemage.autograder.core.check.ExecutableCheck;
import de.firemage.autograder.core.integrated.ExpressionUtil;
import de.firemage.autograder.core.integrated.IntegratedCheck;
import de.firemage.autograder.core.integrated.SpoonUtil;
import de.firemage.autograder.core.integrated.StatementUtil;
import de.firemage.autograder.core.integrated.StaticAnalysis;
import de.firemage.autograder.core.integrated.MethodUtil;
import de.firemage.autograder.core.integrated.TypeUtil;
Expand Down Expand Up @@ -50,7 +51,7 @@ private static boolean isMathSqrt(CtInvocation<?> ctInvocation) {
private static boolean isPowSqrt(CtInvocation<?> ctInvocation) {
return isMathPow(ctInvocation)
&& ctInvocation.getArguments().size() == 2
&& SpoonUtil.resolveConstant(ctInvocation.getArguments().get(1)) instanceof CtLiteral<?> ctLiteral
&& ExpressionUtil.resolveConstant(ctInvocation.getArguments().get(1)) instanceof CtLiteral<?> ctLiteral
&& ctLiteral.getValue() instanceof Double doubleValue
&& doubleValue == 0.5;
}
Expand Down Expand Up @@ -124,7 +125,7 @@ private void checkMaxMin(CtIf ctIf) {

// ensure that in the if block there is only one assignment to a variable
// and the condition is a binary operator with <, <=, > or >=
List<CtStatement> thenBlock = SpoonUtil.getEffectiveStatements(ctIf.getThenStatement());
List<CtStatement> thenBlock = StatementUtil.getEffectiveStatements(ctIf.getThenStatement());
if (thenBlock.size() != 1
|| !(thenBlock.get(0) instanceof CtAssignment<?, ?> thenAssignment)
|| !(thenAssignment.getAssigned() instanceof CtVariableWrite<?> ctVariableWrite)
Expand All @@ -143,7 +144,7 @@ private void checkMaxMin(CtIf ctIf) {
assignedVariable.getModifiers().contains(ModifierKind.STATIC)
);
if (ctIf.getElseStatement() != null) {
List<CtStatement> elseBlock = SpoonUtil.getEffectiveStatements(ctIf.getElseStatement());
List<CtStatement> elseBlock = StatementUtil.getEffectiveStatements(ctIf.getElseStatement());
if (elseBlock.size() != 1
|| !(elseBlock.get(0) instanceof CtAssignment<?,?> elseAssignment)
|| !(elseAssignment.getAssigned() instanceof CtVariableAccess<?> elseAccess)
Expand All @@ -158,7 +159,7 @@ private void checkMaxMin(CtIf ctIf) {
CtBinaryOperator<Boolean> condition = ctBinaryOperator;
// ensure that the else value is on the left side of the condition
if (ctBinaryOperator.getRightHandOperand().equals(elseValue)) {
condition = SpoonUtil.swapCtBinaryOperator(condition);
condition = ExpressionUtil.swapCtBinaryOperator(condition);
}

// if it is not on either side of the condition, return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import de.firemage.autograder.core.LocalizedMessage;
import de.firemage.autograder.core.ProblemType;
import de.firemage.autograder.core.check.ExecutableCheck;
import de.firemage.autograder.core.integrated.ExpressionUtil;
import de.firemage.autograder.core.integrated.IntegratedCheck;
import de.firemage.autograder.core.integrated.SpoonUtil;
import de.firemage.autograder.core.integrated.StaticAnalysis;
import de.firemage.autograder.core.integrated.TypeUtil;
import spoon.processing.AbstractProcessor;
Expand Down Expand Up @@ -37,7 +37,7 @@ public void process(CtInvocation<?> ctInvocation) {
}

List<CtExpression<?>> args = ctInvocation.getArguments();
if (SpoonUtil.resolveConstant(args.get(1)) instanceof CtLiteral<?> ctLiteral
if (ExpressionUtil.resolveConstant(args.get(1)) instanceof CtLiteral<?> ctLiteral
&& ctLiteral.getValue() instanceof Integer number
&& number == 0
&& args.get(2) instanceof CtFieldAccess<?> ctFieldAccess
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
import de.firemage.autograder.core.ProblemType;
import de.firemage.autograder.core.check.ExecutableCheck;

import de.firemage.autograder.core.integrated.ExpressionUtil;
import de.firemage.autograder.core.integrated.ForLoopRange;
import de.firemage.autograder.core.integrated.IntegratedCheck;
import de.firemage.autograder.core.integrated.SpoonUtil;
import de.firemage.autograder.core.integrated.StatementUtil;
import de.firemage.autograder.core.integrated.StaticAnalysis;
import de.firemage.autograder.core.integrated.TypeUtil;
import spoon.processing.AbstractProcessor;
Expand All @@ -26,7 +27,7 @@ public class StringRepeat extends IntegratedCheck {
private void checkStringRepeat(CtFor ctFor) {
ForLoopRange forLoopRange = ForLoopRange.fromCtFor(ctFor).orElse(null);

List<CtStatement> statements = SpoonUtil.getEffectiveStatements(ctFor.getBody());
List<CtStatement> statements = StatementUtil.getEffectiveStatements(ctFor.getBody());
if (statements.size() != 1 || forLoopRange == null) {
return;
}
Expand All @@ -39,7 +40,7 @@ private void checkStringRepeat(CtFor ctFor) {
return;
}

CtExpression<?> rhs = SpoonUtil.resolveCtExpression(ctAssignment.getAssignment());
CtExpression<?> rhs = ExpressionUtil.resolveCtExpression(ctAssignment.getAssignment());
// return if the for loop uses the loop variable (would not be a simple repetition)
if (!ctAssignment.getElements(new VariableAccessFilter<>(forLoopRange.loopVariable())).isEmpty()) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import de.firemage.autograder.core.integrated.ForLoopRange;
import de.firemage.autograder.core.integrated.IntegratedCheck;
import de.firemage.autograder.core.integrated.SpoonUtil;
import de.firemage.autograder.core.integrated.StatementUtil;
import de.firemage.autograder.core.integrated.StaticAnalysis;
import spoon.processing.AbstractProcessor;
import spoon.reflect.code.CtArrayAccess;
Expand All @@ -25,7 +25,7 @@ public class UseArrayCopy extends IntegratedCheck {
private void checkArrayCopy(CtFor ctFor) {
ForLoopRange forLoopRange = ForLoopRange.fromCtFor(ctFor).orElse(null);

List<CtStatement> statements = SpoonUtil.getEffectiveStatements(ctFor.getBody());
List<CtStatement> statements = StatementUtil.getEffectiveStatements(ctFor.getBody());
if (statements.size() != 1 || forLoopRange == null) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
import de.firemage.autograder.core.check.ExecutableCheck;
import de.firemage.autograder.core.integrated.ForLoopRange;
import de.firemage.autograder.core.integrated.IntegratedCheck;
import de.firemage.autograder.core.integrated.SpoonUtil;
import de.firemage.autograder.core.integrated.VariableUtil;
import de.firemage.autograder.core.integrated.StatementUtil;
import de.firemage.autograder.core.integrated.StaticAnalysis;
import de.firemage.autograder.core.integrated.TypeUtil;
import de.firemage.autograder.core.integrated.UsesFinder;
import spoon.processing.AbstractProcessor;
import spoon.reflect.code.CtArrayWrite;
Expand All @@ -30,7 +32,7 @@ public class UseArraysFill extends IntegratedCheck {
private void checkArraysFill(CtFor ctFor) {
ForLoopRange forLoopRange = ForLoopRange.fromCtFor(ctFor).orElse(null);

List<CtStatement> statements = SpoonUtil.getEffectiveStatements(ctFor.getBody());
List<CtStatement> statements = StatementUtil.getEffectiveStatements(ctFor.getBody());

if (statements.size() != 1
|| forLoopRange == null
Expand All @@ -41,7 +43,7 @@ private void checkArraysFill(CtFor ctFor) {
return;
}

CtVariable<?> loopVariable = (CtVariable<?>) SpoonUtil.getReferenceDeclaration(forLoopRange.loopVariable());
CtVariable<?> loopVariable = (CtVariable<?>) VariableUtil.getReferenceDeclaration(forLoopRange.loopVariable());
// return if the for loop uses the loop variable (would not be a simple repetition)
if (UsesFinder.variableUses(loopVariable).nestedIn(ctAssignment.getAssignment()).hasAny()) {
return;
Expand All @@ -53,7 +55,7 @@ private void checkArraysFill(CtFor ctFor) {
}

CtExpression<?> rhs = ctAssignment.getAssignment();
if (!SpoonUtil.isImmutable(rhs.getType())) {
if (!TypeUtil.isImmutable(rhs.getType())) {
return;
}

Expand Down
Loading

0 comments on commit 1ced32e

Please sign in to comment.