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-configuration.git
commit 1f106d438951a40308fc297a5ee71b6fcebf06c7 Author: Gary Gregory <[email protected]> AuthorDate: Sat Aug 22 11:39:39 2026 -0400 Fix SpotBugs USO_UNSAFE_METHOD_SYNCHRONIZATION in FileBasedConfigurationBuilder. --- src/changes/changes.xml | 1 + .../builder/FileBasedConfigurationBuilder.java | 39 ++++++++++++++-------- 2 files changed, 27 insertions(+), 13 deletions(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index d285a67b3..f043f7447 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -28,6 +28,7 @@ <action type="fix" dev="ggregory" due-to="Gary Gregory">Add messages when throwing NullPointerException.</action> <action type="fix" dev="ggregory" due-to="Gary Gregory">Fix SpotBugs USO_UNSAFE_METHOD_SYNCHRONIZATION in BasicConfigurationBuilder.</action> <action type="fix" dev="ggregory" due-to="Gary Gregory">Fix SpotBugs USO_UNSAFE_METHOD_SYNCHRONIZATION in CombinedConfigurationBuilder.</action> + <action type="fix" dev="ggregory" due-to="Gary Gregory">Fix SpotBugs USO_UNSAFE_METHOD_SYNCHRONIZATION in FileBasedConfigurationBuilder.</action> <!-- ADD --> <!-- UPDATE --> <action type="update" dev="ggregory" due-to="Gary Gregory">Bump org.apache.commons:commons-parent from 100 to 103.</action> diff --git a/src/main/java/org/apache/commons/configuration2/builder/FileBasedConfigurationBuilder.java b/src/main/java/org/apache/commons/configuration2/builder/FileBasedConfigurationBuilder.java index d3c29f845..b7dc3c17b 100644 --- a/src/main/java/org/apache/commons/configuration2/builder/FileBasedConfigurationBuilder.java +++ b/src/main/java/org/apache/commons/configuration2/builder/FileBasedConfigurationBuilder.java @@ -128,6 +128,11 @@ public class FileBasedConfigurationBuilder<T extends FileBasedConfiguration> ext /** A flag whether the builder's parameters were reset. */ private boolean resetParameters; + /** + * Private lock for synchronizing access. + */ + private final Object lock = new Object(); + /** * Creates a new instance of {@code FileBasedConfigurationBuilder} which produces result objects of the specified class. * @@ -195,8 +200,10 @@ public class FileBasedConfigurationBuilder<T extends FileBasedConfiguration> ext * * @return The {@code FileHandler} associated with this builder */ - public synchronized FileHandler getFileHandler() { - return currentFileHandler != null ? currentFileHandler : fetchFileHandlerFromParameters(); + public FileHandler getFileHandler() { + synchronized (lock) { + return currentFileHandler != null ? currentFileHandler : fetchFileHandlerFromParameters(); + } } /** @@ -263,8 +270,10 @@ public class FileBasedConfigurationBuilder<T extends FileBasedConfiguration> ext * * @return <strong>true</strong> if auto save is enabled, <strong>false</strong> otherwise */ - public synchronized boolean isAutoSave() { - return autoSaveListener != null; + public boolean isAutoSave() { + synchronized (lock) { + return autoSaveListener != null; + } } /** @@ -295,11 +304,13 @@ public class FileBasedConfigurationBuilder<T extends FileBasedConfiguration> ext * * @param enabled <strong>true</strong> if auto save mode is to be enabled, <strong>false</strong> otherwise */ - public synchronized void setAutoSave(final boolean enabled) { - if (enabled) { - installAutoSaveListener(); - } else { - removeAutoSaveListener(); + public void setAutoSave(final boolean enabled) { + synchronized (lock) { + if (enabled) { + installAutoSaveListener(); + } else { + removeAutoSaveListener(); + } } } @@ -309,9 +320,11 @@ public class FileBasedConfigurationBuilder<T extends FileBasedConfiguration> ext * than reusing the existing one. */ @Override - public synchronized BasicConfigurationBuilder<T> setParameters(final Map<String, Object> params) { - super.setParameters(params); - resetParameters = true; - return this; + public BasicConfigurationBuilder<T> setParameters(final Map<String, Object> params) { + synchronized (lock) { + super.setParameters(params); + resetParameters = true; + return this; + } } }
