-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SONARJAVA-5285 Create rule S7180: "@Cache*" annotations should only b…
…e applied on concrete classes (#4613)
- Loading branch information
1 parent
040c9da
commit 46eb087
Showing
3 changed files
with
73 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
{ | ||
"title": "\"@Cache*\" annotations should only be applied on concrete classes", | ||
"type": "CODE_SMELL", | ||
"status": "ready", | ||
"remediation": { | ||
"func": "Constant\/Issue", | ||
"constantCost": "5min" | ||
}, | ||
"tags": [ | ||
"spring" | ||
], | ||
"defaultSeverity": "Major", | ||
"ruleSpecification": "RSPEC-7180", | ||
"sqKey": "S7180", | ||
"scope": "Main", | ||
"defaultQualityProfiles": ["Sonar way"], | ||
"quickfix": "unknown", | ||
"code": { | ||
"impacts": { | ||
"MAINTAINABILITY": "LOW", | ||
"RELIABILITY": "MEDIUM", | ||
"SECURITY": "LOW" | ||
}, | ||
"attribute": "LOGICAL" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
== Why is this an issue? | ||
|
||
Annotating interfaces or interface methods with ``++@Cache*++`` annotations is not recommended. When using CGLIB-based proxies, these annotations will be ignored, and no caching proxy will be created. | ||
|
||
=== What is the potential impact? | ||
|
||
* *Confusing Code*: Developers may mistakenly believe that caching is in effect, leading to confusion and incorrect assumptions about application performance. | ||
|
||
This rule raises an issue when an interface or an interface method is annotated with a ``++@Cache*++`` annotation. | ||
|
||
== How to fix it | ||
|
||
Move ``++@Cache*++`` annotation from interface or interface method to the concrete class. | ||
|
||
=== Code examples | ||
|
||
==== Noncompliant code example | ||
|
||
[source,java,diff-id=1,diff-type=noncompliant] | ||
---- | ||
public interface ExampleService { | ||
@Cacheable("exampleCache") //non compliant, interface method is annotated with @Cacheable | ||
String getData(String id); | ||
} | ||
---- | ||
|
||
==== Compliant solution | ||
|
||
[source,java,diff-id=1,diff-type=compliant] | ||
---- | ||
@Service | ||
public class ExampleServiceImpl implements ExampleService { | ||
@Cacheable("exampleCache") | ||
@Override | ||
public String getData(String id) { | ||
// Implementation here | ||
} | ||
} | ||
---- | ||
|
||
== Resources | ||
=== Documentation | ||
* Spring - https://docs.spring.io/spring-framework/reference/integration/cache/annotations.html#cache-annotation-enable[Declarative Annotation-based Caching] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
{ | ||
} |