Skip to content

Commit

Permalink
Fix incorrect usage of argument exceptions for Sonar csharpsquid:S3928,
Browse files Browse the repository at this point in the history
  • Loading branch information
paulirwin committed Nov 19, 2024
1 parent 3c14b14 commit e95e250
Show file tree
Hide file tree
Showing 27 changed files with 112 additions and 97 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,31 +43,33 @@ public class ShingleFilterFactory : TokenFilterFactory
private readonly string fillerToken;

/// <summary>
/// Creates a new <see cref="ShingleFilterFactory"/> </summary>
public ShingleFilterFactory(IDictionary<string, string> args)
/// Creates a new <see cref="ShingleFilterFactory"/>
/// </summary>
/// <exception cref="ArgumentException">Thrown if any of the <paramref name="args"/> key/value pairs are invalid.</exception>
public ShingleFilterFactory(IDictionary<string, string> 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);
tokenSeparator = Get(args, "tokenSeparator", ShingleFilter.DEFAULT_TOKEN_SEPARATOR);
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));
}
}

Expand All @@ -81,4 +83,4 @@ public override TokenStream Create(TokenStream input)
return r;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
16 changes: 8 additions & 8 deletions src/Lucene.Net.Analysis.Common/Analysis/Synonym/SynonymMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public class Builder

/// <summary>
/// If dedup is true then identical rules (same input,
/// same output) will be added only once.
/// same output) will be added only once.
/// </summary>
public Builder(bool dedup)
{
Expand All @@ -95,9 +95,9 @@ internal class MapEntry
}

/// <summary>
/// Sugar: just joins the provided terms with
/// Sugar: just joins the provided terms with
/// <see cref="SynonymMap.WORD_SEPARATOR"/>. reuse and its chars
/// must not be null.
/// must not be null.
/// </summary>
public static CharsRef Join(string[] words, CharsRef reuse)
{
Expand Down Expand Up @@ -163,15 +163,15 @@ 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)
{
throw new ArgumentOutOfRangeException(nameof(numOutputWords), "numOutputWords must be > 0 (got " + numOutputWords + ")"); // LUCENENET specific - changed from IllegalArgumentException to ArgumentOutOfRangeException (.NET convention)
}
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)
Expand Down Expand Up @@ -332,7 +332,7 @@ public virtual SynonymMap Build()

/// <summary>
/// Abstraction for parsing synonym files.
///
///
/// @lucene.experimental
/// </summary>
public abstract class Parser : Builder
Expand All @@ -353,7 +353,7 @@ protected Parser(bool dedup, Analyzer analyzer) // LUCENENET: CA1012: Abstract t
/// <summary>
/// Sugar: analyzes the text with the analyzer and
/// separates by <see cref="SynonymMap.WORD_SEPARATOR"/>.
/// reuse and its chars must not be null.
/// reuse and its chars must not be null.
/// </summary>
public virtual CharsRef Analyze(string text, CharsRef reuse)
{
Expand Down Expand Up @@ -394,4 +394,4 @@ public virtual CharsRef Analyze(string text, CharsRef reuse)
}
}
}
}
}
23 changes: 12 additions & 11 deletions src/Lucene.Net.Analysis.Common/Analysis/Util/CharacterUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ namespace Lucene.Net.Analysis.Util
/// <see cref="CharacterUtils"/> provides a unified interface to Character-related
/// operations to implement backwards compatible character operations based on a
/// <see cref="LuceneVersion"/> instance.
///
///
/// @lucene.internal
/// </summary>
public abstract class CharacterUtils
Expand All @@ -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
}
Expand Down Expand Up @@ -125,7 +125,7 @@ public static CharacterUtils GetJava4Instance(LuceneVersion matchVersion) // LUC
/// <param name="offset">
/// the offset to the char values in the chars array to be converted </param>
/// <param name="limit"> the index afer the last element that should be used to calculate
/// codepoint.
/// codepoint.
/// </param>
/// <returns> the Unicode code point at the given index </returns>
/// <exception cref="ArgumentNullException">
Expand Down Expand Up @@ -170,7 +170,7 @@ public static CharacterBuffer NewCharacterBuffer(int bufferSize)


