From 3a6d6bcfe14238f994d35b7c53e9e91256e2ea24 Mon Sep 17 00:00:00 2001 From: LeanBitLab <245915690+LeanBitLab@users.noreply.github.com> Date: Tue, 12 May 2026 18:46:27 +0000 Subject: [PATCH] Refactor BinaryDictionary to enforce lock naming convention Extract synchronized blocks into inner methods suffixed with 'Locked' and adjust 'closeInternalLocked' to remove the synchronized modifier, moving the lock acquisition to its callers. This ensures adherence to the convention where 'Locked' suffixed methods expect the caller to hold the lock. --- .../inputmethod/latin/BinaryDictionary.java | 45 ++++++++++++------- 1 file changed, 28 insertions(+), 17 deletions(-) diff --git a/app/src/main/java/com/android/inputmethod/latin/BinaryDictionary.java b/app/src/main/java/com/android/inputmethod/latin/BinaryDictionary.java index 0aa62b079..a8fd5ad1f 100644 --- a/app/src/main/java/com/android/inputmethod/latin/BinaryDictionary.java +++ b/app/src/main/java/com/android/inputmethod/latin/BinaryDictionary.java @@ -39,7 +39,6 @@ /** * Implements a static, compacted, binary dictionary of standard words. */ -// TODO: All methods which should be locked need to have a suffix "Locked". public final class BinaryDictionary extends Dictionary { private static final String TAG = BinaryDictionary.class.getSimpleName(); @@ -88,13 +87,17 @@ public final class BinaryDictionary extends Dictionary { // {@code mDicTraverseSessions}. private DicTraverseSession getTraverseSession(final int traverseSessionId) { synchronized(mDicTraverseSessions) { - DicTraverseSession traverseSession = mDicTraverseSessions.get(traverseSessionId); - if (traverseSession == null) { - traverseSession = new DicTraverseSession(mLocale, mNativeDict, mDictSize); - mDicTraverseSessions.put(traverseSessionId, traverseSession); - } - return traverseSession; + return getTraverseSessionLocked(traverseSessionId); + } + } + + private DicTraverseSession getTraverseSessionLocked(final int traverseSessionId) { + DicTraverseSession traverseSession = mDicTraverseSessions.get(traverseSessionId); + if (traverseSession == null) { + traverseSession = new DicTraverseSession(mLocale, mNativeDict, mDictSize); + mDicTraverseSessions.put(traverseSessionId, traverseSession); } + return traverseSession; } /** @@ -624,19 +627,25 @@ public boolean shouldAutoCommit(final SuggestedWordInfo candidate) { @Override public void close() { synchronized (mDicTraverseSessions) { - final int sessionsSize = mDicTraverseSessions.size(); - for (int index = 0; index < sessionsSize; ++index) { - final DicTraverseSession traverseSession = mDicTraverseSessions.valueAt(index); - if (traverseSession != null) { - traverseSession.close(); - } + closeDicTraverseSessionsLocked(); + } + synchronized (this) { + closeInternalLocked(); + } + } + + private void closeDicTraverseSessionsLocked() { + final int sessionsSize = mDicTraverseSessions.size(); + for (int index = 0; index < sessionsSize; ++index) { + final DicTraverseSession traverseSession = mDicTraverseSessions.valueAt(index); + if (traverseSession != null) { + traverseSession.close(); } - mDicTraverseSessions.clear(); } - closeInternalLocked(); + mDicTraverseSessions.clear(); } - private synchronized void closeInternalLocked() { + private void closeInternalLocked() { if (mNativeDict != 0) { closeNative(mNativeDict); mNativeDict = 0; @@ -647,7 +656,9 @@ private synchronized void closeInternalLocked() { @Override protected void finalize() throws Throwable { try { - closeInternalLocked(); + synchronized (this) { + closeInternalLocked(); + } } finally { super.finalize(); }