Skip to content

Commit

Permalink
fix exception message check #382
Browse files Browse the repository at this point in the history
  • Loading branch information
Luro02 committed Jan 23, 2024
1 parent 10841db commit 8246890
Showing 1 changed file with 11 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -24,22 +25,19 @@ private static boolean isExceptionWithoutMessage(CtExpression<?> expression) {
&& !hasMessage(ctorCall.getArguments());
}

private static boolean hasMessage(Iterable<? extends CtExpression<?>> arguments) {
for (CtExpression<?> ctExpression : arguments) {
String literal = SpoonUtil.tryGetStringLiteral(ctExpression).orElse(null);
private static boolean hasMessage(List<? extends CtExpression<?>> 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) {
Expand Down

0 comments on commit 8246890

Please sign in to comment.