-
Notifications
You must be signed in to change notification settings - Fork 724
SONARJAVA-6721 Extend rule S2442 to all "java.util.concurrent" objects #5890
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
Draft
NoemieBenard
wants to merge
8
commits into
master
Choose a base branch
from
nb/sonarjava-6721-implement-S9141
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
44356f7
Add rule metadata
NoemieBenard de5f628
Add failing reproducer
NoemieBenard 85b7a23
Implement rule
NoemieBenard 81bde3f
Update ruling expectations
NoemieBenard 42a1522
Fix review comments
NoemieBenard d457829
Update rule metadata
NoemieBenard 2cb59a4
Add test without semantic
NoemieBenard f78f122
Merge S9141 into S2442
NoemieBenard File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
42 changes: 0 additions & 42 deletions
42
java-checks-test-sources/default/src/main/java/checks/SynchronizedLockCheckSample.java
This file was deleted.
Oops, something went wrong.
134 changes: 134 additions & 0 deletions
134
...-test-sources/default/src/main/java/checks/SynchronizedOnConcurrentObjectCheckSample.java
This file contains hidden or 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,134 @@ | ||
| package checks; | ||
|
|
||
| import java.util.concurrent.ArrayBlockingQueue; | ||
| import java.util.concurrent.BlockingQueue; | ||
| import java.util.concurrent.ConcurrentHashMap; | ||
| import java.util.concurrent.CountDownLatch; | ||
| import java.util.concurrent.CyclicBarrier; | ||
| import java.util.concurrent.Future; | ||
| import java.util.concurrent.LinkedBlockingQueue; | ||
| import java.util.concurrent.Semaphore; | ||
| import java.util.concurrent.TimeUnit; | ||
| import java.util.concurrent.atomic.AtomicBoolean; | ||
| import java.util.concurrent.atomic.AtomicInteger; | ||
| import java.util.concurrent.locks.Condition; | ||
| import java.util.concurrent.locks.Lock; | ||
| import java.util.concurrent.locks.ReentrantLock; | ||
| import java.util.concurrent.locks.ReentrantReadWriteLock; | ||
|
|
||
| class SynchronizedOnConcurrentObjectCheckSample { | ||
|
|
||
| private final ReentrantLock reentrantLock = new ReentrantLock(); | ||
| private final ReentrantReadWriteLock rwLock = new ReentrantReadWriteLock(); | ||
| private final Lock lock = new ReentrantLock(); | ||
| private final Semaphore semaphore = new Semaphore(1); | ||
| private final CountDownLatch latch = new CountDownLatch(1); | ||
| private final CyclicBarrier barrier = new CyclicBarrier(2); | ||
| private final BlockingQueue<String> blockingQueue = new ArrayBlockingQueue<>(10); | ||
| private final ArrayBlockingQueue<String> arrayBlockingQueue = new ArrayBlockingQueue<>(10); | ||
| private final LinkedBlockingQueue<String> linkedBlockingQueue = new LinkedBlockingQueue<>(); | ||
| private final AtomicBoolean atomicBoolean = new AtomicBoolean(); | ||
| private final AtomicInteger atomicInteger = new AtomicInteger(); | ||
| private final CustomLock customLock = new CustomLock(); | ||
| private final CustomLockImpl customLockImpl = new CustomLockImpl(); | ||
|
|
||
| private final Object objectLock = new Object(); | ||
| private final ConcurrentHashMap<String, String> concurrentMap = new ConcurrentHashMap<>(); | ||
| private Future<?> future; | ||
|
|
||
| void noncompliant() { | ||
| synchronized (reentrantLock) { // Noncompliant {{Use the "ReentrantLock" API for synchronization instead of a "synchronized" block.}} | ||
| // ^^^^^^^^^^^^^ | ||
| } | ||
|
|
||
| synchronized (lock) { // Noncompliant {{Use the "Lock" API for synchronization instead of a "synchronized" block.}} | ||
| // ^^^^ | ||
| } | ||
|
|
||
| synchronized (rwLock) { // Noncompliant {{Use the "ReentrantReadWriteLock" API for synchronization instead of a "synchronized" block.}} | ||
| // ^^^^^^ | ||
| } | ||
|
|
||
| synchronized (semaphore) { // Noncompliant {{Use the "Semaphore" API for synchronization instead of a "synchronized" block.}} | ||
| // ^^^^^^^^^ | ||
| } | ||
|
|
||
| synchronized (latch) { // Noncompliant {{Use the "CountDownLatch" API for synchronization instead of a "synchronized" block.}} | ||
| // ^^^^^ | ||
| } | ||
|
|
||
| synchronized (barrier) { // Noncompliant {{Use the "CyclicBarrier" API for synchronization instead of a "synchronized" block.}} | ||
| // ^^^^^^^ | ||
| } | ||
|
|
||
| synchronized (blockingQueue) { // Noncompliant {{Use the "BlockingQueue" API for synchronization instead of a "synchronized" block.}} | ||
| // ^^^^^^^^^^^^^ | ||
| } | ||
|
|
||
| synchronized (arrayBlockingQueue) { // Noncompliant {{Use the "ArrayBlockingQueue" API for synchronization instead of a "synchronized" block.}} | ||
| // ^^^^^^^^^^^^^^^^^^ | ||
| } | ||
|
|
||
| synchronized (linkedBlockingQueue) { // Noncompliant {{Use the "LinkedBlockingQueue" API for synchronization instead of a "synchronized" block.}} | ||
| // ^^^^^^^^^^^^^^^^^^^ | ||
| } | ||
|
|
||
| synchronized (atomicBoolean) { // Noncompliant {{Use the "AtomicBoolean" API for synchronization instead of a "synchronized" block.}} | ||
| // ^^^^^^^^^^^^^ | ||
| } | ||
|
|
||
| synchronized (atomicInteger) { // Noncompliant {{Use the "AtomicInteger" API for synchronization instead of a "synchronized" block.}} | ||
| // ^^^^^^^^^^^^^ | ||
| } | ||
|
|
||
| synchronized (customLock) { // Noncompliant {{Use the "CustomLock" API for synchronization instead of a "synchronized" block.}} | ||
| // ^^^^^^^^^^ | ||
| } | ||
|
|
||
| synchronized (customLockImpl) { // Noncompliant {{Use the "CustomLockImpl" API for synchronization instead of a "synchronized" block.}} | ||
| // ^^^^^^^^^^^^^^ | ||
| } | ||
| } | ||
|
|
||
| void compliant() { | ||
| synchronized (objectLock) { | ||
| } | ||
|
|
||
| synchronized (concurrentMap) { | ||
| } | ||
|
|
||
| synchronized (future) { | ||
| } | ||
|
|
||
| reentrantLock.lock(); | ||
| try { | ||
| } finally { | ||
| reentrantLock.unlock(); | ||
| } | ||
|
|
||
| rwLock.writeLock().lock(); | ||
| try { | ||
| } finally { | ||
| rwLock.writeLock().unlock(); | ||
| } | ||
| } | ||
|
|
||
| void example() { | ||
| var lock2 = new ReentrantLock(); | ||
| synchronized (lock2) { // Noncompliant | ||
| } | ||
| } | ||
|
|
||
| static class CustomLock extends ReentrantLock { | ||
| } | ||
|
|
||
| // Custom Lock implementation outside java.util.concurrent.locks — caught via isSubtypeOf(Lock) | ||
| static class CustomLockImpl implements Lock { | ||
| @Override public void lock() {} | ||
| @Override public void lockInterruptibly() throws InterruptedException {} | ||
| @Override public boolean tryLock() { return false; } | ||
| @Override public boolean tryLock(long time, TimeUnit unit) throws InterruptedException { return false; } | ||
| @Override public void unlock() {} | ||
| @Override public Condition newCondition() { return null; } | ||
| } | ||
| } |
45 changes: 0 additions & 45 deletions
45
java-checks/src/main/java/org/sonar/java/checks/SynchronizedLockCheck.java
This file was deleted.
Oops, something went wrong.
73 changes: 73 additions & 0 deletions
73
java-checks/src/main/java/org/sonar/java/checks/SynchronizedOnConcurrentObjectCheck.java
This file contains hidden or 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,73 @@ | ||
| /* | ||
| * SonarQube Java | ||
| * Copyright (C) SonarSource Sàrl | ||
| * mailto:info AT sonarsource DOT com | ||
| * | ||
| * You can redistribute and/or modify this program under the terms of | ||
| * the Sonar Source-Available License Version 1, as published by SonarSource Sàrl. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
| * See the Sonar Source-Available License for more details. | ||
| * | ||
| * You should have received a copy of the Sonar Source-Available License | ||
| * along with this program; if not, see https://sonarsource.com/license/ssal/ | ||
| */ | ||
| package org.sonar.java.checks; | ||
|
|
||
| import java.util.Collections; | ||
| import java.util.List; | ||
| import java.util.Set; | ||
| import org.sonar.check.Rule; | ||
| import org.sonar.plugins.java.api.IssuableSubscriptionVisitor; | ||
| import org.sonar.plugins.java.api.semantic.Type; | ||
| import org.sonar.plugins.java.api.tree.ExpressionTree; | ||
| import org.sonar.plugins.java.api.tree.SynchronizedStatementTree; | ||
| import org.sonar.plugins.java.api.tree.Tree; | ||
| import org.sonar.plugins.java.api.tree.Tree.Kind; | ||
|
|
||
| @Rule(key = "S2442") | ||
| public class SynchronizedOnConcurrentObjectCheck extends IssuableSubscriptionVisitor { | ||
|
|
||
| private static final String CONCURRENT_LOCKS_PREFIX = "java.util.concurrent.locks."; | ||
| private static final String CONCURRENT_ATOMIC_PREFIX = "java.util.concurrent.atomic."; | ||
| private static final Set<String> CONCURRENT_SYNC_TYPES = Set.of( | ||
| "java.util.concurrent.Semaphore", | ||
| "java.util.concurrent.CountDownLatch", | ||
| "java.util.concurrent.CyclicBarrier", | ||
| "java.util.concurrent.Exchanger", | ||
| "java.util.concurrent.Phaser", | ||
| "java.util.concurrent.BlockingQueue", | ||
| "java.util.concurrent.BlockingDeque", | ||
| "java.util.concurrent.TransferQueue"); | ||
|
|
||
| @Override | ||
| public List<Kind> nodesToVisit() { | ||
| return Collections.singletonList(Kind.SYNCHRONIZED_STATEMENT); | ||
| } | ||
|
|
||
| @Override | ||
| public void visitNode(Tree tree) { | ||
| ExpressionTree expression = ((SynchronizedStatementTree) tree).expression(); | ||
| Type type = expression.symbolType(); | ||
| if (isSynchronizationPrimitive(type)) { | ||
| reportIssue(expression, String.format( | ||
| "Use the \"%s\" API for synchronization instead of a \"synchronized\" block.", type.name())); | ||
| } | ||
| } | ||
|
|
||
| private static boolean isSynchronizationPrimitive(Type type) { | ||
| return type.isSubtypeOf("java.util.concurrent.locks.Lock") | ||
| || isKnownSyncPrimitive(type) | ||
| || type.symbol().superTypes().stream().anyMatch(SynchronizedOnConcurrentObjectCheck::isKnownSyncPrimitive); | ||
| } | ||
|
|
||
| private static boolean isKnownSyncPrimitive(Type type) { | ||
| String fqn = type.fullyQualifiedName(); | ||
| return fqn.startsWith(CONCURRENT_LOCKS_PREFIX) | ||
| || fqn.startsWith(CONCURRENT_ATOMIC_PREFIX) | ||
| || CONCURRENT_SYNC_TYPES.contains(fqn); | ||
| } | ||
|
|
||
| } | ||
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -21,13 +21,23 @@ | |
|
|
||
| import static org.sonar.java.checks.verifier.TestUtils.mainCodeSourcesPath; | ||
|
|
||
| class SynchronizedLockCheckTest { | ||
| class SynchronizedOnConcurrentObjectCheckTest { | ||
|
|
||
| @Test | ||
| void test() { | ||
| CheckVerifier.newVerifier() | ||
| .onFile(mainCodeSourcesPath("checks/SynchronizedLockCheckSample.java")) | ||
| .withCheck(new SynchronizedLockCheck()) | ||
| .onFile(mainCodeSourcesPath("checks/SynchronizedOnConcurrentObjectCheckSample.java")) | ||
| .withCheck(new SynchronizedOnConcurrentObjectCheck()) | ||
| .verifyIssues(); | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we also test withoutSemantic? In preparation for getting rid of autoscan. |
||
|
|
||
| @Test | ||
| void test_without_semantic() { | ||
| CheckVerifier.newVerifier() | ||
| .onFile(mainCodeSourcesPath("checks/SynchronizedOnConcurrentObjectCheckSample.java")) | ||
| .withCheck(new SynchronizedOnConcurrentObjectCheck()) | ||
| .withoutSemantic() | ||
| .verifyIssues(); | ||
| } | ||
|
gitar-bot[bot] marked this conversation as resolved.
|
||
|
|
||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This PR re-points the check to rule S2442 and extends it to flag Semaphore, CountDownLatch, CyclicBarrier, atomics, blocking queues, etc., but S2442.json/html still only describe synchronizing on a "Lock" object (title: "Synchronizing on a "Lock" object should be avoided", HTML shows only a Lock example). Users now get issues on non-Lock concurrent types with documentation that doesn't cover them. Update the S2442 title and HTML (why/noncompliant/compliant examples) to reflect the full set of java.util.concurrent synchronization primitives the check reports.
Was this helpful? React with 👍 / 👎