Skip to content

S1186: Ignore empty methods with @CacheEvict annotation #5144

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.cache.annotation.CacheEvict;

class EmptyMethodsCheckNoSemantics {
class A {
Expand Down Expand Up @@ -186,12 +187,18 @@ private void emptyMethod2() {

private class ExceptionalCompliantCases {
@org.aspectj.lang.annotation.Pointcut()
void foo() {
void foo() { // Compliant

}

@Pointcut()
void bar() {}
void bar() {} // Compliant

@org.springframework.cache.annotation.CacheEvict(cacheNames = "flowers", allEntries = true)
void evictAll() {} // Compliant

@CacheEvict(value = "flowers", key = "{#name}")
void evict(String name) {} // Compliant

@Before("")
void stillTriggerOnOtherAnnotations() {} // Noncompliant
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.cache.annotation.CacheEvict;

class EmptyMethodsCheckSample {
class A {
Expand Down Expand Up @@ -186,12 +187,18 @@ private void emptyMethod2() {

private class ExceptionalCompliantCases {
@org.aspectj.lang.annotation.Pointcut()
void foo() {
void foo() { // Compliant

}

@Pointcut()
void bar() {}
void bar() {} // Compliant

@org.springframework.cache.annotation.CacheEvict(cacheNames = "flowers", allEntries = true)
void evictAll() {} // Compliant

@CacheEvict(value = "flowers", key = "{#name}")
void evict(String name) {} // Compliant

@Before("")
void stillTriggerOnOtherAnnotations() {} // Noncompliant
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,6 @@
@Rule(key = "S1186")
public class EmptyMethodsCheck extends IssuableSubscriptionVisitor {

// Some methods may legitimately be left empty, e.g. methods annotated with org.aspectj.lang.annotation.Pointcut. We ignore them here.
private static final String IGNORED_METHODS_ANNOTATION = "org.aspectj.lang.annotation.Pointcut";
private static final String IGNORED_METHODS_ANNOTATION_UNQUALIFIED = "Pointcut";

@Override
public List<Kind> nodesToVisit() {
return Arrays.asList(Tree.Kind.CLASS, Tree.Kind.ENUM, Tree.Kind.RECORD);
Expand Down Expand Up @@ -73,8 +69,18 @@ private void checkMethods(List<Tree> members) {
* Returns true if the annotation indicates that the method body can legitimately be empty.
*/
private static boolean isExceptedAnnotation(AnnotationTree annotationTree) {
return annotationTree.symbolType().is(IGNORED_METHODS_ANNOTATION) ||
(annotationTree.symbolType().isUnknown() && annotationTree.symbolType().name().equals(IGNORED_METHODS_ANNOTATION_UNQUALIFIED));
if (annotationTree.symbolType().isUnknown()) {
return switch (annotationTree.symbolType().name()) {
case "Pointcut",
"CacheEvict" -> true;
default -> false;
};
}
return switch (annotationTree.symbolType().fullyQualifiedName()) {
case "org.aspectj.lang.annotation.Pointcut",
"org.springframework.cache.annotation.CacheEvict" -> true;
default -> false;
};
}

private void checkConstructors(List<Tree> members) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ <h3>Exceptions</h3>
<li> Public default (no-argument) constructors when there are other constructors in the class </li>
<li> Empty methods in abstract classes </li>
<li> Methods annotated with <code>@org.aspectj.lang.annotation.Pointcut()</code> </li>
<li> Methods annotated with <code>@org.springframework.cache.annotation.CacheEvict()</code> </li>
</ul>
<pre>
public abstract class Animal {
Expand Down