From 0ecb1d097fd9cefd1571218cfb1627ae1fc52e7b Mon Sep 17 00:00:00 2001 From: Judit Knoll Date: Fri, 22 Nov 2024 11:43:11 +0100 Subject: [PATCH] add more test for BadVisibilityOnSharedPrimitiveVariables --- ...ibilityOnSharedPrimitiveVariablesTest.java | 7 ++++++ ...hronizedBlockWithBadVisibilityOnField.java | 22 +++++++++++++++++++ 2 files changed, 29 insertions(+) create mode 100644 spotbugsTestCases/src/java/multithreaded/sharedPrimitiveVariables/SynchronizedBlockWithBadVisibilityOnField.java diff --git a/spotbugs-tests/src/test/java/edu/umd/cs/findbugs/detect/BadVisibilityOnSharedPrimitiveVariablesTest.java b/spotbugs-tests/src/test/java/edu/umd/cs/findbugs/detect/BadVisibilityOnSharedPrimitiveVariablesTest.java index 5dfa346740e..542a80a3d62 100644 --- a/spotbugs-tests/src/test/java/edu/umd/cs/findbugs/detect/BadVisibilityOnSharedPrimitiveVariablesTest.java +++ b/spotbugs-tests/src/test/java/edu/umd/cs/findbugs/detect/BadVisibilityOnSharedPrimitiveVariablesTest.java @@ -13,6 +13,13 @@ void failurePath_fieldWithBadVisibility_whenOtherMethodHasSynchronizedBlock() { assertBugInMethodAtLine(BUG_TYPE, "SynchronizedBlockAndBadVisibilityOnField", "shutdown", 17); } + @Test + void failurePath_fieldWithBadVisibility_whenMethodHasIrrelevantSynchronizedBlock() { + performAnalysis("multithreaded/sharedPrimitiveVariables/SynchronizedBlockWithBadVisibilityOnField.class"); + assertBugTypeCount(BUG_TYPE, 1); + assertBugInMethodAtLine(BUG_TYPE, "SynchronizedBlockWithBadVisibilityOnField", "shutdown", 20); + } + @Test void failurePath_fieldWithBadVisibility_whenOtherMethodIsSynchronized() { performAnalysis("multithreaded/sharedPrimitiveVariables/SynchronizedMethodAndBadVisibilityOnField.class"); diff --git a/spotbugsTestCases/src/java/multithreaded/sharedPrimitiveVariables/SynchronizedBlockWithBadVisibilityOnField.java b/spotbugsTestCases/src/java/multithreaded/sharedPrimitiveVariables/SynchronizedBlockWithBadVisibilityOnField.java new file mode 100644 index 00000000000..ad7c906b654 --- /dev/null +++ b/spotbugsTestCases/src/java/multithreaded/sharedPrimitiveVariables/SynchronizedBlockWithBadVisibilityOnField.java @@ -0,0 +1,22 @@ +package multithreaded.sharedPrimitiveVariables; + +public class SynchronizedBlockWithBadVisibilityOnField { + private boolean done = false; + + public void run() { + synchronized (SynchronizedBlockWithBadVisibilityOnField.class) { + System.out.println("this is synchronized, but not the whole method"); + } + while (!done) { + try { + Thread.currentThread().sleep(1000); + } catch(InterruptedException ie) { + Thread.currentThread().interrupt(); + } + } + } + + public void shutdown() { + done = true; + } +}