You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucenenet.apache.org by la...@apache.org on 2023/04/09 16:48:19 UTC

[lucenenet] branch master updated: BREAKING: remove virtual call from the constructor for ByteArrayDataOutput (#814)

This is an automated email from the ASF dual-hosted git repository.

laimis pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/lucenenet.git


The following commit(s) were added to refs/heads/master by this push:
     new ecdb549b0 BREAKING: remove virtual call from the constructor for ByteArrayDataOutput (#814)
ecdb549b0 is described below

commit ecdb549b0bfff89565bcbcaf461ee0916838a0fd
Author: Laimonas Simutis <la...@gmail.com>
AuthorDate: Sun Apr 9 09:48:12 2023 -0700

    BREAKING: remove virtual call from the constructor for ByteArrayDataOutput (#814)
---
 src/Lucene.Net/Store/ByteArrayDataOutput.cs | 48 ++++++++++++++++++++++++-----
 1 file changed, 40 insertions(+), 8 deletions(-)

diff --git a/src/Lucene.Net/Store/ByteArrayDataOutput.cs b/src/Lucene.Net/Store/ByteArrayDataOutput.cs
index 99642f93b..716619ab7 100644
--- a/src/Lucene.Net/Store/ByteArrayDataOutput.cs
+++ b/src/Lucene.Net/Store/ByteArrayDataOutput.cs
@@ -1,5 +1,6 @@
 using Lucene.Net.Diagnostics;
 using Lucene.Net.Support;
+using System;
 
 namespace Lucene.Net.Store
 {
@@ -38,26 +39,57 @@ namespace Lucene.Net.Store
 
         public ByteArrayDataOutput(byte[] bytes)
         {
-            Reset(bytes);
+            // LUCENENET: Changed to call private method to avoid virtual method call in constructor
+            ResetInternal(bytes, 0, bytes?.Length ?? 0);
         }
 
         public ByteArrayDataOutput(byte[] bytes, int offset, int len)
         {
-            Reset(bytes, offset, len);
+            // LUCENENET: Changed to call private method to avoid virtual method call in constructor
+            ResetInternal(bytes, offset, len);
         }
 
         public ByteArrayDataOutput()
         {
-            Reset(BytesRef.EMPTY_BYTES);
+            // LUCENENET: Changed to call private method to avoid virtual method call in constructor
+            ResetInternal(BytesRef.EMPTY_BYTES, 0, BytesRef.EMPTY_BYTES.Length);
         }
 
-        public virtual void Reset(byte[] bytes)
-        {
-            Reset(bytes, 0, bytes.Length);
-        }
+        /// <summary>
+        /// 
+        /// NOTE: When overriding this method, be aware that the constructor of this class calls 
+        /// a private method and not this virtual method. So if you need to override
+        /// the behavior during the initialization, call your own private method from the constructor
+        /// with whatever custom behavior you need.
+        /// </summary>
+        public virtual void Reset(byte[] bytes) =>
+            ResetInternal(bytes, 0, bytes?.Length ?? 0);
+
+        /// <summary>
+        /// 
+        /// NOTE: When overriding this method, be aware that the constructor of this class calls 
+        /// a private method and not this virtual method. So if you need to override
+        /// the behavior during the initialization, call your own private method from the constructor
+        /// with whatever custom behavior you need.
+        /// </summary>
+        public virtual void Reset(byte[] bytes, int offset, int len) =>
+            ResetInternal(bytes, offset, len);
 
-        public virtual void Reset(byte[] bytes, int offset, int len)
+        // LUCENENET specific - created a private method that can be called
+        // from the constructor and the Reset methods to avoid virtual method
+        // calls in the constructor.
+        private void ResetInternal(byte[] bytes, int offset, int len)
         {
+            // LUCENENET: Added guard clauses
+            if (bytes is null)
+                throw new ArgumentNullException(nameof(bytes));
+            if (offset < 0)
+                throw new ArgumentOutOfRangeException(nameof(offset), offset, "Non-negative number required.");
+            if (len < 0)
+                throw new ArgumentOutOfRangeException(nameof(len), len, "Non-negative number required.");
+            if (bytes.Length - offset < len)
+                throw new ArgumentException("Offset and length were out of bounds for the array or length is greater than the number of elements from index to the end of the source array.");
+                
             this.bytes = bytes;
             pos = offset;
             limit = offset + len;