From e95e2506b0f01c2cf3f1ca56a064c5a3a334e1fc Mon Sep 17 00:00:00 2001 From: Paul Irwin Date: Mon, 18 Nov 2024 22:35:07 -0700 Subject: [PATCH] Fix incorrect usage of argument exceptions for Sonar csharpsquid:S3928, #682 --- .../Analysis/Shingle/ShingleFilterFactory.cs | 16 +++--- .../Analysis/Synonym/SynonymFilter.cs | 2 +- .../Analysis/Synonym/SynonymMap.cs | 16 +++--- .../Analysis/Util/CharacterUtils.cs | 23 ++++---- .../JapaneseKatakanaStemFilterFactory.cs | 9 ++-- .../Language/Bm/PhoneticEngine.cs | 2 +- src/Lucene.Net.Benchmark/ByTask/Benchmark.cs | 2 +- .../Quality/Trec/QueryDriver.cs | 2 +- .../Quality/Utils/QualityQueriesFinder.cs | 2 +- .../Utils/ExtractWikipedia.cs | 2 +- .../Taxonomy/PrintTaxonomyStats.cs | 8 +-- .../VectorHighlight/BaseFragListBuilder.cs | 2 +- src/Lucene.Net.Misc/Index/IndexSplitter.cs | 2 +- .../Index/MultiPassIndexSplitter.cs | 6 +-- src/Lucene.Net.Misc/Misc/GetTermInfo.cs | 2 +- src/Lucene.Net.Misc/Misc/HighFreqTerms.cs | 2 +- src/Lucene.Net.Misc/Misc/IndexMergeTool.cs | 2 +- src/Lucene.Net.Queries/TermFilter.cs | 6 +-- .../Suggest/Fst/FSTCompletionBuilder.cs | 2 +- src/Lucene.Net/Index/IndexUpgrader.cs | 2 +- src/Lucene.Net/Index/IndexWriterConfig.cs | 52 +++++++++---------- .../Search/MinShouldMatchSumScorer.cs | 4 +- src/Lucene.Net/Store/CompoundFileWriter.cs | 4 +- src/Lucene.Net/Store/LockStressTest.cs | 2 +- src/Lucene.Net/Store/LockVerifyServer.cs | 2 +- .../Util/Automaton/BasicAutomata.cs | 17 ++++-- src/Lucene.Net/Util/UnicodeUtil.cs | 18 +++---- 27 files changed, 112 insertions(+), 97 deletions(-) diff --git a/src/Lucene.Net.Analysis.Common/Analysis/Shingle/ShingleFilterFactory.cs b/src/Lucene.Net.Analysis.Common/Analysis/Shingle/ShingleFilterFactory.cs index 75b5529b94..a30e93c1a3 100644 --- a/src/Lucene.Net.Analysis.Common/Analysis/Shingle/ShingleFilterFactory.cs +++ b/src/Lucene.Net.Analysis.Common/Analysis/Shingle/ShingleFilterFactory.cs @@ -43,23 +43,25 @@ public class ShingleFilterFactory : TokenFilterFactory private readonly string fillerToken; /// - /// Creates a new - public ShingleFilterFactory(IDictionary args) + /// Creates a new + /// + /// Thrown if any of the key/value pairs are invalid. + public ShingleFilterFactory(IDictionary args) : base(args) { maxShingleSize = GetInt32(args, "maxShingleSize", ShingleFilter.DEFAULT_MAX_SHINGLE_SIZE); if (maxShingleSize < 2) { - throw new ArgumentOutOfRangeException(nameof(maxShingleSize), "Invalid maxShingleSize (" + maxShingleSize + ") - must be at least 2"); // LUCENENET specific - changed from IllegalArgumentException to ArgumentOutOfRangeException (.NET convention) + throw new ArgumentException("Invalid maxShingleSize (" + maxShingleSize + ") - must be at least 2", nameof(args)); } minShingleSize = GetInt32(args, "minShingleSize", ShingleFilter.DEFAULT_MIN_SHINGLE_SIZE); if (minShingleSize < 2) { - throw new ArgumentOutOfRangeException(nameof(minShingleSize), "Invalid minShingleSize (" + minShingleSize + ") - must be at least 2"); // LUCENENET specific - changed from IllegalArgumentException to ArgumentOutOfRangeException (.NET convention) + throw new ArgumentException("Invalid minShingleSize (" + minShingleSize + ") - must be at least 2", nameof(args)); } if (minShingleSize > maxShingleSize) { - throw new ArgumentException("Invalid minShingleSize (" + minShingleSize + ") - must be no greater than maxShingleSize (" + maxShingleSize + ")"); + throw new ArgumentException("Invalid minShingleSize (" + minShingleSize + ") - must be no greater than maxShingleSize (" + maxShingleSize + ")", nameof(args)); } outputUnigrams = GetBoolean(args, "outputUnigrams", true); outputUnigramsIfNoShingles = GetBoolean(args, "outputUnigramsIfNoShingles", false); @@ -67,7 +69,7 @@ public ShingleFilterFactory(IDictionary args) fillerToken = Get(args, "fillerToken", ShingleFilter.DEFAULT_FILLER_TOKEN); if (args.Count > 0) { - throw new ArgumentException(string.Format(J2N.Text.StringFormatter.CurrentCulture, "Unknown parameters: {0}", args)); + throw new ArgumentException(string.Format(J2N.Text.StringFormatter.CurrentCulture, "Unknown parameters: {0}", args), nameof(args)); } } @@ -81,4 +83,4 @@ public override TokenStream Create(TokenStream input) return r; } } -} \ No newline at end of file +} diff --git a/src/Lucene.Net.Analysis.Common/Analysis/Synonym/SynonymFilter.cs b/src/Lucene.Net.Analysis.Common/Analysis/Synonym/SynonymFilter.cs index 07a3a28026..d4af2d792c 100644 --- a/src/Lucene.Net.Analysis.Common/Analysis/Synonym/SynonymFilter.cs +++ b/src/Lucene.Net.Analysis.Common/Analysis/Synonym/SynonymFilter.cs @@ -273,7 +273,7 @@ public SynonymFilter(TokenStream input, SynonymMap synonyms, bool ignoreCase) this.fst = synonyms.Fst; if (fst is null) { - throw new ArgumentNullException(nameof(synonyms.Fst), "fst must be non-null"); // LUCENENET specific - changed from IllegalArgumentException to ArgumentNullException (.NET convention) + throw new ArgumentException("synonyms.Fst must be non-null", nameof(synonyms)); } this.fstReader = fst.GetBytesReader(); diff --git a/src/Lucene.Net.Analysis.Common/Analysis/Synonym/SynonymMap.cs b/src/Lucene.Net.Analysis.Common/Analysis/Synonym/SynonymMap.cs index ea6329725b..4f3f66adee 100644 --- a/src/Lucene.Net.Analysis.Common/Analysis/Synonym/SynonymMap.cs +++ b/src/Lucene.Net.Analysis.Common/Analysis/Synonym/SynonymMap.cs @@ -80,7 +80,7 @@ public class Builder /// /// If dedup is true then identical rules (same input, - /// same output) will be added only once. + /// same output) will be added only once. /// public Builder(bool dedup) { @@ -95,9 +95,9 @@ internal class MapEntry } /// - /// Sugar: just joins the provided terms with + /// Sugar: just joins the provided terms with /// . reuse and its chars - /// must not be null. + /// must not be null. /// public static CharsRef Join(string[] words, CharsRef reuse) { @@ -163,7 +163,7 @@ internal virtual void Add(CharsRef input, int numInputWords, CharsRef output, in } if (input.Length <= 0) { - throw new ArgumentOutOfRangeException(nameof(input.Length), "input.Length must be > 0 (got " + input.Length + ")"); // LUCENENET specific - changed from IllegalArgumentException to ArgumentOutOfRangeException (.NET convention) + throw new ArgumentException("input.Length must be > 0 (got " + input.Length + ")", nameof(input)); } if (numOutputWords <= 0) { @@ -171,7 +171,7 @@ internal virtual void Add(CharsRef input, int numInputWords, CharsRef output, in } if (output.Length <= 0) { - throw new ArgumentOutOfRangeException(nameof(output.Length), "output.Length must be > 0 (got " + output.Length + ")"); // LUCENENET specific - changed from IllegalArgumentException to ArgumentOutOfRangeException (.NET convention) + throw new ArgumentException("output.Length must be > 0 (got " + output.Length + ")", nameof(output)); } if (Debugging.AssertsEnabled) @@ -332,7 +332,7 @@ public virtual SynonymMap Build() /// /// Abstraction for parsing synonym files. - /// + /// /// @lucene.experimental /// public abstract class Parser : Builder @@ -353,7 +353,7 @@ protected Parser(bool dedup, Analyzer analyzer) // LUCENENET: CA1012: Abstract t /// /// Sugar: analyzes the text with the analyzer and /// separates by . - /// reuse and its chars must not be null. + /// reuse and its chars must not be null. /// public virtual CharsRef Analyze(string text, CharsRef reuse) { @@ -394,4 +394,4 @@ public virtual CharsRef Analyze(string text, CharsRef reuse) } } } -} \ No newline at end of file +} diff --git a/src/Lucene.Net.Analysis.Common/Analysis/Util/CharacterUtils.cs b/src/Lucene.Net.Analysis.Common/Analysis/Util/CharacterUtils.cs index 179e095b20..669d16f873 100644 --- a/src/Lucene.Net.Analysis.Common/Analysis/Util/CharacterUtils.cs +++ b/src/Lucene.Net.Analysis.Common/Analysis/Util/CharacterUtils.cs @@ -33,7 +33,7 @@ namespace Lucene.Net.Analysis.Util /// provides a unified interface to Character-related /// operations to implement backwards compatible character operations based on a /// instance. - /// + /// /// @lucene.internal /// public abstract class CharacterUtils @@ -57,8 +57,8 @@ public abstract class CharacterUtils public static CharacterUtils GetInstance(LuceneVersion matchVersion) { #pragma warning disable 612, 618 - return matchVersion.OnOrAfter(LuceneVersion.LUCENE_31) - ? JAVA_5 + return matchVersion.OnOrAfter(LuceneVersion.LUCENE_31) + ? JAVA_5 : JAVA_4_BW_COMPAT; #pragma warning restore 612, 618 } @@ -125,7 +125,7 @@ public static CharacterUtils GetJava4Instance(LuceneVersion matchVersion) // LUC /// /// the offset to the char values in the chars array to be converted /// the index afer the last element that should be used to calculate - /// codepoint. + /// codepoint. /// /// the Unicode code point at the given index /// @@ -170,7 +170,7 @@ public static CharacterBuffer NewCharacterBuffer(int bufferSize) /// - /// Converts each unicode codepoint to lowerCase via in the invariant culture starting + /// Converts each unicode codepoint to lowerCase via in the invariant culture starting /// at the given offset. /// the char buffer to lowercase /// the offset to start at @@ -195,7 +195,7 @@ public static CharacterBuffer NewCharacterBuffer(int bufferSize) // .ToLower(new string(buffer, offset, length)) // .CopyTo(0, buffer, offset, length); - //// Optimization provided by Vincent Van Den Berghe: + //// Optimization provided by Vincent Van Den Berghe: //// http://search-lucene.com/m/Lucene.Net/j1zMf1uckOzOYqsi?subj=Proposal+to+speed+up+implementation+of+LowercaseFilter+charUtils+ToLower //new string(buffer, offset, length) // .ToLowerInvariant() @@ -212,7 +212,7 @@ public static CharacterBuffer NewCharacterBuffer(int bufferSize) } /// - /// Converts each unicode codepoint to UpperCase via in the invariant culture starting + /// Converts each unicode codepoint to UpperCase via in the invariant culture starting /// at the given offset. /// the char buffer to UPPERCASE /// the offset to start at @@ -237,7 +237,7 @@ public static CharacterBuffer NewCharacterBuffer(int bufferSize) // .ToUpper(new string(buffer, offset, length)) // .CopyTo(0, buffer, offset, length); - //// Optimization provided by Vincent Van Den Berghe: + //// Optimization provided by Vincent Van Den Berghe: //// http://search-lucene.com/m/Lucene.Net/j1zMf1uckOzOYqsi?subj=Proposal+to+speed+up+implementation+of+LowercaseFilter+charUtils+ToLower //new string(buffer, offset, length) // .ToUpperInvariant() @@ -338,7 +338,7 @@ public virtual bool Fill(CharacterBuffer buffer, TextReader reader) /// /// Return the index within buf[start:start+count] which is by - /// code points from . + /// code points from . /// public abstract int OffsetByCodePoints(char[] buf, int start, int count, int index, int offset); @@ -548,7 +548,8 @@ public override int OffsetByCodePoints(char[] buf, int start, int count, int ind uint result = (uint)index + (uint)offset; if (result < 0 || result > count) { - throw new ArgumentOutOfRangeException(nameof(index) + " + " + nameof(offset), "index + offset must be >= 0 and <= count"); + // LUCENENET note - it is common in .NET to use the "index" (or "start") parameter for the paramName, even though offset might be what is invalid + throw new ArgumentOutOfRangeException(nameof(index), "index + offset must be >= 0 and <= count"); } return (int)result; } @@ -652,4 +653,4 @@ public void Reset() } } } -} \ No newline at end of file +} diff --git a/src/Lucene.Net.Analysis.Kuromoji/JapaneseKatakanaStemFilterFactory.cs b/src/Lucene.Net.Analysis.Kuromoji/JapaneseKatakanaStemFilterFactory.cs index 0b3b66a158..0f5511ed51 100644 --- a/src/Lucene.Net.Analysis.Kuromoji/JapaneseKatakanaStemFilterFactory.cs +++ b/src/Lucene.Net.Analysis.Kuromoji/JapaneseKatakanaStemFilterFactory.cs @@ -38,18 +38,21 @@ public class JapaneseKatakanaStemFilterFactory : TokenFilterFactory private const string MINIMUM_LENGTH_PARAM = "minimumLength"; private readonly int minimumLength; - /// Creates a new + /// + /// Creates a new + /// + /// Thrown if any of the key/value pairs are invalid. public JapaneseKatakanaStemFilterFactory(IDictionary args) : base(args) { minimumLength = GetInt32(args, MINIMUM_LENGTH_PARAM, JapaneseKatakanaStemFilter.DEFAULT_MINIMUM_LENGTH); if (minimumLength < 2) { - throw new ArgumentOutOfRangeException(nameof(minimumLength), "Illegal " + MINIMUM_LENGTH_PARAM + " " + minimumLength + " (must be 2 or greater)"); // LUCENENET specific - changed from IllegalArgumentException to ArgumentOutOfRangeException (.NET convention) + throw new ArgumentException("Illegal " + MINIMUM_LENGTH_PARAM + " " + minimumLength + " (must be 2 or greater)", nameof(args)); } if (args.Count > 0) { - throw new ArgumentException(string.Format(J2N.Text.StringFormatter.CurrentCulture, "Unknown parameters: {0}", args)); + throw new ArgumentException(string.Format(J2N.Text.StringFormatter.CurrentCulture, "Unknown parameters: {0}", args), nameof(args)); } } diff --git a/src/Lucene.Net.Analysis.Phonetic/Language/Bm/PhoneticEngine.cs b/src/Lucene.Net.Analysis.Phonetic/Language/Bm/PhoneticEngine.cs index 3950e7559f..79a40c1b74 100644 --- a/src/Lucene.Net.Analysis.Phonetic/Language/Bm/PhoneticEngine.cs +++ b/src/Lucene.Net.Analysis.Phonetic/Language/Bm/PhoneticEngine.cs @@ -357,7 +357,7 @@ private PhonemeBuilder ApplyFinalRules(PhonemeBuilder phonemeBuilder, { if (finalRules is null) { - throw new ArgumentNullException("finalRules can not be null");// LUCENENET specific - changed from IllegalArgumentException to ArgumentNullException (.NET convention) + throw new ArgumentNullException(nameof(finalRules), "finalRules can not be null"); // LUCENENET specific - changed from IllegalArgumentException to ArgumentNullException (.NET convention) } if (finalRules.Count == 0) { diff --git a/src/Lucene.Net.Benchmark/ByTask/Benchmark.cs b/src/Lucene.Net.Benchmark/ByTask/Benchmark.cs index 8f04e2fda1..46223a2ade 100644 --- a/src/Lucene.Net.Benchmark/ByTask/Benchmark.cs +++ b/src/Lucene.Net.Benchmark/ByTask/Benchmark.cs @@ -123,7 +123,7 @@ public static void Exec(string[] args) if (args.Length < 1) { // LUCENENET specific - usage info printed by our wrapper console - throw new ArgumentException(); + throw new ArgumentException("Benchmark requires 1 argument", nameof(args)); //Console.WriteLine("Usage: java Benchmark "); //Environment.Exit(1); } diff --git a/src/Lucene.Net.Benchmark/Quality/Trec/QueryDriver.cs b/src/Lucene.Net.Benchmark/Quality/Trec/QueryDriver.cs index 482c6130a2..b75eb55b50 100644 --- a/src/Lucene.Net.Benchmark/Quality/Trec/QueryDriver.cs +++ b/src/Lucene.Net.Benchmark/Quality/Trec/QueryDriver.cs @@ -58,7 +58,7 @@ public static void Main(string[] args) if (args.Length < 4 || args.Length > 5) { // LUCENENET specific - our wrapper console shows correct usage - throw new ArgumentException(); + throw new ArgumentException("QueryDriver requires 4 or 5 arguments", nameof(args)); //Console.Error.WriteLine("Usage: QueryDriver [querySpec]"); //Console.Error.WriteLine("topicsFile: input file containing queries"); //Console.Error.WriteLine("qrelsFile: input file containing relevance judgements"); diff --git a/src/Lucene.Net.Benchmark/Quality/Utils/QualityQueriesFinder.cs b/src/Lucene.Net.Benchmark/Quality/Utils/QualityQueriesFinder.cs index 62f23ec399..01ebc8a5d3 100644 --- a/src/Lucene.Net.Benchmark/Quality/Utils/QualityQueriesFinder.cs +++ b/src/Lucene.Net.Benchmark/Quality/Utils/QualityQueriesFinder.cs @@ -66,7 +66,7 @@ public static void Main(string[] args) if (args.Length < 1) { // LUCENENET specific - our wrapper console shows correct usage - throw new ArgumentException(); + throw new ArgumentException("QualityQueriesFinder requires 1 argument", nameof(args)); //Console.Error.WriteLine("Usage: java QualityQueriesFinder "); //Environment.Exit(1); } diff --git a/src/Lucene.Net.Benchmark/Utils/ExtractWikipedia.cs b/src/Lucene.Net.Benchmark/Utils/ExtractWikipedia.cs index d25bceb2f4..5504248a3d 100644 --- a/src/Lucene.Net.Benchmark/Utils/ExtractWikipedia.cs +++ b/src/Lucene.Net.Benchmark/Utils/ExtractWikipedia.cs @@ -177,7 +177,7 @@ public static void Main(string[] args) else { // LUCENENET specific - our wrapper console shows correct usage - throw new ArgumentException(); + throw new ArgumentException("Wikipedia file not found", nameof(args)); //PrintUsage(); } } diff --git a/src/Lucene.Net.Facet/Taxonomy/PrintTaxonomyStats.cs b/src/Lucene.Net.Facet/Taxonomy/PrintTaxonomyStats.cs index 1b60962a7d..7ed3d10d4d 100644 --- a/src/Lucene.Net.Facet/Taxonomy/PrintTaxonomyStats.cs +++ b/src/Lucene.Net.Facet/Taxonomy/PrintTaxonomyStats.cs @@ -27,7 +27,7 @@ namespace Lucene.Net.Facet.Taxonomy using FSDirectory = Lucene.Net.Store.FSDirectory; /// - /// Prints how many ords are under each dimension. + /// Prints how many ords are under each dimension. /// // java -cp ../build/core/classes/java:../build/facet/classes/java org.apache.lucene.facet.util.PrintTaxonomyStats -printTree /s2/scratch/indices/wikibig.trunk.noparents.facets.Lucene41.nd1M/facets @@ -54,14 +54,14 @@ public static int Main(string[] args) if (args.Length != (printTree ? 2 : 1)) { // LUCENENET specific - our wrapper console shows the correct usage - throw new ArgumentException(); + throw new ArgumentException("PrintTaxonomyStats requires 1 or 2 arguments", nameof(args)); //Console.WriteLine("\nUsage: java -classpath ... org.apache.lucene.facet.util.PrintTaxonomyStats [-printTree] /path/to/taxononmy/index\n"); //return 1; } using Store.Directory dir = FSDirectory.Open(new DirectoryInfo(path)); using var r = new DirectoryTaxonomyReader(dir); PrintStats(r, System.Console.Out, printTree); - + return 0; } @@ -114,4 +114,4 @@ private static void PrintAllChildren(TextWriter @out, TaxonomyReader r, int ord, } } } -} \ No newline at end of file +} diff --git a/src/Lucene.Net.Highlighter/VectorHighlight/BaseFragListBuilder.cs b/src/Lucene.Net.Highlighter/VectorHighlight/BaseFragListBuilder.cs index aae799a508..b9651c3d3d 100644 --- a/src/Lucene.Net.Highlighter/VectorHighlight/BaseFragListBuilder.cs +++ b/src/Lucene.Net.Highlighter/VectorHighlight/BaseFragListBuilder.cs @@ -137,7 +137,7 @@ protected virtual bool AcceptPhrase(WeightedPhraseInfo info, int matchLength, in { // LUCENENET specific - added guard clause to check for null if (info is null) - throw new ArgumentNullException(nameof(WeightedPhraseInfo)); + throw new ArgumentNullException(nameof(info)); return info.TermsOffsets.Count <= 1 || matchLength <= fragCharSize; } diff --git a/src/Lucene.Net.Misc/Index/IndexSplitter.cs b/src/Lucene.Net.Misc/Index/IndexSplitter.cs index 1d6e409ece..26f51ed5f4 100644 --- a/src/Lucene.Net.Misc/Index/IndexSplitter.cs +++ b/src/Lucene.Net.Misc/Index/IndexSplitter.cs @@ -74,7 +74,7 @@ public static void Main(string[] args) if (args.Length < 2) { // LUCENENET specific - our wrapper console shows the correct usage - throw new ArgumentException(); + throw new ArgumentException("IndexSplitter requires at least 2 arguments", nameof(args)); //Console.Error.WriteLine("Usage: IndexSplitter -l (list the segments and their sizes)"); //Console.Error.WriteLine("IndexSplitter +"); //Console.Error.WriteLine("IndexSplitter -d (delete the following segments)"); diff --git a/src/Lucene.Net.Misc/Index/MultiPassIndexSplitter.cs b/src/Lucene.Net.Misc/Index/MultiPassIndexSplitter.cs index 98c2900866..99910ec5ac 100644 --- a/src/Lucene.Net.Misc/Index/MultiPassIndexSplitter.cs +++ b/src/Lucene.Net.Misc/Index/MultiPassIndexSplitter.cs @@ -63,11 +63,11 @@ public virtual void Split(LuceneVersion version, IndexReader @in, Store.Director { if (outputs is null || outputs.Length < 2) { - throw new IOException("Invalid number of outputs."); + throw new ArgumentException("Invalid number of outputs.", nameof(outputs)); } if (@in is null || @in.NumDocs < 2) { - throw new IOException("Not enough documents for splitting"); + throw new ArgumentException("Not enough documents for splitting", nameof(@in)); } int numParts = outputs.Length; // wrap a potentially read-only input @@ -136,7 +136,7 @@ public static void Main(string[] args) if (args.Length < 5) { // LUCENENET specific - our wrapper console shows the correct usage - throw new ArgumentException(); + throw new ArgumentException("MultiPassIndexSplitter requires at least 5 arguments", nameof(args)); //Console.Error.WriteLine("Usage: MultiPassIndexSplitter -out -num [-seq] [ 4) { // LUCENENET specific - our wrapper console shows the correct usage - throw new ArgumentException(); + throw new ArgumentException("HighFreqTerms requires 1 to 4 arguments", nameof(args)); //Usage(); //Environment.Exit(1); } diff --git a/src/Lucene.Net.Misc/Misc/IndexMergeTool.cs b/src/Lucene.Net.Misc/Misc/IndexMergeTool.cs index 2208599e62..21496dfda3 100644 --- a/src/Lucene.Net.Misc/Misc/IndexMergeTool.cs +++ b/src/Lucene.Net.Misc/Misc/IndexMergeTool.cs @@ -54,7 +54,7 @@ public static void Main(string[] args) if (args.Length < 3) { // LUCENENET specific - our wrapper console shows the correct usage - throw new ArgumentException(); + throw new ArgumentException("IndexMergeTool requires at least 3 arguments", nameof(args)); //Console.Error.WriteLine("Usage: IndexMergeTool [index3] ..."); //Environment.Exit(1); } diff --git a/src/Lucene.Net.Queries/TermFilter.cs b/src/Lucene.Net.Queries/TermFilter.cs index 61a9967c97..41fc8fe28e 100644 --- a/src/Lucene.Net.Queries/TermFilter.cs +++ b/src/Lucene.Net.Queries/TermFilter.cs @@ -22,7 +22,7 @@ namespace Lucene.Net.Queries * See the License for the specific language governing permissions and * limitations under the License. */ - + /// /// A filter that includes documents that match with a specific term. /// @@ -39,7 +39,7 @@ public TermFilter(Term term) } else if (term.Field is null) { - throw new ArgumentNullException(nameof(term.Field), "term.Field must not be null"); // LUCENENET specific - changed from IllegalArgumentException to ArgumentNullException (.NET convention) + throw new ArgumentException("term.Field must not be null", nameof(term)); } this.term = term; } @@ -111,4 +111,4 @@ public override string ToString() return term.Field + ":" + term.Text; } } -} \ No newline at end of file +} diff --git a/src/Lucene.Net.Suggest/Suggest/Fst/FSTCompletionBuilder.cs b/src/Lucene.Net.Suggest/Suggest/Fst/FSTCompletionBuilder.cs index 7e1adac673..ff4c828d60 100644 --- a/src/Lucene.Net.Suggest/Suggest/Fst/FSTCompletionBuilder.cs +++ b/src/Lucene.Net.Suggest/Suggest/Fst/FSTCompletionBuilder.cs @@ -209,7 +209,7 @@ public virtual void Add(BytesRef utf8, int bucket) if (bucket < 0 || bucket >= buckets) { - throw new ArgumentOutOfRangeException(nameof(buckets), "Bucket outside of the allowed range [0, " + buckets + "): " + bucket); // LUCENENET specific - changed from IllegalArgumentException to ArgumentOutOfRangeException (.NET convention) + throw new ArgumentOutOfRangeException(nameof(bucket), "Bucket outside of the allowed range [0, " + buckets + "): " + bucket); // LUCENENET specific - changed from IllegalArgumentException to ArgumentOutOfRangeException (.NET convention) } if (scratch.Bytes.Length < utf8.Length + 1) diff --git a/src/Lucene.Net/Index/IndexUpgrader.cs b/src/Lucene.Net/Index/IndexUpgrader.cs index 731369ed72..883ce7ff38 100644 --- a/src/Lucene.Net/Index/IndexUpgrader.cs +++ b/src/Lucene.Net/Index/IndexUpgrader.cs @@ -68,7 +68,7 @@ public sealed class IndexUpgrader private static void PrintUsage() { // LUCENENET specific - our wrapper console shows the correct usage - throw new ArgumentException(); + throw new ArgumentException("One or more arguments was invalid"); //Console.Error.WriteLine("Upgrades an index so all segments created with a previous Lucene version are rewritten."); //Console.Error.WriteLine("Usage:"); //Console.Error.WriteLine(" java " + typeof(IndexUpgrader).Name + " [-delete-prior-commits] [-verbose] [-dir-impl X] indexDir"); diff --git a/src/Lucene.Net/Index/IndexWriterConfig.cs b/src/Lucene.Net/Index/IndexWriterConfig.cs index 8686363fae..7985d47327 100644 --- a/src/Lucene.Net/Index/IndexWriterConfig.cs +++ b/src/Lucene.Net/Index/IndexWriterConfig.cs @@ -47,19 +47,19 @@ namespace Lucene.Net.Index /// OpenMode = OpenMode.CREATE /// }; /// - /// - /// However, if you prefer to match the syntax of Lucene using chained setter methods, + /// + /// However, if you prefer to match the syntax of Lucene using chained setter methods, /// there are extension methods in the Lucene.Net.Index.Extensions namespace. Example usage: /// /// using Lucene.Net.Index.Extensions; - /// + /// /// .. - /// + /// /// IndexWriterConfig conf = new IndexWriterConfig(analyzer) /// .SetCodec(new Lucene46Codec()) /// .SetOpenMode(OpenMode.CREATE); /// - /// + /// /// @since 3.1 /// /// @@ -156,8 +156,8 @@ internal IndexWriterConfig SetIndexWriter(IndexWriter writer) /// /// Creates a new config that with defaults that match the specified - /// as well as the default - /// . If is >= + /// as well as the default + /// . If is >= /// , is used /// for merging; else . /// Note that is free to select @@ -199,7 +199,7 @@ public object Clone() /// /// Only takes effect when is first created. /// - // LUCENENET NOTE: We cannot override a getter and add a setter, + // LUCENENET NOTE: We cannot override a getter and add a setter, // so must declare it new. See: http://stackoverflow.com/q/82437 new public OpenMode OpenMode { @@ -229,7 +229,7 @@ public object Clone() /// /// Only takes effect when IndexWriter is first created. /// - // LUCENENET NOTE: We cannot override a getter and add a setter, + // LUCENENET NOTE: We cannot override a getter and add a setter, // so must declare it new. See: http://stackoverflow.com/q/82437 new public IndexDeletionPolicy IndexDeletionPolicy { @@ -243,7 +243,7 @@ public object Clone() /// /// Only takes effect when is first created. /// - // LUCENENET NOTE: We cannot override a getter and add a setter, + // LUCENENET NOTE: We cannot override a getter and add a setter, // so must declare it new. See: http://stackoverflow.com/q/82437 new public IndexCommit IndexCommit { @@ -258,7 +258,7 @@ public object Clone() /// /// Only takes effect when is first created. /// - // LUCENENET NOTE: We cannot override a getter and add a setter, + // LUCENENET NOTE: We cannot override a getter and add a setter, // so must declare it new. See: http://stackoverflow.com/q/82437 new public Similarity Similarity { @@ -274,7 +274,7 @@ public object Clone() /// /// Only takes effect when is first created. /// - // LUCENENET NOTE: We cannot override a getter and add a setter, + // LUCENENET NOTE: We cannot override a getter and add a setter, // so must declare it new. See: http://stackoverflow.com/q/82437 new public IMergeScheduler MergeScheduler { @@ -289,7 +289,7 @@ public object Clone() /// /// Only takes effect when is first created. /// - // LUCENENET NOTE: We cannot override a getter and add a setter, + // LUCENENET NOTE: We cannot override a getter and add a setter, // so must declare it new. See: http://stackoverflow.com/q/82437 new public long WriteLockTimeout { @@ -302,7 +302,7 @@ public object Clone() /// /// Only takes effect when is first created. /// - // LUCENENET NOTE: We cannot override a getter and add a setter, + // LUCENENET NOTE: We cannot override a getter and add a setter, // so must declare it new. See: http://stackoverflow.com/q/82437 new public Codec Codec { @@ -318,7 +318,7 @@ public object Clone() /// /// Only takes effect when is first created. /// - // LUCENENET NOTE: We cannot override a getter and add a setter, + // LUCENENET NOTE: We cannot override a getter and add a setter, // so must declare it new. See: http://stackoverflow.com/q/82437 new public MergePolicy MergePolicy { @@ -341,7 +341,7 @@ public object Clone() /// /// NOTE: this only takes effect when is first created. /// - // LUCENENET NOTE: We cannot override a getter and add a setter, + // LUCENENET NOTE: We cannot override a getter and add a setter, // so must declare it new. See: http://stackoverflow.com/q/82437 new internal DocumentsWriterPerThreadPool IndexerThreadPool { @@ -357,7 +357,7 @@ public object Clone() /// /// Only takes effect when is first created. /// - // LUCENENET NOTE: We cannot override a getter and add a setter, + // LUCENENET NOTE: We cannot override a getter and add a setter, // so must declare it new. See: http://stackoverflow.com/q/82437 new public int MaxThreadStates { @@ -378,9 +378,9 @@ public object Clone() /// /// Only takes effect when is first created. /// - // LUCENENET NOTE: We cannot override a getter and add a setter, + // LUCENENET NOTE: We cannot override a getter and add a setter, // so must declare it new. See: http://stackoverflow.com/q/82437 - new public bool UseReaderPooling + new public bool UseReaderPooling { get => readerPooling; set => this.readerPooling = value; @@ -391,7 +391,7 @@ public object Clone() /// /// Only takes effect when is first created. /// - // LUCENENET NOTE: We cannot override a getter and add a setter, + // LUCENENET NOTE: We cannot override a getter and add a setter, // so must declare it new. See: http://stackoverflow.com/q/82437 new internal IndexingChain IndexingChain { @@ -409,7 +409,7 @@ public object Clone() /// The given value must be less that 2GB (2048MB). /// /// - // LUCENENET NOTE: We cannot override a getter and add a setter, + // LUCENENET NOTE: We cannot override a getter and add a setter, // so must declare it new. See: http://stackoverflow.com/q/82437 new public int RAMPerThreadHardLimitMB { @@ -432,7 +432,7 @@ public object Clone() /// /// /// - // LUCENENET NOTE: We cannot override a getter and add a setter, + // LUCENENET NOTE: We cannot override a getter and add a setter, // so must declare it new. See: http://stackoverflow.com/q/82437 new internal FlushPolicy FlushPolicy { @@ -515,20 +515,20 @@ public object Clone() public IndexWriterConfig SetInfoStream(InfoStream infoStream) { this.infoStream = infoStream ?? throw new ArgumentNullException(nameof(infoStream), - "Cannot set InfoStream implementation to null. " + + "Cannot set InfoStream implementation to null. " + "To disable logging use InfoStream.NO_OUTPUT"); return this; } /// - /// Convenience method that uses to write to the passed in . + /// Convenience method that uses to write to the passed in . /// Must not be null. /// public IndexWriterConfig SetInfoStream(TextWriter printStream) { if (printStream is null) { - throw new ArgumentNullException("printStream must not be null"); // LUCENENET specific - changed from IllegalArgumentException to ArgumentNullException (.NET convention) + throw new ArgumentNullException(nameof(printStream), "printStream must not be null"); // LUCENENET specific - changed from IllegalArgumentException to ArgumentNullException (.NET convention) } return SetInfoStream(new TextWriterInfoStream(printStream)); } @@ -605,4 +605,4 @@ public enum OpenMode // LUCENENET specific: De-nested from IndexWriterConfig to /// CREATE_OR_APPEND } -} \ No newline at end of file +} diff --git a/src/Lucene.Net/Search/MinShouldMatchSumScorer.cs b/src/Lucene.Net/Search/MinShouldMatchSumScorer.cs index 7e187f2f0c..cc8bfc9c35 100644 --- a/src/Lucene.Net/Search/MinShouldMatchSumScorer.cs +++ b/src/Lucene.Net/Search/MinShouldMatchSumScorer.cs @@ -95,7 +95,7 @@ public MinShouldMatchSumScorer(Weight weight, IList subScorers, int mini } if (numScorers <= 1) { - throw new ArgumentOutOfRangeException(nameof(numScorers), "There must be at least 2 subScorers"); // LUCENENET specific - changed from IllegalArgumentException to ArgumentOutOfRangeException (.NET convention) + throw new ArgumentOutOfRangeException(nameof(subScorers), "There must be at least 2 subScorers"); // LUCENENET specific - changed from IllegalArgumentException to ArgumentOutOfRangeException (.NET convention) } this.mm = minimumNrMatchers; @@ -477,4 +477,4 @@ private bool MinheapCheck(int root) return MinheapCheck(lchild) && MinheapCheck(rchild); } } -} \ No newline at end of file +} diff --git a/src/Lucene.Net/Store/CompoundFileWriter.cs b/src/Lucene.Net/Store/CompoundFileWriter.cs index 61b229533f..bd9326b132 100644 --- a/src/Lucene.Net/Store/CompoundFileWriter.cs +++ b/src/Lucene.Net/Store/CompoundFileWriter.cs @@ -94,7 +94,7 @@ internal CompoundFileWriter(Directory dir, string name) { // LUCENENET specific - changed order to take advantage of throw expression and // changed from IllegalArgumentException to ArgumentNullException (.NET convention) - directory = dir ?? throw new ArgumentNullException(nameof(directory), $"{nameof(directory)} cannot be null"); + directory = dir ?? throw new ArgumentNullException(nameof(dir), $"{nameof(directory)} cannot be null"); dataFileName = name ?? throw new ArgumentNullException(nameof(name), $"{nameof(name)} cannot be null"); entryTableName = IndexFileNames.SegmentFileName(IndexFileNames.StripExtension(name), "", IndexFileNames.COMPOUND_FILE_ENTRIES_EXTENSION); } @@ -431,4 +431,4 @@ public override void WriteBytes(byte[] b, int offset, int length) public override long Checksum => @delegate.Checksum; } } -} \ No newline at end of file +} diff --git a/src/Lucene.Net/Store/LockStressTest.cs b/src/Lucene.Net/Store/LockStressTest.cs index d18ee0f411..2614a64340 100644 --- a/src/Lucene.Net/Store/LockStressTest.cs +++ b/src/Lucene.Net/Store/LockStressTest.cs @@ -62,7 +62,7 @@ public static void Main(string[] args) if (args.Length != 7) { // LUCENENET specific - our lucene-cli wrapper console shows the correct usage - throw new ArgumentException(); + throw new ArgumentException("LockStressTest requires 7 arguments", nameof(args)); //Console.WriteLine("Usage: java Lucene.Net.Store.LockStressTest myID verifierHost verifierPort lockFactoryClassName lockDirName sleepTimeMS count\n" + // "\n" + // " myID = int from 0 .. 255 (should be unique for test process)\n" + diff --git a/src/Lucene.Net/Store/LockVerifyServer.cs b/src/Lucene.Net/Store/LockVerifyServer.cs index 5da9d58bb8..cf087d7543 100644 --- a/src/Lucene.Net/Store/LockVerifyServer.cs +++ b/src/Lucene.Net/Store/LockVerifyServer.cs @@ -65,7 +65,7 @@ public static void Main(string[] args) if (args.Length != 2) { // LUCENENET specific - our wrapper console shows the correct usage - throw new ArgumentException(); + throw new ArgumentException("LockVerifyServer requires 2 arguments", nameof(args)); //Console.WriteLine("Usage: java Lucene.Net.Store.LockVerifyServer bindToIp clients\n"); //Environment.FailFast("1"); } diff --git a/src/Lucene.Net/Util/Automaton/BasicAutomata.cs b/src/Lucene.Net/Util/Automaton/BasicAutomata.cs index f55dee0912..d28b4a18ae 100644 --- a/src/Lucene.Net/Util/Automaton/BasicAutomata.cs +++ b/src/Lucene.Net/Util/Automaton/BasicAutomata.cs @@ -248,7 +248,7 @@ private static State Between(string x, string y, int n, ICollection initi /// If > 0, use fixed number of digits (strings must be prefixed /// by 0's to obtain the right length) - otherwise, the number of /// digits is not fixed. - /// If min > max or if numbers in the + /// If min > max or if numbers in the /// interval cannot be expressed with the given fixed number of /// digits. public static Automaton MakeInterval(int min, int max, int digits) @@ -256,10 +256,19 @@ public static Automaton MakeInterval(int min, int max, int digits) Automaton a = new Automaton(); string x = Convert.ToString(min, CultureInfo.InvariantCulture); string y = Convert.ToString(max, CultureInfo.InvariantCulture); - if (min > max || (digits > 0 && y.Length > digits)) + + // LUCENENET specific - split into two `if` checks for different ArgumentOutOfRangeExceptions. + // Also changed to use ArgumentOutOfRangeException instead of IllegalArgumentException (.NET Convention). + if (min > max) { - throw new ArgumentException(); + throw new ArgumentOutOfRangeException(nameof(min), "min must be less than or equal to max"); } + + if (digits > 0 && y.Length > digits) + { + throw new ArgumentOutOfRangeException(nameof(digits), $"Cannot represent numbers between {min} and {max} with {digits} digits"); + } + int d; if (digits > 0) { @@ -364,4 +373,4 @@ public static Automaton MakeStringUnion(ICollection utf8Strings) } } } -} \ No newline at end of file +} diff --git a/src/Lucene.Net/Util/UnicodeUtil.cs b/src/Lucene.Net/Util/UnicodeUtil.cs index f29ce7cbbe..5974af1a16 100644 --- a/src/Lucene.Net/Util/UnicodeUtil.cs +++ b/src/Lucene.Net/Util/UnicodeUtil.cs @@ -727,15 +727,15 @@ public static int CodePointCount(BytesRef utf8) if (v < /* 111x xxxx */ 0xe0) { pos += 2; continue; } if (v < /* 1111 xxxx */ 0xf0) { pos += 3; continue; } if (v < /* 1111 1xxx */ 0xf8) { pos += 4; continue; } - // fallthrough, consider 5 and 6 byte sequences invalid. + // fallthrough, consider 5 and 6 byte sequences invalid. } // Anything not covered above is invalid UTF8. - throw new ArgumentException("Invalid UTF-8"); + throw new ArgumentException("Invalid UTF-8", nameof(utf8)); } // Check if we didn't go over the limit on the last character. - if (pos > limit) throw new ArgumentException(); + if (pos > limit) throw new ArgumentException("Invalid UTF-8", nameof(utf8)); return codePointCount; } @@ -787,7 +787,7 @@ public static void UTF8toUTF32(BytesRef utf8, Int32sRef utf32) break; default: - throw new ArgumentException("invalid utf8"); + throw new ArgumentException("invalid utf8", nameof(utf8)); } // TODO: this may read past utf8's limit. @@ -862,11 +862,11 @@ public static char[] ToCharArray(int[] codePoints, int offset, int count) { throw new ArgumentOutOfRangeException(nameof(count), "count must be >= 0"); // LUCENENET specific - changed from IllegalArgumentException to ArgumentOutOfRangeException (.NET convention) } - int countThreashold = 1024; // If the number of chars exceeds this, we count them instead of allocating count * 2 - // LUCENENET: as a first approximation, assume each codepoint + const int countThreashold = 1024; // If the number of chars exceeds this, we count them instead of allocating count * 2 + // LUCENENET: as a first approximation, assume each codepoint // is 2 characters (since it cannot be longer than this) int arrayLength = count * 2; - // LUCENENET: if we go over the threashold, count the number of + // LUCENENET: if we go over the threashold, count the number of // chars we will need so we can allocate the precise amount of memory if (count > countThreashold) { @@ -889,7 +889,7 @@ public static char[] ToCharArray(int[] codePoints, int offset, int count) int cp = codePoints[r]; if (cp < 0 || cp > 0x10ffff) { - throw new ArgumentException(); + throw new ArgumentException($"Invalid code point: {cp}", nameof(codePoints)); } if (cp < 0x010000) { @@ -1010,4 +1010,4 @@ public static void UTF8toUTF16(BytesRef bytesRef, CharsRef chars) UTF8toUTF16(bytesRef.Bytes, bytesRef.Offset, bytesRef.Length, chars); } } -} \ No newline at end of file +}