From 82468900d8f3d0552eae390524e5941ea7605735 Mon Sep 17 00:00:00 2001 From: Lucas <24826124+Luro02@users.noreply.github.com> Date: Tue, 23 Jan 2024 21:53:32 +0100 Subject: [PATCH] fix exception message check #382 --- .../exceptions/ExceptionMessageCheck.java | 24 +++++++++---------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/autograder-core/src/main/java/de/firemage/autograder/core/check/exceptions/ExceptionMessageCheck.java b/autograder-core/src/main/java/de/firemage/autograder/core/check/exceptions/ExceptionMessageCheck.java index 034009b3..d0460a37 100644 --- a/autograder-core/src/main/java/de/firemage/autograder/core/check/exceptions/ExceptionMessageCheck.java +++ b/autograder-core/src/main/java/de/firemage/autograder/core/check/exceptions/ExceptionMessageCheck.java @@ -13,9 +13,10 @@ import spoon.reflect.code.CtConstructorCall; import spoon.reflect.code.CtExpression; import spoon.reflect.code.CtThrow; -import spoon.reflect.code.CtVariableAccess; import spoon.reflect.declaration.CtElement; +import java.util.List; + @ExecutableCheck(reportedProblems = ProblemType.EXCEPTION_WITHOUT_MESSAGE) public class ExceptionMessageCheck extends IntegratedCheck { private static boolean isExceptionWithoutMessage(CtExpression expression) { @@ -24,22 +25,19 @@ private static boolean isExceptionWithoutMessage(CtExpression expression) { && !hasMessage(ctorCall.getArguments()); } - private static boolean hasMessage(Iterable> arguments) { - for (CtExpression ctExpression : arguments) { - String literal = SpoonUtil.tryGetStringLiteral(ctExpression).orElse(null); + private static boolean hasMessage(List> arguments) { + if (arguments.isEmpty()) { + return false; + } - if (literal != null) { - return !literal.isBlank(); - } + CtExpression ctExpression = arguments.get(0); + String literal = SpoonUtil.tryGetStringLiteral(ctExpression).orElse(null); - // allow wrapping exceptions into a new exception - if (ctExpression instanceof CtVariableAccess ctVariableAccess - && SpoonUtil.isSubtypeOf(ctVariableAccess.getType(), java.lang.Throwable.class)) { - return true; - } + if (literal != null) { + return !literal.isBlank(); } - return false; + return true; } private static boolean isInAllowedContext(CtElement ctElement) {