This is an automated email from the ASF dual-hosted git repository.

garydgregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-bcel.git

commit 2925ec539ed84ba678118e9ec6b738a8e6854587
Author: Gary Gregory <[email protected]>
AuthorDate: Sat Aug 22 08:19:28 2026 -0400

    Fix SpotBugs USO_UNSAFE_METHOD_SYNCHRONIZATION in ConstantUtf8.
---
 src/changes/changes.xml                            |  1 +
 .../org/apache/bcel/classfile/ConstantUtf8.java    | 35 ++++++++++++----------
 2 files changed, 21 insertions(+), 15 deletions(-)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 7902132d..d3eb71f9 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -87,6 +87,7 @@ The <action> type attribute can be add,update,fix,remove.
       <action                  type="fix" dev="ggregory" due-to="Naveed Khan, 
Gary Gregory">Validate u1 count bound in INVOKEINTERFACE and MULTIANEWARRAY 
(#523).</action>
       <action                  type="fix" dev="ggregory" due-to="Naveed Khan, 
Gary Gregory">Match wide local variable instruction length to dumped bytes 
(#525).</action>
       <action                  type="fix" dev="ggregory" due-to="Naveed Khan, 
Gary Gregory">Bound IINC increment to signed short (#526).</action>
+      <action                  type="fix" dev="ggregory" due-to="Gary 
Gregory">Fix SpotBugs USO_UNSAFE_METHOD_SYNCHRONIZATION in 
ConstantUtf8.</action>
       <!-- ADD -->
       <action                  type="add" dev="ggregory" due-to="nbauma109, 
Gary Gregory">Add support for permitted subclasses #493.</action>
       <action                  type="add" dev="ggregory" due-to="nbauma109, 
Gary Gregory">Add RecordComponentInfo.getAttribute(byte tag)#494.</action>
diff --git a/src/main/java/org/apache/bcel/classfile/ConstantUtf8.java 
b/src/main/java/org/apache/bcel/classfile/ConstantUtf8.java
index 151e1b21..f15de903 100644
--- a/src/main/java/org/apache/bcel/classfile/ConstantUtf8.java
+++ b/src/main/java/org/apache/bcel/classfile/ConstantUtf8.java
@@ -84,6 +84,7 @@ public final class ConstantUtf8 extends Constant {
 
     }
 
+    private static final Object LOCK = new Object();
     // TODO these should perhaps be AtomicInt?
     private static volatile int considered;
     private static volatile int created;
@@ -105,8 +106,10 @@ public final class ConstantUtf8 extends Constant {
      *
      * @since 6.4.0
      */
-    public static synchronized void clearCache() {
-        Cache.CACHE.clear();
+    public static void clearCache() {
+        synchronized (LOCK) {
+            Cache.CACHE.clear();
+        }
     }
 
     // for access by test code
@@ -129,21 +132,23 @@ public final class ConstantUtf8 extends Constant {
      * @return A new or cached instance of the given value.
      * @since 6.0
      */
-    public static synchronized ConstantUtf8 getCachedInstance(final String 
value) {
-        if (value.length() > Cache.MAX_ENTRY_SIZE) {
-            skipped++;
-            return new ConstantUtf8(value);
-        }
-        considered++;
-        synchronized (ConstantUtf8.class) { // might be better with a specific 
lock object
-            ConstantUtf8 result = Cache.CACHE.get(value);
-            if (result != null) {
-                hits++;
+    public static ConstantUtf8 getCachedInstance(final String value) {
+        synchronized (LOCK) {
+            if (value.length() > Cache.MAX_ENTRY_SIZE) {
+                skipped++;
+                return new ConstantUtf8(value);
+            }
+            considered++;
+            synchronized (ConstantUtf8.class) { // might be better with a 
specific lock object
+                ConstantUtf8 result = Cache.CACHE.get(value);
+                if (result != null) {
+                    hits++;
+                    return result;
+                }
+                result = new ConstantUtf8(value);
+                Cache.CACHE.put(value, result);
                 return result;
             }
-            result = new ConstantUtf8(value);
-            Cache.CACHE.put(value, result);
-            return result;
         }
     }
 

Reply via email to