Skip to content

Commit 7f28869

Browse files
SONARJAVA-5318 modify S7180 to not raise in interface annotated with spring data repository (#5027)
1 parent b8cbc1c commit 7f28869

2 files changed

Lines changed: 33 additions & 0 deletions

File tree

java-checks-test-sources/default/src/main/java/checks/spring/CacheAnnotationsShouldOnlyBeAppliedToConcreteClassesCheckSample.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import org.springframework.cache.annotation.CachePut;
66
import org.springframework.cache.annotation.Cacheable;
77
import org.springframework.cache.annotation.Caching;
8+
import org.springframework.data.repository.CrudRepository;
9+
import org.springframework.data.repository.Repository;
810

911
public class CacheAnnotationsShouldOnlyBeAppliedToConcreteClassesCheckSample {
1012

@@ -86,4 +88,30 @@ class ClassCaching {
8688
@Caching
8789
String getData(String id){return "";}
8890
}
91+
92+
interface MyRepository extends Repository<Integer, Integer> {
93+
@Cacheable("aCache") // compliant, it is the only way to do it in repositories. I guess depending on proxy mode
94+
// repositories don't work at all
95+
Integer findById(Integer id);
96+
97+
@CachePut("aCache") // compliant
98+
Integer findByIdCachePut(Integer id);
99+
100+
@CacheEvict("aCache") // compliant
101+
Integer findByIdCacheEvict(Integer id);
102+
103+
@org.springframework.cache.annotation.Cacheable("aCache")
104+
Integer fullyQualifiedCache(Integer i);
105+
}
106+
107+
public interface MyCrudRepository extends CrudRepository<Integer, Integer> {
108+
@CachePut("aCache") // compliant
109+
Integer findByIdCachePut(Integer id);
110+
111+
@CacheEvict("aCache") // compliant
112+
Integer findByIdCacheEvict(Integer id);
113+
114+
@org.springframework.cache.annotation.Cacheable("aCache")
115+
Integer fullyQualifiedCache(Integer i);
116+
}
89117
}

java-checks/src/main/java/org/sonar/java/checks/spring/CacheAnnotationsShouldOnlyBeAppliedToConcreteClassesCheck.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
public class CacheAnnotationsShouldOnlyBeAppliedToConcreteClassesCheck extends IssuableSubscriptionVisitor {
3232

3333
private static final String ISSUE_MESSAGE = "\"@%s\" annotation should only be applied to concrete classes.";
34+
private static final String REPOSITORY_INTERFACE = "org.springframework.data.repository.Repository";
3435
private static final Set<String> CACHING_ANNOTATIONS = Set.of(
3536
"org.springframework.cache.annotation.CacheConfig",
3637
"org.springframework.cache.annotation.CacheEvict",
@@ -47,6 +48,10 @@ public List<Tree.Kind> nodesToVisit() {
4748
public void visitNode(Tree tree) {
4849
ClassTree anInterface = (ClassTree) tree;
4950

51+
if (anInterface.symbol().type().isSubtypeOf(REPOSITORY_INTERFACE)) {
52+
return;
53+
}
54+
5055
// report caching annotations on the whole interface
5156
selectCachingAnnotations(anInterface.modifiers())
5257
.forEach(ann -> {

0 commit comments

Comments
 (0)