Skip to content

Commit

Permalink
Create rule S6862: Beans in "@configuration" class should have differ…
Browse files Browse the repository at this point in the history
…ent names
  • Loading branch information
irina-batinic-sonarsource committed Nov 30, 2023
1 parent 73c8652 commit 112a7af
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 32 deletions.
13 changes: 6 additions & 7 deletions rules/S6862/java/metadata.json
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
{
"title": "FIXME",
"type": "CODE_SMELL",
"title": "Beans in \"@Configuration\" class should have different names",
"type": "BUG",
"status": "ready",
"remediation": {
"func": "Constant\/Issue",
"constantCost": "5min"
},
"tags": [
"spring"
],
"defaultSeverity": "Major",
"ruleSpecification": "RSPEC-6862",
"sqKey": "S6862",
"scope": "All",
"scope": "Main",
"defaultQualityProfiles": ["Sonar way"],
"quickfix": "unknown",
"code": {
"impacts": {
"MAINTAINABILITY": "HIGH",
"RELIABILITY": "MEDIUM",
"SECURITY": "LOW"
"RELIABILITY": "MEDIUM"
},
"attribute": "CONVENTIONAL"
"attribute": "CLEAR"
}
}
60 changes: 35 additions & 25 deletions rules/S6862/java/rule.adoc
Original file line number Diff line number Diff line change
@@ -1,44 +1,54 @@
FIXME: add a description

// If you want to factorize the description uncomment the following line and create the file.
//include::../description.adoc[]

== Why is this an issue?

FIXME: remove the unused optional headers (that are commented out)

//=== What is the potential impact?
Naming conventions play a crucial role in maintaining code clarity and readability.
In Spring configurations, the uniqueness of bean names is vital to avoid confusion for both developers and the Spring Context.
When two beans share the same name within a configuration, it becomes challenging for a reader to discern which bean is being referred to, leading to potential misunderstandings and errors.

== How to fix it
//== How to fix it in FRAMEWORK NAME

To address this issue, ensure each bean within a configuration has a distinct and meaningful name.
Choose names that accurately represent the purpose or functionality of the bean, making it easier for developers to understand the role of each component.

=== Code examples

==== Noncompliant code example

[source,text,diff-id=1,diff-type=noncompliant]
[source,java,diff-id=1,diff-type=noncompliant]
----
FIXME
@Configuration
class Config {
@Bean
public User user() {
return currentUser();
}
@Bean
public User user(AuthService auth) { // Noncompliant
return auth.user();
}
}
----

==== Compliant solution

[source,text,diff-id=1,diff-type=compliant]
[source,java,diff-id=1,diff-type=compliant]
----
FIXME
@Configuration
class Config {
@Bean
public User user() {
return currentUser();
}
@Bean
public User userFromAuth(AuthService auth) {
return auth.user();
}
}
----

//=== How does this work?

//=== Pitfalls

//=== Going the extra mile
== Resources

=== Documentation

//== Resources
//=== Documentation
//=== Articles & blog posts
//=== Conference presentations
//=== Standards
//=== External coding guidelines
//=== Benchmarks
* https://docs.spring.io/spring-framework/reference/core/beans/java/basic-concepts.html[Spring IO - Basic concepts: @Bean and @Configuration]
* https://docs.spring.io/spring-framework/reference/core/beans/java/configuration-annotation.html[Spring IO - Using the @Configuration annotation]
* https://docs.spring.io/spring-framework/reference/core/beans/java/bean-annotation.html[Spring IO - Using the @Bean annotation]

0 comments on commit 112a7af

Please sign in to comment.