Skip to content
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

SONARJAVA-5284 Create rule S7178: Injecting data into static fields is not supported by Spring #4611

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

github-actions[bot]
Copy link
Contributor

You can preview this rule here (updated a few minutes after each push).

Review

A dedicated reviewer checked the rule description successfully for:

  • logical errors and incorrect information
  • information gaps and missing content
  • text style and tone
  • PR summary and labels follow the guidelines

@github-actions github-actions bot added the java label Jan 23, 2025
@erwan-serandour erwan-serandour changed the title Create rule S7178 SONARJAVA-5284 Create rule S7178 Jan 23, 2025
Comment on lines 18 to 22
"impacts": {
"MAINTAINABILITY": "HIGH",
"RELIABILITY": "HIGH",
"SECURITY": "LOW"
},

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would say that the issue mostly impacts reliability, as the code will not behave as intended

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍


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.

What is the potential impact?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
What is the potential impact?
=== What is the potential impact?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

* 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.

This rule raises an issue when a static will is annotated with @Value, @Inject, or @Autowired.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
This rule raises an issue when a static will is annotated with @Value, @Inject, or @Autowired.
This rule raises an issue when a static field is annotated with @Value, @Inject, or @Autowired.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice catch 👍

public class MyComponent {

@Value("${my.app.prop}")
private static SomeDependency dependency; // Noncompliant, @Value will be ignored and no value will be injected

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
private static SomeDependency dependency; // Noncompliant, @Value will be ignored and no value will be injected
private static SomeDependency dependency; // Non compliant, @Value will be ignored and no value will be injected

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@erwan-serandour erwan-serandour changed the title SONARJAVA-5284 Create rule S7178 SONARJAVA-5284 Create rule S7178: Injecting data into static fields is not supported by Spring Jan 24, 2025
Copy link

Quality Gate passed Quality Gate passed for 'rspec-tools'

Issues
0 New issues
0 Fixed issues
0 Accepted issues

Measures
0 Security Hotspots
No data about Coverage
No data about Duplication

See analysis details on SonarQube

Copy link

Quality Gate passed Quality Gate passed for 'rspec-frontend'

Issues
0 New issues
0 Fixed issues
0 Accepted issues

Measures
0 Security Hotspots
No data about Coverage
No data about Duplication

See analysis details on SonarQube

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants