SONARJAVA-2943 Rule S2139: Exceptions should be either logged or rethrown but not both#2313
SONARJAVA-2943 Rule S2139: Exceptions should be either logged or rethrown but not both#2313
Conversation
09a89a8 to
ef7ce53
Compare
|
|
||
| @Rule(key = "S2139") | ||
| public class LoggedRethrownExceptionsCheck extends IssuableSubscriptionVisitor { | ||
| private static final MethodMatcher JAVA_UTIL_LOGGER = MethodMatcher.create().typeDefinition("java.util.logging.Logger").name("log").withAnyParameters(); |
There was a problem hiding this comment.
As you rely on MethodMatcher, the check should ensure it has access to the semantic for the rule to work correctly
|
|
||
| @Rule(key = "S2139") | ||
| public class LoggedRethrownExceptionsCheck extends IssuableSubscriptionVisitor { | ||
| private static final MethodMatcher JAVA_UTIL_LOGGER = MethodMatcher.create().typeDefinition("java.util.logging.Logger").name("log").withAnyParameters(); |
There was a problem hiding this comment.
java.util.logging.Logger has other log methods (logp, logrb, throwing, info, warning, ...)
And org.slf4j.Logger logging methods (error, info, ...) could also be added (as it is already used in other checks, ex: LazyArgEvaluationCheck).
|
|
||
| class A { | ||
|
|
||
| public foo() { |
There was a problem hiding this comment.
Method is missing return type
| for (StatementTree statementTree : catchTree.block().body()) { | ||
| if (statementTree.is(Tree.Kind.THROW_STATEMENT)) { | ||
| secondaryLocations.add(new Location("", ((ThrowStatementTree) statementTree).expression())); | ||
| isRethrowing = true; |
There was a problem hiding this comment.
I think you could just report issue here if error was logged and return, as having statements after a throw is not valid java code
| if (expression.is(Tree.Kind.METHOD_INVOCATION)) { | ||
| MethodInvocationTree mit = (MethodInvocationTree) expression; | ||
| return JAVA_UTIL_LOGGER.matches(mit) && mit.arguments().stream().anyMatch( | ||
| param -> param.is(Tree.Kind.IDENTIFIER) && ((IdentifierTree)param).name().equals(exceptionIdentifier)); |
There was a problem hiding this comment.
I would look for any use of the exception symbol inside the arguments, otherwise you will miss the following cases:
logger.log(Level.ALL, "MyError: " + e);
logger.log(Level.ALL, e.getMessage());
throw new MySQLException(contextInfo, e);
…rown but not both
2e3f80a to
e71ab4a
Compare
e71ab4a to
2a277b8
Compare
| public class LoggedRethrownExceptionsCheck extends IssuableSubscriptionVisitor { | ||
| private static final String JAVA_UTIL_LOGGING_LOGGER = "java.util.logging.Logger"; | ||
| private static final String SLF4J_LOGGER = "org.slf4j.Logger"; | ||
| private static final List<MethodMatcher> LOGGING_METHODS = Collections.unmodifiableList(Arrays.asList( |
There was a problem hiding this comment.
You could use a MethodMatcherCollection instead
|
|
||
| @Override | ||
| public void visitIdentifier(IdentifierTree tree) { | ||
| if (tree.name().equals(exceptionIdentifier.name())) { |
There was a problem hiding this comment.
There is no need to recheck equality if isExceptionIdentifierUsed is already true
christophe-zurn-sonarsource
left a comment
There was a problem hiding this comment.
LGTM
No description provided.