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

Rule Request or Fix: Avoid wrong use of @GuardedBy #537

Open
jborgers opened this issue Feb 12, 2025 · 0 comments
Open

Rule Request or Fix: Avoid wrong use of @GuardedBy #537

jborgers opened this issue Feb 12, 2025 · 0 comments

Comments

@jborgers
Copy link
Owner

jborgers commented Feb 12, 2025

@ GuardedBy is used is wrong apparently to get rid of violations which indicate concurrency bugs, so high risk. Related to rule: NotProperlySynchronizingOnFieldWhileUsingGuardedBy

Example 1:

class Foo {
   @GuardedBy("lock")
    private Map<String, Product> cachedData = new HashMap<>();
}

And no lock field exists.

Example 2:

@Component
@ConfigurationProperties(prefix = "my-application")
@Getter
@Setter
public class MyApplicationConfig {
    
    @GuardedBy("this")
    private String bannerText;
    @GuardedBy("this")
    private String buttonText;

}

There is no synchronized on "this" involved. Used to get rid of the violation, it this case because of the @Component annotation. Proper fix is to make it @Configuration instead of @Component.

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

No branches or pull requests

1 participant