Skip to content

Commit

Permalink
SONARJAVA-3995 FN S3400 should also report issues on static methods (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
alban-auzeill committed Feb 10, 2023
1 parent 0521bb3 commit 54efaf8
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ final long finalMethod() {
return 1L; // Noncompliant
}

static long staticMethod() {
return 1L; // Noncompliant
}

long noFinalMethod() {
return 1L; // Compliant
}
Expand All @@ -87,6 +91,27 @@ String noFinalMethod() {
}
}

enum MyEnum {
ONE() {
int overridableMethod() {
return 1; // Compliant, override
}
},
TWO(){
int overridableMethod() {
return 2; // Compliant, override
}
};

int overridableMethod() {
return 0; // Compliant, overridable
}

static int nonOverridableStaticMethod() {
return 0; // Noncompliant
}
}

abstract class AbstractClass {
abstract void abstractMethod(); // Compliant

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,13 @@ public void visitNode(Tree tree) {

private static boolean isEffectivelyFinal(MethodTree methodTree) {
Tree methodParent = methodTree.parent();
if (!(methodParent instanceof ClassTree) || methodParent.is(Tree.Kind.ANNOTATION_TYPE, Tree.Kind.INTERFACE, Kind.ENUM)) {
if (!(methodParent instanceof ClassTree) || methodParent.is(Tree.Kind.ANNOTATION_TYPE, Tree.Kind.INTERFACE)) {
return false;
} else if (methodParent.is(Tree.Kind.RECORD) || ((ClassTree)methodParent).symbol().isFinal()) {
return true;
}
Symbol.MethodSymbol methodSymbol = methodTree.symbol();
return methodSymbol.isFinal() || methodSymbol.isPrivate();
return methodSymbol.isFinal() || methodSymbol.isPrivate() || methodSymbol.isStatic();
}

private static boolean isConstant(@Nullable ExpressionTree returnedExpression) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,11 @@ <h2>Compliant Solution</h2>
static final int BEST_NUMBER = 12;
</pre>
<h2>Exceptions</h2>
<p>Methods with annotations, such as <code>@Override</code> and Spring’s <code>@RequestMapping</code>, are ignored.</p>
<p>The following types of method are ignored:</p>
<ul>
<li> methods that override a method. </li>
<li> methods that are not final (not having the <code>final</code>, <code>private</code> or <code>static</code> modifier and not in a record or a
final class). </li>
<li> methods with annotations, such as <code>@Override</code> or Spring’s <code>@RequestMapping</code>. </li>
</ul>

0 comments on commit 54efaf8

Please sign in to comment.