Skip to content

Commit

Permalink
SWEEP: Changed all applicable ObjectDisposedException creation statem…
Browse files Browse the repository at this point in the history
…ents into AlreadyClosedException.Create() (see #446)
  • Loading branch information
NightOwl888 committed Apr 26, 2021
1 parent 7eb7d74 commit f692ac5
Show file tree
Hide file tree
Showing 27 changed files with 138 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ public override object Clone()
{
if (_input == null)
{
throw new ObjectDisposedException(this.GetType().FullName, "this FieldsReader is closed");
throw AlreadyClosedException.Create(this.GetType().FullName, "this FieldsReader is disposed.");
}
return new SimpleTextStoredFieldsReader(_offsets, (IndexInput) _input.Clone(), _fieldInfos);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ public override object Clone()
{
if (_input == null)
{
throw new ObjectDisposedException(this.GetType().FullName, "this TermVectorsReader is closed");
throw AlreadyClosedException.Create(this.GetType().FullName, "this TermVectorsReader is disposed.");
}
return new SimpleTextTermVectorsReader(_offsets, (IndexInput)_input.Clone());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,7 @@ protected void EnsureOpen()
{
if (isClosed)
{
throw new ObjectDisposedException(this.GetType().FullName, "The taxonomy writer has already been closed");
throw AlreadyClosedException.Create(this.GetType().FullName, "The taxonomy writer has already been disposed.");
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Lucene.Net.Facet/Taxonomy/TaxonomyReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ protected void EnsureOpen()
{
if (RefCount <= 0)
{
throw new ObjectDisposedException(this.GetType().FullName, "this TaxonomyReader is closed");
throw AlreadyClosedException.Create(this.GetType().FullName, "this TaxonomyReader is disposed.");
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Lucene.Net.Replicator/Http/HttpClientBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ protected void EnsureOpen()
{
if (IsDisposed)
{
throw new ObjectDisposedException("HttpClient already disposed");
throw AlreadyClosedException.Create(this.GetType().FullName, "HttpClient already disposed.");
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/Lucene.Net.Replicator/LocalReplicator.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using J2N.Threading.Atomic;
using J2N.Threading.Atomic;
using System;
using System.Collections.Generic;
using System.Diagnostics;
Expand Down Expand Up @@ -164,7 +164,7 @@ protected void EnsureOpen()
if (!disposed)
return;

throw new ObjectDisposedException("This replicator has already been disposed");
throw AlreadyClosedException.Create(this.GetType().FullName, "This replicator has already been disposed.");
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/Lucene.Net.Replicator/ReplicationClient.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Lucene.Net.Store;
using Lucene.Net.Store;
using Lucene.Net.Support.Threading;
using Lucene.Net.Util;
using System;
Expand Down Expand Up @@ -320,7 +320,7 @@ protected void EnsureOpen()
if (!disposed)
return;

throw new ObjectDisposedException("this update client has already been closed");
throw AlreadyClosedException.Create(this.GetType().FullName, "this update client has already been disposed.");
}

// LUCENENET specific Utility Method
Expand Down
4 changes: 2 additions & 2 deletions src/Lucene.Net.Suggest/Spell/SpellChecker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -655,7 +655,7 @@ private void EnsureOpen()
{
if (disposed)
{
throw new ObjectDisposedException(this.GetType().FullName, "Spellchecker has been disposed.");
throw AlreadyClosedException.Create(this.GetType().FullName, "Spellchecker has been disposed.");
}
}

Expand Down Expand Up @@ -703,7 +703,7 @@ private void SwapSearcher(Directory dir)
if (disposed)
{
indexSearcher.IndexReader.Dispose();
throw new ObjectDisposedException(this.GetType().FullName, "Spellchecker has been disposed.");
throw AlreadyClosedException.Create(this.GetType().FullName, "Spellchecker has been disposed.");
}
searcher?.IndexReader?.Dispose();
// set the spellindex in the sync block - ensure consistency.
Expand Down
2 changes: 1 addition & 1 deletion src/Lucene.Net.Tests/Index/TestReaderClosed.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public virtual void TestReaderChaining()
//Assert.AreEqual("this IndexReader cannot be used anymore as one of its child readers was closed", ace.Message);
// LUCENENET specific - ObjectDisposedExeption appends the type of object to the end of the message,
// so we need to check the start of the message only.
assertTrue(ace.Message.StartsWith("this IndexReader cannot be used anymore as one of its child readers was closed", StringComparison.Ordinal));
assertTrue(ace.Message.StartsWith("this IndexReader cannot be used anymore as one of its child readers was disposed.", StringComparison.Ordinal));
}
finally
{
Expand Down
4 changes: 2 additions & 2 deletions src/Lucene.Net/Analysis/Analyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,7 @@ protected internal static object GetStoredValue(Analyzer analyzer) // LUCENENET:
{
if (analyzer.storedValue == null)
{
throw new ObjectDisposedException(analyzer.GetType().FullName, "this Analyzer is closed");
throw AlreadyClosedException.Create(analyzer.GetType().FullName, "this Analyzer is disposed.");
}
return analyzer.storedValue.Value;
}
Expand All @@ -626,7 +626,7 @@ protected internal static void SetStoredValue(Analyzer analyzer, object storedVa
{
if (analyzer.storedValue == null)
{
throw new ObjectDisposedException(analyzer.GetType().FullName, "this Analyzer is closed");
throw AlreadyClosedException.Create(analyzer.GetType().FullName, "this Analyzer is disposed.");
}
analyzer.storedValue.Value = storedValue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ private void EnsureOpen()
{
if (closed)
{
throw new ObjectDisposedException(this.GetType().FullName, "this FieldsReader is closed");
throw AlreadyClosedException.Create(this.GetType().FullName, "this FieldsReader is disposed.");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ private void EnsureOpen()
{
if (closed)
{
throw new ObjectDisposedException(this.GetType().FullName, "this FieldsReader is closed");
throw AlreadyClosedException.Create(this.GetType().FullName, "this FieldsReader is disposed.");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ private void EnsureOpen()
{
if (closed)
{
throw new ObjectDisposedException(this.GetType().FullName, "this FieldsReader is closed");
throw AlreadyClosedException.Create(this.GetType().FullName, "this FieldsReader is disposed.");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ private void EnsureOpen()
{
if (closed)
{
throw new ObjectDisposedException(this.GetType().FullName, "this FieldsReader is closed");
throw AlreadyClosedException.Create(this.GetType().FullName, "this FieldsReader is disposed.");
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/Lucene.Net/Index/DocumentsWriter.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using J2N.Threading.Atomic;
using J2N.Threading.Atomic;
using Lucene.Net.Diagnostics;
using System;
using System.Collections.Concurrent;
Expand Down Expand Up @@ -229,7 +229,7 @@ private void EnsureOpen()
{
if (closed)
{
throw new ObjectDisposedException(this.GetType().FullName, "this IndexWriter is closed");
throw AlreadyClosedException.Create(this.GetType().FullName, "this IndexWriter is disposed.");
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Lucene.Net/Index/IndexFileDeleter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ private void EnsureOpen()
{
if (writer == null)
{
throw new ObjectDisposedException(this.GetType().FullName, "this IndexWriter is closed");
throw AlreadyClosedException.Create(this.GetType().FullName, "this IndexWriter is disposed.");
}
else
{
Expand Down
6 changes: 3 additions & 3 deletions src/Lucene.Net/Index/IndexReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ public void DecRef()
// still close the reader if it was made invalid by a child:
if (refCount <= 0)
{
throw new ObjectDisposedException(this.GetType().FullName, "this IndexReader is closed");
throw AlreadyClosedException.Create(this.GetType().FullName, "this IndexReader is disposed.");
}

int rc = refCount.DecrementAndGet();
Expand Down Expand Up @@ -309,13 +309,13 @@ protected internal void EnsureOpen()
{
if (refCount <= 0)
{
throw new ObjectDisposedException(this.GetType().FullName, "this IndexReader is closed");
throw AlreadyClosedException.Create(this.GetType().FullName, "this IndexReader is disposed.");
}
// the happens before rule on reading the refCount, which must be after the fake write,
// ensures that we see the value:
if (closedByChild)
{
throw new ObjectDisposedException(this.GetType().FullName, "this IndexReader cannot be used anymore as one of its child readers was closed");
throw AlreadyClosedException.Create(this.GetType().FullName, "this IndexReader cannot be used anymore as one of its child readers was disposed.");
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Lucene.Net/Index/IndexWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -750,7 +750,7 @@ protected internal void EnsureOpen(bool failIfDisposing)
{
if (closed || (failIfDisposing && closing))
{
throw new ObjectDisposedException(this.GetType().FullName, "this IndexWriter is closed");
throw AlreadyClosedException.Create(this.GetType().FullName, "this IndexWriter is disposed.");
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Lucene.Net/Index/SegmentCoreReaders.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ internal void IncRef()
return;
}
}
throw new ObjectDisposedException(this.GetType().FullName, "SegmentCoreReaders is already closed");
throw AlreadyClosedException.Create(this.GetType().FullName, "SegmentCoreReaders is already disposed.");
}

internal NumericDocValues GetNormValues(FieldInfo fi)
Expand Down
6 changes: 3 additions & 3 deletions src/Lucene.Net/Search/ReferenceManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ namespace Lucene.Net.Search
public abstract class ReferenceManager<G> : IDisposable
where G : class //Make G nullable
{
private const string REFERENCE_MANAGER_IS_CLOSED_MSG = "this ReferenceManager is closed";
private const string REFERENCE_MANAGER_IS_CLOSED_MSG = "this ReferenceManager is disposed.";

// LUCENENET NOTE: changed this to be a private volatile field
// with a property to set/get it, since protected volatile
Expand All @@ -62,7 +62,7 @@ private void EnsureOpen()
{
if (current == null)
{
throw new ObjectDisposedException(this.GetType().FullName, REFERENCE_MANAGER_IS_CLOSED_MSG);
throw AlreadyClosedException.Create(this.GetType().FullName, REFERENCE_MANAGER_IS_CLOSED_MSG);
}
}

Expand Down Expand Up @@ -109,7 +109,7 @@ public G Acquire()
{
if ((@ref = current) == null)
{
throw new ObjectDisposedException(this.GetType().FullName, REFERENCE_MANAGER_IS_CLOSED_MSG);
throw AlreadyClosedException.Create(this.GetType().FullName, REFERENCE_MANAGER_IS_CLOSED_MSG);
}
if (TryIncRef(@ref))
{
Expand Down
2 changes: 1 addition & 1 deletion src/Lucene.Net/Search/SearcherLifetimeManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ private void EnsureOpen()
{
if (_closed)
{
throw new ObjectDisposedException(this.GetType().FullName, "this SearcherLifetimeManager instance is closed");
throw AlreadyClosedException.Create(this.GetType().FullName, "this SearcherLifetimeManager instance is disposed.");
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/Lucene.Net/Store/BaseDirectory.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Lucene.Net.Diagnostics;
using Lucene.Net.Diagnostics;
using System;

namespace Lucene.Net.Store
Expand Down Expand Up @@ -77,7 +77,7 @@ protected internal override sealed void EnsureOpen()
{
if (!IsOpen)
{
throw new ObjectDisposedException(this.GetType().FullName, "this Directory is closed");
throw AlreadyClosedException.Create(this.GetType().FullName, "this Directory is disposed.");
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Lucene.Net/Store/ByteBufferIndexInput.cs
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ private void EnsureOpen()
{
if (buffers is null)
{
throw new ObjectDisposedException(this.GetType().FullName, "Already closed: " + this);
throw AlreadyClosedException.Create(this.GetType().FullName, "Already disposed: " + this);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Lucene.Net/Store/CompoundFileWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ private void EnsureOpen()
{
if (closed)
{
throw new ObjectDisposedException(this.GetType().FullName, "CFS Directory is already closed");
throw AlreadyClosedException.Create(this.GetType().FullName, "CFS Directory is already disposed.");
}
}

Expand Down
15 changes: 10 additions & 5 deletions src/Lucene.Net/Store/FSDirectory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -499,8 +499,9 @@ public FSIndexOutput(FSDirectory parent, string name)
/// <inheritdoc/>
public override void WriteByte(byte b)
{
// LUCENENET specific: Guard to ensure we aren't disposed.
if (!isOpen)
throw new ObjectDisposedException(nameof(FSIndexOutput));
throw AlreadyClosedException.Create(this.GetType().FullName, "This FSIndexOutput is disposed.");

crc.Update(b);
file.WriteByte(b);
Expand All @@ -509,8 +510,9 @@ public override void WriteByte(byte b)
/// <inheritdoc/>
public override void WriteBytes(byte[] b, int offset, int length)
{
// LUCENENET specific: Guard to ensure we aren't disposed.
if (!isOpen)
throw new ObjectDisposedException(nameof(FSIndexOutput));
throw AlreadyClosedException.Create(this.GetType().FullName, "This FSIndexOutput is disposed.");

crc.Update(b, offset, length);
file.Write(b, offset, length);
Expand All @@ -519,8 +521,9 @@ public override void WriteBytes(byte[] b, int offset, int length)
/// <inheritdoc/>
protected internal override void FlushBuffer(byte[] b, int offset, int size)
{
// LUCENENET specific: Guard to ensure we aren't disposed.
if (!isOpen)
throw new ObjectDisposedException(nameof(FSIndexOutput));
throw AlreadyClosedException.Create(this.GetType().FullName, "This FSIndexOutput is disposed.");

crc.Update(b, offset, size);
file.Write(b, offset, size);
Expand All @@ -530,8 +533,9 @@ protected internal override void FlushBuffer(byte[] b, int offset, int size)
[MethodImpl(MethodImplOptions.NoInlining)]
public override void Flush()
{
// LUCENENET specific: Guard to ensure we aren't disposed.
if (!isOpen)
throw new ObjectDisposedException(nameof(FSIndexOutput));
throw AlreadyClosedException.Create(this.GetType().FullName, "This FSIndexOutput is disposed.");

file.Flush();
}
Expand Down Expand Up @@ -568,8 +572,9 @@ protected override void Dispose(bool disposing)
[Obsolete("(4.1) this method will be removed in Lucene 5.0")]
public override void Seek(long pos)
{
// LUCENENET specific: Guard to ensure we aren't disposed.
if (!isOpen)
throw new ObjectDisposedException(nameof(FSIndexOutput));
throw AlreadyClosedException.Create(this.GetType().FullName, "This FSIndexOutput is disposed.");

file.Seek(pos, SeekOrigin.Begin);
}
Expand Down
Loading

0 comments on commit f692ac5

Please sign in to comment.