Skip to content

Commit

Permalink
first draft for Injecting data into static fields is not supported by…
Browse files Browse the repository at this point in the history
… Spring
  • Loading branch information
erwan-serandour committed Jan 24, 2025
1 parent a25cec9 commit 723e32b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 24 deletions.
8 changes: 4 additions & 4 deletions rules/S7178/java/metadata.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"title": "FIXME",
"title": "Injecting data into static fields is not supported by Spring",
"type": "CODE_SMELL",
"status": "ready",
"remediation": {
Expand All @@ -11,15 +11,15 @@
"defaultSeverity": "Major",
"ruleSpecification": "RSPEC-7178",
"sqKey": "S7178",
"scope": "All",
"scope": "Main",
"defaultQualityProfiles": ["Sonar way"],
"quickfix": "unknown",
"code": {
"impacts": {
"MAINTAINABILITY": "HIGH",
"RELIABILITY": "MEDIUM",
"RELIABILITY": "HIGH",
"SECURITY": "LOW"
},
"attribute": "CONVENTIONAL"
"attribute": "INTENTIONAL"
}
}
49 changes: 29 additions & 20 deletions rules/S7178/java/rule.adoc
Original file line number Diff line number Diff line change
@@ -1,44 +1,53 @@
FIXME: add a description
== Why is this an issue?

// If you want to factorize the description uncomment the following line and create the file.
//include::../description.adoc[]
Spring dependency injection framework does not support injecting data into static fields. When @Value, @Inject, or @Autowired are applied to static fields, they are ignored.

== Why is this an issue?
What is the potential impact?

FIXME: remove the unused optional headers (that are commented out)
* Null Values: Uninitialized static fields annotated with @Value, @Inject, or @Autowired will not be initialized by Spring, potentially causing NullPointerException at runtime.
* Confusing Code: The presence of injection annotations on static fields can mislead developers into believing that the fields will be populated by Spring.
//=== What is the potential impact?
This rule raises an issue when a static will is annotated with @Value, @Inject, or @Autowired.

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

Either use an instance field instead of a static field or remove the @Value, @Inject, or @Autowired annotation and initialize the field.


=== Code examples

==== Noncompliant code example

[source,java,diff-id=1,diff-type=noncompliant]
----
FIXME
@Component
public class MyComponent {
@Autowired
private static SomeDependency dependency; // Noncompliant, @Autowired will be ignored and no value will be injected
// ...
}
----

==== Compliant solution

[source,java,diff-id=1,diff-type=compliant]
----
FIXME
----
@Component
public class MyComponent {
//=== How does this work?
private final SomeDependency dependency;
//=== Pitfalls

//=== Going the extra mile
@Autowired
public ExampleClass(SomeDependency dependency) {
this.dependency = dependency;
}
// ...
}
----

//== Resources
== Resources
//=== Documentation
//=== Articles & blog posts
//=== Conference presentations
//=== Standards
//=== External coding guidelines
//=== Benchmarks
=== Articles & blog posts
* Java Guides - https://www.baeldung.com/spring-inject-static-field

0 comments on commit 723e32b

Please sign in to comment.