Skip to content

Commit

Permalink
fix exception message check implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Luro02 committed Jun 29, 2024
1 parent 41725cc commit 13b427c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
@ExecutableCheck(reportedProblems = ProblemType.EXCEPTION_WITHOUT_MESSAGE)
public class ExceptionMessageCheck extends IntegratedCheck {
private static boolean isExceptionWithoutMessage(CtExpression<?> expression) {
if (!(expression instanceof CtConstructorCall<?> ctConstructorCall) ||
!(SpoonUtil.isSubtypeOf(expression.getType(), java.lang.Exception.class))) {
if (!(expression instanceof CtConstructorCall<?> ctConstructorCall)
|| !SpoonUtil.isSubtypeOf(expression.getType(), java.lang.Exception.class)) {
return false;
}

Expand Down Expand Up @@ -50,9 +50,7 @@ private static boolean hasMessage(Iterable<? extends CtExpression<?>> arguments)

String literal = SpoonUtil.tryGetStringLiteral(ctExpression).orElse(null);

if (literal != null) {
return !literal.isBlank();
}
return literal == null || !literal.isBlank();
}

return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -293,4 +293,22 @@ public static void main(String[] args) throws Exception {

problems.assertExhausted();
}

@Test
void testExceptionWithFunctionCall() throws IOException, LinterException {
ProblemIterator problems = this.checkIterator(StringSourceInfo.fromSourceString(
JavaVersion.JAVA_17,
"Main",
"""
public class Main {
private static final String MESSAGE = "message: %s";
public static void main(String[] args) throws Exception {
throw new IllegalStateException(MESSAGE.formatted(args));
}
}
"""
), PROBLEM_TYPES);

problems.assertExhausted();
}
}

0 comments on commit 13b427c

Please sign in to comment.