Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

SONARJAVA-2943 Rule S2139: Exceptions should be either logged or rethrown but not both#2313

Merged
christophe-zurn-sonarsource merged 5 commits into
masterSonarSource/sonar-java:masterfrom
SONARJAVA-2943SonarSource/sonar-java:SONARJAVA-2943Copy head branch name to clipboard
Nov 2, 2018
Merged

SONARJAVA-2943 Rule S2139: Exceptions should be either logged or rethrown but not both#2313
christophe-zurn-sonarsource merged 5 commits into
masterSonarSource/sonar-java:masterfrom
SONARJAVA-2943SonarSource/sonar-java:SONARJAVA-2943Copy head branch name to clipboard

Conversation

@andrea-guarino-sonarsource

Copy link
Copy Markdown
Contributor

No description provided.

@andrea-guarino-sonarsource andrea-guarino-sonarsource force-pushed the SONARJAVA-2943 branch 2 times, most recently from 09a89a8 to ef7ce53 Compare November 1, 2018 16:50

@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();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);

@sonarsource-next sonarsource-next Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SonarQube analysis found issues:
Bug Bugs: 0
Vulnerability Vulnerabilities: 0
Code Smell Code Smells: 1

See all issues in SonarQube

@sonarsource-next sonarsource-next Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SonarQube analysis found issues:
Bug Bugs: 0
Vulnerability Vulnerabilities: 0
Code Smell Code Smells: 1

See all issues in SonarQube

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(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could use a MethodMatcherCollection instead


@Override
public void visitIdentifier(IdentifierTree tree) {
if (tree.name().equals(exceptionIdentifier.name())) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no need to recheck equality if isExceptionIdentifierUsed is already true

Comment thread java-checks/src/test/files/checks/LoggedRethrownExceptionsCheck.java Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@christophe-zurn-sonarsource christophe-zurn-sonarsource merged commit 2613446 into master Nov 2, 2018
@christophe-zurn-sonarsource christophe-zurn-sonarsource deleted the SONARJAVA-2943 branch November 2, 2018 13:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

Morty Proxy This is a proxified and sanitized view of the page, visit original site.