/// <summary>
/// Converts each unicode codepoint to lowerCase via <see cref="TextInfo.ToLower(string)"/> in the invariant culture starting
/// Converts each unicode codepoint to lowerCase via <see cref="TextInfo.ToLower(string)"/> in the invariant culture starting
/// at the given offset. </summary>
/// <param name="buffer"> the char buffer to lowercase </param>
/// <param name="offset"> the offset to start at </param>
Expand All @@ -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()
Expand All @@ -212,7 +212,7 @@ public static CharacterBuffer NewCharacterBuffer(int bufferSize)
}

/// <summary>
/// Converts each unicode codepoint to UpperCase via <see cref="TextInfo.ToUpper(string)"/> in the invariant culture starting
/// Converts each unicode codepoint to UpperCase via <see cref="TextInfo.ToUpper(string)"/> in the invariant culture starting
/// at the given offset. </summary>
/// <param name="buffer"> the char buffer to UPPERCASE </param>
/// <param name="offset"> the offset to start at </param>
Expand All @@ -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()
Expand Down Expand Up @@ -338,7 +338,7 @@ public virtual bool Fill(CharacterBuffer buffer, TextReader reader)

/// <summary>
/// Return the index within <c>buf[start:start+count]</c> which is by <paramref name="offset"/>
/// code points from <paramref name="index"/>.
/// code points from <paramref name="index"/>.
/// </summary>
public abstract int OffsetByCodePoints(char[] buf, int start, int count, int index, int offset);

Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -652,4 +653,4 @@ public void Reset()
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,21 @@ public class JapaneseKatakanaStemFilterFactory : TokenFilterFactory
private const string MINIMUM_LENGTH_PARAM = "minimumLength";
private readonly int minimumLength;

/// <summary>Creates a new <see cref="JapaneseKatakanaStemFilterFactory"/></summary>
/// <summary>
/// Creates a new <see cref="JapaneseKatakanaStemFilterFactory"/>
/// </summary>
/// <exception cref="ArgumentException">Thrown if any of the <paramref name="args"/> key/value pairs are invalid.</exception>
public JapaneseKatakanaStemFilterFactory(IDictionary<string, string> 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));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Lucene.Net.Benchmark/ByTask/Benchmark.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 <algorithm file>");
//Environment.Exit(1);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Lucene.Net.Benchmark/Quality/Trec/QueryDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 <topicsFile> <qrelsFile> <submissionFile> <indexDir> [querySpec]");
//Console.Error.WriteLine("topicsFile: input file containing queries");
//Console.Error.WriteLine("qrelsFile: input file containing relevance judgements");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 <index-dir>");
//Environment.Exit(1);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Lucene.Net.Benchmark/Utils/ExtractWikipedia.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/Lucene.Net.Facet/Taxonomy/PrintTaxonomyStats.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ namespace Lucene.Net.Facet.Taxonomy
using FSDirectory = Lucene.Net.Store.FSDirectory;

/// <summary>
/// Prints how many ords are under each dimension.
/// Prints how many ords are under each dimension.
/// </summary>

// 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
Expand All @@ -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;
}

Expand Down Expand Up @@ -114,4 +114,4 @@ private static void PrintAllChildren(TextWriter @out, TaxonomyReader r, int ord,
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Lucene.Net.Misc/Index/IndexSplitter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 <srcDir> -l (list the segments and their sizes)");
//Console.Error.WriteLine("IndexSplitter <srcDir> <destDir> <segments>+");
//Console.Error.WriteLine("IndexSplitter <srcDir> -d (delete the following segments)");
Expand Down
6 changes: 3 additions & 3 deletions src/Lucene.Net.Misc/Index/MultiPassIndexSplitter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 <outputDir> -num <numParts> [-seq] <inputIndex1> [<inputIndex2 ...]");
//Console.Error.WriteLine("\tinputIndex\tpath to input index, multiple values are ok");
//Console.Error.WriteLine("\t-out ouputDir\tpath to output directory to contain partial indexes");
Expand Down
2 changes: 1 addition & 1 deletion src/Lucene.Net.Misc/Misc/GetTermInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public static void Main(string[] args)
else
{
// LUCENENET specific - our wrapper console shows the correct usage
throw new ArgumentException();
throw new ArgumentException("GetTermInfo requires 3 arguments", nameof(args));
//Usage();
//Environment.Exit(1);
}
Expand Down
Loading

0 comments on commit e95e250

Please sign in to comment.