Apache Lucene open-source search software
Ensure that FilterXxx properly delegate to the decorated instances (#15469)
Benjamin Lerer6af9e96754fe
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txtindex 2b78b986808..154ce7c7595 100644--- a/lucene/CHANGES.txt+++ b/lucene/CHANGES.txt@@ -56,6 +56,8 @@ API Changes* GITHUB#14615 : Remove unnecessary public methods in FuzzySet (Greg Miller)+* GITHUB#15452: Ensure that Filter classes properly delegate to the decorated instances (Benjamin Lerer)+* GITHUB#15507: Expose MergeRateLimiter in ConcurrentMergeScheduler.runOnMergeFinished hook.The runOnMergeFinished method signature now includes OneMerge and MergeRateLimiter parameters,enabling applications to monitor merge performance metrics like pause time, throttling behavior,diff --git a/lucene/core/src/java/org/apache/lucene/index/FilterBinaryDocValues.java b/lucene/core/src/java/org/apache/lucene/index/FilterBinaryDocValues.javaindex fac56fae5ca..72db1522be9 100644--- a/lucene/core/src/java/org/apache/lucene/index/FilterBinaryDocValues.java+++ b/lucene/core/src/java/org/apache/lucene/index/FilterBinaryDocValues.java@@ -20,6 +20,7 @@ package org.apache.lucene.index;import java.io.IOException;import java.util.Objects;import org.apache.lucene.util.BytesRef;+import org.apache.lucene.util.FixedBitSet;/** Delegates all methods to a wrapped {@link BinaryDocValues}. */public abstract class FilterBinaryDocValues extends BinaryDocValues {@@ -62,4 +63,14 @@ public abstract class FilterBinaryDocValues extends BinaryDocValues {public BytesRef binaryValue() throws IOException {return in.binaryValue();}++ @Override+ public void intoBitSet(int upTo, FixedBitSet bitSet, int offset) throws IOException {+ in.intoBitSet(upTo, bitSet, offset);+ }++ @Override+ public int docIDRunEnd() throws IOException {+ return in.docIDRunEnd();+ }}diff --git a/lucene/core/src/java/org/apache/lucene/index/FilterCodecReader.java b/lucene/core/src/java/org/apache/lucene/index/FilterCodecReader.javaindex 737aedce204..3cb3d74d128 100644--- a/lucene/core/src/java/org/apache/lucene/index/FilterCodecReader.java+++ b/lucene/core/src/java/org/apache/lucene/index/FilterCodecReader.java@@ -26,6 +26,7 @@ import org.apache.lucene.codecs.PointsReader;import org.apache.lucene.codecs.StoredFieldsReader;import org.apache.lucene.codecs.TermVectorsReader;import org.apache.lucene.util.Bits;+import org.apache.lucene.util.LiveDocs;/*** A <code>FilterCodecReader</code> contains another CodecReader, which it uses as its basic source@@ -130,6 +131,16 @@ public abstract class FilterCodecReader extends CodecReader {in.checkIntegrity();}+ @Override+ public LiveDocs getLiveDocsWithDeletedIterator() {+ return in.getLiveDocsWithDeletedIterator();+ }++ @Override+ public boolean hasDeletions() {+ return in.hasDeletions();+ }+/** Returns the wrapped {@link CodecReader}. */public CodecReader getDelegate() {return in;diff --git a/lucene/core/src/java/org/apache/lucene/index/FilterDirectoryReader.java b/lucene/core/src/java/org/apache/lucene/index/FilterDirectoryReader.javaindex c286dfc34e3..5dfce7b03f1 100644--- a/lucene/core/src/java/org/apache/lucene/index/FilterDirectoryReader.java+++ b/lucene/core/src/java/org/apache/lucene/index/FilterDirectoryReader.java@@ -163,6 +163,11 @@ public abstract class FilterDirectoryReader extends DirectoryReader {in.close();}+ @Override+ public boolean hasDeletions() {+ return in.hasDeletions();+ }+/** Returns the wrapped {@link DirectoryReader}. */public DirectoryReader getDelegate() {return in;diff --git a/lucene/core/src/java/org/apache/lucene/index/FilterLeafReader.java b/lucene/core/src/java/org/apache/lucene/index/FilterLeafReader.javaindex 174072407a8..ff529188514 100644--- a/lucene/core/src/java/org/apache/lucene/index/FilterLeafReader.java+++ b/lucene/core/src/java/org/apache/lucene/index/FilterLeafReader.java@@ -19,12 +19,16 @@ package org.apache.lucene.index;import java.io.IOException;import java.util.Iterator;import org.apache.lucene.search.AcceptDocs;+import org.apache.lucene.search.DocAndFloatFeatureBuffer;import org.apache.lucene.search.KnnCollector;import org.apache.lucene.util.AttributeSource;import org.apache.lucene.util.Bits;import org.apache.lucene.util.BytesRef;+import org.apache.lucene.util.FixedBitSet;import org.apache.lucene.util.IOBooleanSupplier;+import org.apache.lucene.util.LiveDocs;import org.apache.lucene.util.Unwrappable;+import org.apache.lucene.util.automaton.CompiledAutomaton;/*** A <code>FilterLeafReader</code> contains another LeafReader, which it uses as its basic source of@@ -159,6 +163,21 @@ public abstract class FilterLeafReader extends LeafReader {public Object getStats() throws IOException {return in.getStats();}++ @Override+ public TermsEnum intersect(CompiledAutomaton compiled, BytesRef startTerm) throws IOException {+ return in.intersect(compiled, startTerm);+ }++ @Override+ public BytesRef getMin() throws IOException {+ return in.getMin();+ }++ @Override+ public BytesRef getMax() throws IOException {+ return in.getMax();+ }}/** Base class for filtering {@link TermsEnum} implementations. */@@ -317,6 +336,21 @@ public abstract class FilterLeafReader extends LeafReader {public PostingsEnum unwrap() {return in;}++ @Override+ public void nextPostings(int upTo, DocAndFloatFeatureBuffer buffer) throws IOException {+ in.nextPostings(upTo, buffer);+ }++ @Override+ public void intoBitSet(int upTo, FixedBitSet bitSet, int offset) throws IOException {+ in.intoBitSet(upTo, bitSet, offset);+ }++ @Override+ public int docIDRunEnd() throws IOException {+ return in.docIDRunEnd();+ }}/** The underlying LeafReader. */@@ -474,6 +508,16 @@ public abstract class FilterLeafReader extends LeafReader {in.checkIntegrity();}+ @Override+ public boolean hasDeletions() {+ return in.hasDeletions();+ }++ @Override+ public LiveDocs getLiveDocsWithDeletedIterator() {+ return in.getLiveDocsWithDeletedIterator();+ }+/** Returns the wrapped {@link LeafReader}. */public LeafReader getDelegate() {return in;diff --git a/lucene/core/src/java/org/apache/lucene/index/FilterNumericDocValues.java b/lucene/core/src/java/org/apache/lucene/index/FilterNumericDocValues.javaindex 3ece4900bf1..2f7d5c0d883 100644--- a/lucene/core/src/java/org/apache/lucene/index/FilterNumericDocValues.java+++ b/lucene/core/src/java/org/apache/lucene/index/FilterNumericDocValues.java@@ -19,6 +19,7 @@ package org.apache.lucene.index;import java.io.IOException;import java.util.Objects;+import org.apache.lucene.util.FixedBitSet;/** Delegates all methods to a wrapped {@link NumericDocValues}. */public abstract class FilterNumericDocValues extends NumericDocValues {@@ -61,4 +62,20 @@ public abstract class FilterNumericDocValues extends NumericDocValues {public long longValue() throws IOException {return in.longValue();}++ @Override+ public void longValues(int size, int[] docs, long[] values, long defaultValue)+ throws IOException {+ in.longValues(size, docs, values, defaultValue);+ }++ @Override+ public void intoBitSet(int upTo, FixedBitSet bitSet, int offset) throws IOException {+ in.intoBitSet(upTo, bitSet, offset);+ }++ @Override+ public int docIDRunEnd() throws IOException {+ return in.docIDRunEnd();+ }}diff --git a/lucene/core/src/java/org/apache/lucene/index/FilterSortedDocValues.java b/lucene/core/src/java/org/apache/lucene/index/FilterSortedDocValues.javaindex 01c8e3edfca..66c0fa02969 100644--- a/lucene/core/src/java/org/apache/lucene/index/FilterSortedDocValues.java+++ b/lucene/core/src/java/org/apache/lucene/index/FilterSortedDocValues.java@@ -19,6 +19,7 @@ package org.apache.lucene.index;import java.io.IOException;import java.util.Objects;import org.apache.lucene.util.BytesRef;+import org.apache.lucene.util.FixedBitSet;import org.apache.lucene.util.automaton.CompiledAutomaton;/** Delegates all methods to a wrapped {@link SortedDocValues}. */@@ -87,4 +88,14 @@ public abstract class FilterSortedDocValues extends SortedDocValues {public long cost() {return in.cost();}++ @Override+ public void intoBitSet(int upTo, FixedBitSet bitSet, int offset) throws IOException {+ in.intoBitSet(upTo, bitSet, offset);+ }++ @Override+ public int docIDRunEnd() throws IOException {+ return in.docIDRunEnd();+ }}diff --git a/lucene/core/src/java/org/apache/lucene/index/FilterSortedNumericDocValues.java b/lucene/core/src/java/org/apache/lucene/index/FilterSortedNumericDocValues.javaindex 7d2c7f1b280..ff50a8a9b9c 100644--- a/lucene/core/src/java/org/apache/lucene/index/FilterSortedNumericDocValues.java+++ b/lucene/core/src/java/org/apache/lucene/index/FilterSortedNumericDocValues.java@@ -18,6 +18,7 @@ package org.apache.lucene.index;import java.io.IOException;import java.util.Objects;+import org.apache.lucene.util.FixedBitSet;/** Delegates all methods to a wrapped {@link SortedNumericDocValues}. */public abstract class FilterSortedNumericDocValues extends SortedNumericDocValues {@@ -65,4 +66,14 @@ public abstract class FilterSortedNumericDocValues extends SortedNumericDocValuepublic long cost() {return in.cost();}++ @Override+ public void intoBitSet(int upTo, FixedBitSet bitSet, int offset) throws IOException {+ in.intoBitSet(upTo, bitSet, offset);+ }++ @Override+ public int docIDRunEnd() throws IOException {+ return in.docIDRunEnd();+ }}diff --git a/lucene/core/src/java/org/apache/lucene/index/FilterSortedSetDocValues.java b/lucene/core/src/java/org/apache/lucene/index/FilterSortedSetDocValues.javaindex 92c5d344e57..56b88d82b1d 100644--- a/lucene/core/src/java/org/apache/lucene/index/FilterSortedSetDocValues.java+++ b/lucene/core/src/java/org/apache/lucene/index/FilterSortedSetDocValues.java@@ -19,6 +19,7 @@ package org.apache.lucene.index;import java.io.IOException;import java.util.Objects;import org.apache.lucene.util.BytesRef;+import org.apache.lucene.util.FixedBitSet;import org.apache.lucene.util.automaton.CompiledAutomaton;/** Delegates all methods to a wrapped {@link SortedSetDocValues}. */@@ -92,4 +93,14 @@ public class FilterSortedSetDocValues extends SortedSetDocValues {public long cost() {return in.cost();}++ @Override+ public void intoBitSet(int upTo, FixedBitSet bitSet, int offset) throws IOException {+ in.intoBitSet(upTo, bitSet, offset);+ }++ @Override+ public int docIDRunEnd() throws IOException {+ return in.docIDRunEnd();+ }}diff --git a/lucene/core/src/java/org/apache/lucene/search/BooleanScorerSupplier.java b/lucene/core/src/java/org/apache/lucene/search/BooleanScorerSupplier.javaindex 480a524de54..1799fe7d7b1 100644--- a/lucene/core/src/java/org/apache/lucene/search/BooleanScorerSupplier.java+++ b/lucene/core/src/java/org/apache/lucene/search/BooleanScorerSupplier.java@@ -428,12 +428,12 @@ final class BooleanScorerSupplier extends ScorerSupplier {conjunctionScorer =new FilterScorer(inner) {@Override- public float score() throws IOException {+ public float score() {return 0f;}@Override- public float getMaxScore(int upTo) throws IOException {+ public float getMaxScore(int upTo) {return 0f;}};@@ -477,19 +477,25 @@ final class BooleanScorerSupplier extends ScorerSupplier {}if (requiredScoring.isEmpty()) {- // Scores are needed but we only have a filter clause- // BooleanWeight expects that calling score() is ok so we need to wrap- // to prevent score() from being propagated+ // Scores are needed, but we only have a filter clause+ // BooleanWeight expects that calling score(), getMaxScore() and advanceShallow()+ // is ok so we need to override those methods to stop them+ // from being propagated.return new FilterScorer(req) {@Override- public float score() throws IOException {+ public float score() {return 0f;}@Override- public float getMaxScore(int upTo) throws IOException {+ public float getMaxScore(int upTo) {return 0f;}++ @Override+ public int advanceShallow(int target) {+ return DocIdSetIterator.NO_MORE_DOCS;+ }};}diff --git a/lucene/core/src/java/org/apache/lucene/search/FilterScorable.java b/lucene/core/src/java/org/apache/lucene/search/FilterScorable.javaindex 890a3f3c2d5..3bbf80813d7 100644--- a/lucene/core/src/java/org/apache/lucene/search/FilterScorable.java+++ b/lucene/core/src/java/org/apache/lucene/search/FilterScorable.java@@ -49,4 +49,14 @@ public class FilterScorable extends Scorable {public Collection<ChildScorable> getChildren() throws IOException {return Collections.singletonList(new ChildScorable(in, "FILTER"));}++ @Override+ public float smoothingScore(int docId) throws IOException {+ return in.smoothingScore(docId);+ }++ @Override+ public void setMinCompetitiveScore(float minScore) throws IOException {+ in.setMinCompetitiveScore(minScore);+ }}diff --git a/lucene/core/src/java/org/apache/lucene/search/FilterScorer.java b/lucene/core/src/java/org/apache/lucene/search/FilterScorer.javaindex 9ea6fad407e..e02225d5cdb 100644--- a/lucene/core/src/java/org/apache/lucene/search/FilterScorer.java+++ b/lucene/core/src/java/org/apache/lucene/search/FilterScorer.java@@ -17,6 +17,9 @@package org.apache.lucene.search;import java.io.IOException;+import java.util.Collection;+import java.util.Collections;+import org.apache.lucene.util.Bits;import org.apache.lucene.util.Unwrappable;/**@@ -64,6 +67,32 @@ public abstract class FilterScorer extends Scorer implements Unwrappable<Scorer>return in.twoPhaseIterator();}+ @Override+ public void setMinCompetitiveScore(float minScore) throws IOException {+ in.setMinCompetitiveScore(minScore);+ }++ @Override+ public Collection<ChildScorable> getChildren() throws IOException {+ return Collections.singletonList(new ChildScorable(in, "FILTER"));+ }++ @Override+ public float smoothingScore(int docId) throws IOException {+ return in.smoothingScore(docId);+ }++ @Override+ public void nextDocsAndScores(int upTo, Bits liveDocs, DocAndFloatFeatureBuffer buffer)+ throws IOException {+ in.nextDocsAndScores(upTo, liveDocs, buffer);+ }++ @Override+ public int advanceShallow(int target) throws IOException {+ return in.advanceShallow(target);+ }+@Overridepublic Scorer unwrap() {return in;diff --git a/lucene/core/src/java/org/apache/lucene/search/FilterWeight.java b/lucene/core/src/java/org/apache/lucene/search/FilterWeight.javaindex 16bb75ef406..4d67cd0e8ed 100644--- a/lucene/core/src/java/org/apache/lucene/search/FilterWeight.java+++ b/lucene/core/src/java/org/apache/lucene/search/FilterWeight.java@@ -67,4 +67,9 @@ public abstract class FilterWeight extends Weight {public ScorerSupplier scorerSupplier(LeafReaderContext context) throws IOException {return in.scorerSupplier(context);}++ @Override+ public int count(LeafReaderContext context) throws IOException {+ return in.count(context);+ }}diff --git a/lucene/core/src/java/org/apache/lucene/store/FilterIndexInput.java b/lucene/core/src/java/org/apache/lucene/store/FilterIndexInput.javaindex 933701b3c3d..7448a2d8fec 100644--- a/lucene/core/src/java/org/apache/lucene/store/FilterIndexInput.java+++ b/lucene/core/src/java/org/apache/lucene/store/FilterIndexInput.java@@ -17,6 +17,9 @@package org.apache.lucene.store;import java.io.IOException;+import java.util.Map;+import java.util.Optional;+import java.util.Set;import java.util.concurrent.CopyOnWriteArrayList;import org.apache.lucene.internal.tests.TestSecrets;@@ -77,6 +80,12 @@ public class FilterIndexInput extends IndexInput {in.close();}+ @Override+ public IndexInput clone() {+ throw new RuntimeException(+ "Clone not implemented by the filter class: " + getClass().getName());+ }+@Overridepublic long getFilePointer() {return in.getFilePointer();@@ -106,4 +115,105 @@ public class FilterIndexInput extends IndexInput {public void readBytes(byte[] b, int offset, int len) throws IOException {in.readBytes(b, offset, len);}++ @Override+ public Set<String> readSetOfStrings() throws IOException {+ return in.readSetOfStrings();+ }++ @Override+ public Map<String, String> readMapOfStrings() throws IOException {+ return in.readMapOfStrings();+ }++ @Override+ public String readString() throws IOException {+ return in.readString();+ }++ @Override+ public long readZLong() throws IOException {+ return in.readZLong();+ }++ @Override+ public long readVLong() throws IOException {+ return in.readVLong();+ }++ @Override+ public void readFloats(float[] floats, int offset, int len) throws IOException {+ in.readFloats(floats, offset, len);+ }++ @Override+ public void readInts(int[] dst, int offset, int length) throws IOException {+ in.readInts(dst, offset, length);+ }++ @Override+ public void readLongs(long[] dst, int offset, int length) throws IOException {+ in.readLongs(dst, offset, length);+ }++ @Override+ public long readLong() throws IOException {+ return in.readLong();+ }++ @Override+ public int readZInt() throws IOException {+ return in.readZInt();+ }++ @Override+ public int readVInt() throws IOException {+ return in.readVInt();+ }++ @Override+ public int readInt() throws IOException {+ return in.readInt();+ }++ @Override+ public short readShort() throws IOException {+ return in.readShort();+ }++ @Override+ public void readBytes(byte[] b, int offset, int len, boolean useBuffer) throws IOException {+ in.readBytes(b, offset, len, useBuffer);+ }++ @Override+ public void prefetch(long offset, long length) throws IOException {+ in.prefetch(offset, length);+ }++ @Override+ public RandomAccessInput randomAccessSlice(long offset, long length) throws IOException {+ return in.randomAccessSlice(offset, length);+ }++ @Override+ public void updateIOContext(IOContext context) throws IOException {+ in.updateIOContext(context);+ }++ @Override+ public Optional<Boolean> isLoaded() {+ return in.isLoaded();+ }++ @Override+ public IndexInput slice(String sliceDescription, long offset, long length, IOContext context)+ throws IOException {+ return in.slice(sliceDescription, offset, length, context);+ }++ @Override+ public void skipBytes(long numBytes) throws IOException {+ in.skipBytes(numBytes);+ }}diff --git a/lucene/core/src/java/org/apache/lucene/store/FilterIndexOutput.java b/lucene/core/src/java/org/apache/lucene/store/FilterIndexOutput.javaindex 1faec626d08..2511f4a523b 100644--- a/lucene/core/src/java/org/apache/lucene/store/FilterIndexOutput.java+++ b/lucene/core/src/java/org/apache/lucene/store/FilterIndexOutput.java@@ -18,6 +18,8 @@package org.apache.lucene.store;import java.io.IOException;+import java.util.Map;+import java.util.Set;/*** IndexOutput implementation that delegates calls to another directory. This class can be used to@@ -57,6 +59,11 @@ public class FilterIndexOutput extends IndexOutput {return out;}+ @Override+ public String getName() {+ return out.getName();+ }+@Overridepublic void close() throws IOException {out.close();@@ -81,4 +88,55 @@ public class FilterIndexOutput extends IndexOutput {public void writeBytes(byte[] b, int offset, int length) throws IOException {out.writeBytes(b, offset, length);}++ @Override+ public void writeBytes(byte[] b, int length) throws IOException {+ out.writeBytes(b, length);+ }++ @Override+ public void writeInt(int i) throws IOException {+ out.writeInt(i);+ }++ @Override+ public void writeShort(short i) throws IOException {+ out.writeShort(i);+ }++ @Override+ public void writeLong(long i) throws IOException {+ out.writeLong(i);+ }++ @Override+ public void writeString(String s) throws IOException {+ out.writeString(s);+ }++ @Override+ public void copyBytes(DataInput input, long numBytes) throws IOException {+ out.copyBytes(input, numBytes);+ }++ @Override+ public void writeMapOfStrings(Map<String, String> map) throws IOException {+ out.writeMapOfStrings(map);+ }++ @Override+ public void writeSetOfStrings(Set<String> set) throws IOException {+ out.writeSetOfStrings(set);+ }++ @Override+ public void writeGroupVInts(int[] values, int limit) throws IOException {+ out.writeGroupVInts(values, limit);+ }++ @Override+ @Deprecated+ public void writeGroupVInts(long[] values, int limit) throws IOException {+ out.writeGroupVInts(values, limit);+ }}diff --git a/lucene/core/src/test/org/apache/lucene/codecs/TestFilterCodec.java b/lucene/core/src/test/org/apache/lucene/codecs/TestFilterCodec.javanew file mode 100644index 00000000000..23208a883bb--- /dev/null+++ b/lucene/core/src/test/org/apache/lucene/codecs/TestFilterCodec.java@@ -0,0 +1,28 @@+/*+ * Licensed to the Apache Software Foundation (ASF) under one or more+ * contributor license agreements. See the NOTICE file distributed with+ * this work for additional information regarding copyright ownership.+ * The ASF licenses this file to You under the Apache License, Version 2.0+ * (the "License"); you may not use this file except in compliance with+ * the License. You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */+package org.apache.lucene.codecs;++import org.apache.lucene.tests.util.LuceneTestCase;+import org.junit.Test;++public class TestFilterCodec extends LuceneTestCase {++ @Test+ public void testDeclaredMethodsOverridden() {+ assertDelegatorOverridesAllRequiredMethods(FilterCodec.class);+ }+}diff --git a/lucene/core/src/test/org/apache/lucene/codecs/lucene90/TestLucene90StoredFieldsFormat.java b/lucene/core/src/test/org/apache/lucene/codecs/lucene90/TestLucene90StoredFieldsFormat.javaindex ae9c6d7b53f..8d612b9d918 100644--- a/lucene/core/src/test/org/apache/lucene/codecs/lucene90/TestLucene90StoredFieldsFormat.java+++ b/lucene/core/src/test/org/apache/lucene/codecs/lucene90/TestLucene90StoredFieldsFormat.java@@ -80,6 +80,13 @@ public class TestLucene90StoredFieldsFormat extends BaseStoredFieldsFormatTestCapublic IndexInput slice(String sliceDescription, long offset, long length) throws IOException {return new CountingPrefetchIndexInput(in.slice(sliceDescription, offset, length), counter);}++ @Override+ public IndexInput slice(String sliceDescription, long offset, long length, IOContext context)+ throws IOException {+ return new CountingPrefetchIndexInput(+ in.slice(sliceDescription, offset, length, context), counter);+ }}public void testSkipRedundantPrefetches() throws IOException {diff --git a/lucene/core/src/test/org/apache/lucene/codecs/lucene90/TestLucene90TermVectorsFormat.java b/lucene/core/src/test/org/apache/lucene/codecs/lucene90/TestLucene90TermVectorsFormat.javaindex 8cc6dda1c1d..2aec6afc5e4 100644--- a/lucene/core/src/test/org/apache/lucene/codecs/lucene90/TestLucene90TermVectorsFormat.java+++ b/lucene/core/src/test/org/apache/lucene/codecs/lucene90/TestLucene90TermVectorsFormat.java@@ -78,6 +78,13 @@ public class TestLucene90TermVectorsFormat extends BaseTermVectorsFormatTestCasereturn new CountingPrefetchIndexInput(in.clone(), counter);}+ @Override+ public IndexInput slice(String sliceDescription, long offset, long length, IOContext context)+ throws IOException {+ return new CountingPrefetchIndexInput(+ in.slice(sliceDescription, offset, length, context), counter);+ }+@Overridepublic IndexInput slice(String sliceDescription, long offset, long length) throws IOException {return new CountingPrefetchIndexInput(in.slice(sliceDescription, offset, length), counter);diff --git a/lucene/core/src/test/org/apache/lucene/index/TestFilterBinaryDocValues.java b/lucene/core/src/test/org/apache/lucene/index/TestFilterBinaryDocValues.javanew file mode 100644index 00000000000..29637c551a4--- /dev/null+++ b/lucene/core/src/test/org/apache/lucene/index/TestFilterBinaryDocValues.java@@ -0,0 +1,28 @@+/*+ * Licensed to the Apache Software Foundation (ASF) under one or more+ * contributor license agreements. See the NOTICE file distributed with+ * this work for additional information regarding copyright ownership.+ * The ASF licenses this file to You under the Apache License, Version 2.0+ * (the "License"); you may not use this file except in compliance with+ * the License. You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */+package org.apache.lucene.index;++import org.apache.lucene.tests.util.LuceneTestCase;+import org.junit.Test;++public class TestFilterBinaryDocValues extends LuceneTestCase {++ @Test+ public void testDeclaredMethodsOverridden() {+ assertDelegatorOverridesAllRequiredMethods(FilterBinaryDocValues.class);+ }+}diff --git a/lucene/core/src/test/org/apache/lucene/index/TestFilterCodecReader.java b/lucene/core/src/test/org/apache/lucene/index/TestFilterCodecReader.javaindex e9ee2f32627..66fa2ac22b0 100644--- a/lucene/core/src/test/org/apache/lucene/index/TestFilterCodecReader.java+++ b/lucene/core/src/test/org/apache/lucene/index/TestFilterCodecReader.java@@ -17,8 +17,6 @@package org.apache.lucene.index;import java.io.IOException;-import java.lang.reflect.Method;-import java.lang.reflect.Modifier;import org.apache.lucene.document.Document;import org.apache.lucene.store.Directory;import org.apache.lucene.tests.util.LuceneTestCase;@@ -26,8 +24,7 @@ import org.apache.lucene.tests.util.LuceneTestCase;public class TestFilterCodecReader extends LuceneTestCase {public void testDeclaredMethodsOverridden() throws Exception {- final Class<?> subClass = FilterCodecReader.class;- implTestDeclaredMethodsOverridden(subClass.getSuperclass(), subClass);+ assertDelegatorOverridesAllRequiredMethods(FilterCodecReader.class);}public void testGetDelegate() throws IOException {@@ -44,25 +41,4 @@ public class TestFilterCodecReader extends LuceneTestCase {}}}-- private void implTestDeclaredMethodsOverridden(Class<?> superClass, Class<?> subClass)- throws Exception {- for (final Method superClassMethod : superClass.getDeclaredMethods()) {- final int modifiers = superClassMethod.getModifiers();- if (Modifier.isPrivate(modifiers)) continue;- if (Modifier.isFinal(modifiers)) continue;- if (Modifier.isStatic(modifiers)) continue;- try {- final Method subClassMethod =- subClass.getDeclaredMethod(- superClassMethod.getName(), superClassMethod.getParameterTypes());- assertEquals(- "getReturnType() difference",- superClassMethod.getReturnType(),- subClassMethod.getReturnType());- } catch (NoSuchMethodException _) {- fail(subClass + " needs to override '" + superClassMethod + "'");- }- }- }}diff --git a/lucene/core/src/test/org/apache/lucene/index/TestFilterDirectoryReader.java b/lucene/core/src/test/org/apache/lucene/index/TestFilterDirectoryReader.javaindex 6bd144dc0ad..5ab73e93c9d 100644--- a/lucene/core/src/test/org/apache/lucene/index/TestFilterDirectoryReader.java+++ b/lucene/core/src/test/org/apache/lucene/index/TestFilterDirectoryReader.java@@ -27,6 +27,7 @@ import org.apache.lucene.index.FilterDirectoryReader.SubReaderWrapper;import org.apache.lucene.store.Directory;import org.apache.lucene.tests.util.LuceneTestCase;import org.apache.lucene.util.IOUtils;+import org.junit.Test;public class TestFilterDirectoryReader extends LuceneTestCase {@@ -197,4 +198,9 @@ public class TestFilterDirectoryReader extends LuceneTestCase {w.close();dir.close();}++ @Test+ public void testOverrides() throws Exception {+ assertDelegatorOverridesAllRequiredMethods(FilterDirectoryReader.class);+ }}diff --git a/lucene/core/src/test/org/apache/lucene/index/TestFilterIndexInput.java b/lucene/core/src/test/org/apache/lucene/index/TestFilterIndexInput.javaindex d43b580f601..1f77fa76dab 100644--- a/lucene/core/src/test/org/apache/lucene/index/TestFilterIndexInput.java+++ b/lucene/core/src/test/org/apache/lucene/index/TestFilterIndexInput.java@@ -17,8 +17,6 @@package org.apache.lucene.index;import java.io.IOException;-import java.lang.reflect.Method;-import java.lang.reflect.Modifier;import java.util.Random;import org.apache.lucene.store.Directory;import org.apache.lucene.store.FSDirectory;@@ -61,24 +59,7 @@ public class TestFilterIndexInput extends TestIndexInput {@Testpublic void testOverrides() {- for (Method m : FilterIndexInput.class.getMethods()) {- if (m.getName().contains("clone")) {- // special case- continue;- }- if (m.getDeclaringClass() == FilterIndexInput.class) {- // verify that only abstract methods are overridden- Method indexInputMethod;- try {- indexInputMethod = IndexInput.class.getMethod(m.getName(), m.getParameterTypes());- assertTrue(- "Non-abstract method " + m.getName() + " is overridden",- Modifier.isAbstract(indexInputMethod.getModifiers()));- } catch (Exception e) {- assertTrue(e instanceof NoSuchMethodException);- }- }- }+ assertDelegatorOverridesAllRequiredMethods(FilterIndexInput.class);}public void testUnwrap() throws IOException {diff --git a/lucene/core/src/test/org/apache/lucene/index/TestFilterIndexOutput.java b/lucene/core/src/test/org/apache/lucene/index/TestFilterIndexOutput.javanew file mode 100644index 00000000000..034771403d7--- /dev/null+++ b/lucene/core/src/test/org/apache/lucene/index/TestFilterIndexOutput.java@@ -0,0 +1,29 @@+/*+ * Licensed to the Apache Software Foundation (ASF) under one or more+ * contributor license agreements. See the NOTICE file distributed with+ * this work for additional information regarding copyright ownership.+ * The ASF licenses this file to You under the Apache License, Version 2.0+ * (the "License"); you may not use this file except in compliance with+ * the License. You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */+package org.apache.lucene.index;++import org.apache.lucene.store.FilterIndexOutput;+import org.apache.lucene.tests.util.LuceneTestCase;+import org.junit.Test;++public class TestFilterIndexOutput extends LuceneTestCase {++ @Test+ public void testOverrides() {+ assertDelegatorOverridesAllRequiredMethods(FilterIndexOutput.class);+ }+}diff --git a/lucene/core/src/test/org/apache/lucene/index/TestFilterLeafReader.java b/lucene/core/src/test/org/apache/lucene/index/TestFilterLeafReader.javaindex be1715279fd..4435483bd46 100644--- a/lucene/core/src/test/org/apache/lucene/index/TestFilterLeafReader.java+++ b/lucene/core/src/test/org/apache/lucene/index/TestFilterLeafReader.java@@ -17,8 +17,8 @@package org.apache.lucene.index;import java.io.IOException;-import java.lang.reflect.Method;-import java.lang.reflect.Modifier;+import java.util.Set;+import java.util.function.Consumer;import org.apache.lucene.document.Document;import org.apache.lucene.document.Field;import org.apache.lucene.search.DocIdSetIterator;@@ -180,39 +180,16 @@ public class TestFilterLeafReader extends LuceneTestCase {target.close();}- private static void checkOverrideMethods(Class<?> clazz)- throws NoSuchMethodException, SecurityException {- final Class<?> superClazz = clazz.getSuperclass();- for (Method m : superClazz.getMethods()) {- final int mods = m.getModifiers();- if (Modifier.isStatic(mods)- || Modifier.isAbstract(mods)- || Modifier.isFinal(mods)- || m.isSynthetic()- || m.getName().equals("attributes")- || m.getName().equals("getStats")) {- continue;- }- // The point of these checks is to ensure that methods that have a default- // impl through other methods are not overridden. This makes the number of- // methods to override to have a working impl minimal and prevents from some- // traps: for example, think about having getCoreCacheKey delegate to the- // filtered impl by default- final Method subM = clazz.getMethod(m.getName(), m.getParameterTypes());- if (subM.getDeclaringClass() == clazz- && m.getDeclaringClass() != Object.class- && m.getDeclaringClass() != subM.getDeclaringClass()) {- fail(clazz + " overrides " + m + " although it has a default impl");- }- }- }-public void testOverrideMethods() throws Exception {- checkOverrideMethods(FilterLeafReader.class);- checkOverrideMethods(FilterLeafReader.FilterFields.class);- checkOverrideMethods(FilterLeafReader.FilterTerms.class);- checkOverrideMethods(FilterLeafReader.FilterTermsEnum.class);- checkOverrideMethods(FilterLeafReader.FilterPostingsEnum.class);+ assertDelegatorOverridesAllRequiredMethods(FilterLeafReader.class);+ assertDelegatorOverridesAllRequiredMethods(+ FilterLeafReader.FilterFields.class,+ Set.of(+ Iterable.class.getMethod("forEach", Consumer.class),+ Iterable.class.getMethod("spliterator")));+ assertDelegatorOverridesAllRequiredMethods(FilterLeafReader.FilterTerms.class);+ assertDelegatorOverridesAllRequiredMethods(FilterLeafReader.FilterTermsEnum.class);+ assertDelegatorOverridesAllRequiredMethods(FilterLeafReader.FilterPostingsEnum.class);}public void testUnwrap() throws IOException {diff --git a/lucene/core/src/test/org/apache/lucene/index/TestFilterMergePolicy.java b/lucene/core/src/test/org/apache/lucene/index/TestFilterMergePolicy.javaindex bc979ae07c9..872f64fc813 100644--- a/lucene/core/src/test/org/apache/lucene/index/TestFilterMergePolicy.java+++ b/lucene/core/src/test/org/apache/lucene/index/TestFilterMergePolicy.java@@ -16,20 +16,11 @@*/package org.apache.lucene.index;-import java.lang.reflect.Method;-import java.lang.reflect.Modifier;import org.apache.lucene.tests.util.LuceneTestCase;public class TestFilterMergePolicy extends LuceneTestCase {public void testMethodsOverridden() {- for (Method m : MergePolicy.class.getDeclaredMethods()) {- if (Modifier.isFinal(m.getModifiers()) || Modifier.isPrivate(m.getModifiers())) continue;- try {- FilterMergePolicy.class.getDeclaredMethod(m.getName(), m.getParameterTypes());- } catch (NoSuchMethodException _) {- fail("FilterMergePolicy needs to override '" + m + "'");- }- }+ assertDelegatorOverridesAllRequiredMethods(FilterMergePolicy.class);}}diff --git a/lucene/core/src/test/org/apache/lucene/index/TestFilterNumericDocValues.java b/lucene/core/src/test/org/apache/lucene/index/TestFilterNumericDocValues.javanew file mode 100644index 00000000000..e054c94d2b6--- /dev/null+++ b/lucene/core/src/test/org/apache/lucene/index/TestFilterNumericDocValues.java@@ -0,0 +1,27 @@+/*+ * Licensed to the Apache Software Foundation (ASF) under one or more+ * contributor license agreements. See the NOTICE file distributed with+ * this work for additional information regarding copyright ownership.+ * The ASF licenses this file to You under the Apache License, Version 2.0+ * (the "License"); you may not use this file except in compliance with+ * the License. You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */+package org.apache.lucene.index;++import org.apache.lucene.tests.util.LuceneTestCase;+import org.junit.Test;++public class TestFilterNumericDocValues extends LuceneTestCase {+ @Test+ public void testDeclaredMethodsOverridden() {+ assertDelegatorOverridesAllRequiredMethods(FilterNumericDocValues.class);+ }+}diff --git a/lucene/core/src/test/org/apache/lucene/index/TestFilterSortedDocValues.java b/lucene/core/src/test/org/apache/lucene/index/TestFilterSortedDocValues.javanew file mode 100644index 00000000000..f09e51d7f3f--- /dev/null+++ b/lucene/core/src/test/org/apache/lucene/index/TestFilterSortedDocValues.java@@ -0,0 +1,28 @@+/*+ * Licensed to the Apache Software Foundation (ASF) under one or more+ * contributor license agreements. See the NOTICE file distributed with+ * this work for additional information regarding copyright ownership.+ * The ASF licenses this file to You under the Apache License, Version 2.0+ * (the "License"); you may not use this file except in compliance with+ * the License. You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */+package org.apache.lucene.index;++import org.apache.lucene.tests.util.LuceneTestCase;+import org.junit.Test;++public class TestFilterSortedDocValues extends LuceneTestCase {++ @Test+ public void testDeclaredMethodsOverridden() {+ assertDelegatorOverridesAllRequiredMethods(FilterSortedDocValues.class);+ }+}diff --git a/lucene/core/src/test/org/apache/lucene/index/TestFilterSortedNumericDocValues.java b/lucene/core/src/test/org/apache/lucene/index/TestFilterSortedNumericDocValues.javanew file mode 100644index 00000000000..dd1d33d4020--- /dev/null+++ b/lucene/core/src/test/org/apache/lucene/index/TestFilterSortedNumericDocValues.java@@ -0,0 +1,28 @@+/*+ * Licensed to the Apache Software Foundation (ASF) under one or more+ * contributor license agreements. See the NOTICE file distributed with+ * this work for additional information regarding copyright ownership.+ * The ASF licenses this file to You under the Apache License, Version 2.0+ * (the "License"); you may not use this file except in compliance with+ * the License. You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */+package org.apache.lucene.index;++import org.apache.lucene.tests.util.LuceneTestCase;+import org.junit.Test;++public class TestFilterSortedNumericDocValues extends LuceneTestCase {++ @Test+ public void testDeclaredMethodsOverridden() {+ assertDelegatorOverridesAllRequiredMethods(FilterSortedNumericDocValues.class);+ }+}diff --git a/lucene/core/src/test/org/apache/lucene/index/TestFilterSortedSetDocValues.java b/lucene/core/src/test/org/apache/lucene/index/TestFilterSortedSetDocValues.javanew file mode 100644index 00000000000..a31870b7fb2--- /dev/null+++ b/lucene/core/src/test/org/apache/lucene/index/TestFilterSortedSetDocValues.java@@ -0,0 +1,28 @@+/*+ * Licensed to the Apache Software Foundation (ASF) under one or more+ * contributor license agreements. See the NOTICE file distributed with+ * this work for additional information regarding copyright ownership.+ * The ASF licenses this file to You under the Apache License, Version 2.0+ * (the "License"); you may not use this file except in compliance with+ * the License. You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */+package org.apache.lucene.index;++import org.apache.lucene.tests.util.LuceneTestCase;+import org.junit.Test;++public class TestFilterSortedSetDocValues extends LuceneTestCase {++ @Test+ public void testDeclaredMethodsOverridden() {+ assertDelegatorOverridesAllRequiredMethods(FilterSortedSetDocValues.class);+ }+}diff --git a/lucene/core/src/test/org/apache/lucene/search/TestFilterCollector.java b/lucene/core/src/test/org/apache/lucene/search/TestFilterCollector.javanew file mode 100644index 00000000000..245192e25a9--- /dev/null+++ b/lucene/core/src/test/org/apache/lucene/search/TestFilterCollector.java@@ -0,0 +1,28 @@+/*+ * Licensed to the Apache Software Foundation (ASF) under one or more+ * contributor license agreements. See the NOTICE file distributed with+ * this work for additional information regarding copyright ownership.+ * The ASF licenses this file to You under the Apache License, Version 2.0+ * (the "License"); you may not use this file except in compliance with+ * the License. You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */+package org.apache.lucene.search;++import org.apache.lucene.tests.util.LuceneTestCase;+import org.junit.Test;++public class TestFilterCollector extends LuceneTestCase {++ @Test+ public void testDeclaredMethodsOverridden() {+ assertDelegatorOverridesAllRequiredMethods(FilterCollector.class);+ }+}diff --git a/lucene/core/src/test/org/apache/lucene/search/TestFilterScorable.java b/lucene/core/src/test/org/apache/lucene/search/TestFilterScorable.javanew file mode 100644index 00000000000..b8af4d04cd5--- /dev/null+++ b/lucene/core/src/test/org/apache/lucene/search/TestFilterScorable.java@@ -0,0 +1,28 @@+/*+ * Licensed to the Apache Software Foundation (ASF) under one or more+ * contributor license agreements. See the NOTICE file distributed with+ * this work for additional information regarding copyright ownership.+ * The ASF licenses this file to You under the Apache License, Version 2.0+ * (the "License"); you may not use this file except in compliance with+ * the License. You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */+package org.apache.lucene.search;++import org.apache.lucene.tests.util.LuceneTestCase;+import org.junit.Test;++public class TestFilterScorable extends LuceneTestCase {++ @Test+ public void testDeclaredMethodsOverridden() {+ assertDelegatorOverridesAllRequiredMethods(FilterScorable.class);+ }+}diff --git a/lucene/core/src/test/org/apache/lucene/search/TestFilterScorer.java b/lucene/core/src/test/org/apache/lucene/search/TestFilterScorer.javanew file mode 100644index 00000000000..9575cd36a78--- /dev/null+++ b/lucene/core/src/test/org/apache/lucene/search/TestFilterScorer.java@@ -0,0 +1,28 @@+/*+ * Licensed to the Apache Software Foundation (ASF) under one or more+ * contributor license agreements. See the NOTICE file distributed with+ * this work for additional information regarding copyright ownership.+ * The ASF licenses this file to You under the Apache License, Version 2.0+ * (the "License"); you may not use this file except in compliance with+ * the License. You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */+package org.apache.lucene.search;++import org.apache.lucene.tests.util.LuceneTestCase;+import org.junit.Test;++public class TestFilterScorer extends LuceneTestCase {++ @Test+ public void testDeclaredMethodsOverridden() {+ assertDelegatorOverridesAllRequiredMethods(FilterScorer.class);+ }+}diff --git a/lucene/core/src/test/org/apache/lucene/search/TestFilterWeight.java b/lucene/core/src/test/org/apache/lucene/search/TestFilterWeight.javaindex a6177ab892b..d1778d7a51a 100644--- a/lucene/core/src/test/org/apache/lucene/search/TestFilterWeight.java+++ b/lucene/core/src/test/org/apache/lucene/search/TestFilterWeight.java@@ -16,59 +16,13 @@*/package org.apache.lucene.search;-import java.lang.reflect.Method;-import java.lang.reflect.Modifier;-import java.util.Arrays;import org.apache.lucene.tests.util.LuceneTestCase;import org.junit.Test;public class TestFilterWeight extends LuceneTestCase {@Test- public void testDeclaredMethodsOverridden() throws Exception {- final Class<?> subClass = FilterWeight.class;- implTestDeclaredMethodsOverridden(subClass.getSuperclass(), subClass);- }-- private void implTestDeclaredMethodsOverridden(Class<?> superClass, Class<?> subClass)- throws Exception {- for (final Method superClassMethod : superClass.getDeclaredMethods()) {- final int modifiers = superClassMethod.getModifiers();- if (Modifier.isFinal(modifiers)) continue;- if (Modifier.isStatic(modifiers)) continue;- if (Arrays.asList("bulkScorer", "count").contains(superClassMethod.getName())) {- try {- final Method subClassMethod =- subClass.getDeclaredMethod(- superClassMethod.getName(), superClassMethod.getParameterTypes());- fail(- subClass- + " must not override\n'"- + superClassMethod- + "'"- + " but it does override\n'"- + subClassMethod- + "'");- } catch (NoSuchMethodException _) {- /* FilterWeight must not override the bulkScorer method- * since as of July 2016 not all deriving classes use the- * {code}return in.bulkScorer(content);{code}- * implementation that FilterWeight.bulkScorer would use.- */- continue;- }- }- try {- final Method subClassMethod =- subClass.getDeclaredMethod(- superClassMethod.getName(), superClassMethod.getParameterTypes());- assertEquals(- "getReturnType() difference",- superClassMethod.getReturnType(),- subClassMethod.getReturnType());- } catch (NoSuchMethodException _) {- fail(subClass + " needs to override '" + superClassMethod + "'");- }- }+ public void testDeclaredMethodsOverridden() {+ assertDelegatorOverridesAllRequiredMethods(FilterWeight.class);}}diff --git a/lucene/core/src/test/org/apache/lucene/store/TestFilterDirectory.java b/lucene/core/src/test/org/apache/lucene/store/TestFilterDirectory.javaindex 6109115c885..d2767d5d62a 100644--- a/lucene/core/src/test/org/apache/lucene/store/TestFilterDirectory.java+++ b/lucene/core/src/test/org/apache/lucene/store/TestFilterDirectory.java@@ -19,7 +19,6 @@ package org.apache.lucene.store;import java.io.IOException;import java.lang.reflect.Method;import java.nio.file.Path;-import java.util.HashSet;import java.util.Set;import org.apache.lucene.tests.store.BaseDirectoryTestCase;import org.junit.Test;@@ -35,16 +34,13 @@ public class TestFilterDirectory extends BaseDirectoryTestCase {public void testOverrides() throws Exception {// verify that all methods of Directory are overridden by FilterDirectory,// except those under the 'exclude' list- Set<Method> exclude = new HashSet<>();- exclude.add(- Directory.class.getMethod(- "copyFrom", Directory.class, String.class, String.class, IOContext.class));- exclude.add(Directory.class.getMethod("openChecksumInput", String.class));- for (Method m : FilterDirectory.class.getMethods()) {- if (m.getDeclaringClass() == Directory.class) {- assertTrue("method " + m.getName() + " not overridden!", exclude.contains(m));- }- }+ Set<Method> exclude =+ Set.of(+ Directory.class.getMethod(+ "copyFrom", Directory.class, String.class, String.class, IOContext.class),+ Directory.class.getMethod("openChecksumInput", String.class));++ assertDelegatorOverridesAllRequiredMethods(FilterDirectory.class, exclude);}public void testUnwrap() throws IOException {diff --git a/lucene/core/src/test/org/apache/lucene/store/TestFilterIndexOutput.java b/lucene/core/src/test/org/apache/lucene/store/TestFilterIndexOutput.javaindex 2abdf754d59..65567878356 100644--- a/lucene/core/src/test/org/apache/lucene/store/TestFilterIndexOutput.java+++ b/lucene/core/src/test/org/apache/lucene/store/TestFilterIndexOutput.java@@ -17,8 +17,6 @@package org.apache.lucene.store;import java.io.IOException;-import java.lang.reflect.Method;-import java.lang.reflect.Modifier;import org.junit.Test;public class TestFilterIndexOutput extends BaseDataOutputTestCase<FilterIndexOutput> {@@ -38,20 +36,7 @@ public class TestFilterIndexOutput extends BaseDataOutputTestCase<FilterIndexOut@Testpublic void testOverrides() {- for (Method m : FilterIndexOutput.class.getMethods()) {- if (m.getDeclaringClass() == FilterIndexOutput.class) {- // verify that only abstract methods are overridden- Method indexOutputMethod;- try {- indexOutputMethod = IndexOutput.class.getMethod(m.getName(), m.getParameterTypes());- assertTrue(- "Non-abstract method " + m.getName() + " is overridden",- Modifier.isAbstract(indexOutputMethod.getModifiers()));- } catch (Exception e) {- assertTrue(e instanceof NoSuchMethodException);- }- }- }+ assertDelegatorOverridesAllRequiredMethods(FilterIndexOutput.class);}public void testUnwrap() throws IOException {diff --git a/lucene/join/src/java/org/apache/lucene/search/join/GlobalOrdinalsWithScoreQuery.java b/lucene/join/src/java/org/apache/lucene/search/join/GlobalOrdinalsWithScoreQuery.javaindex 9b118d3cf2e..f5ddd156196 100644--- a/lucene/join/src/java/org/apache/lucene/search/join/GlobalOrdinalsWithScoreQuery.java+++ b/lucene/join/src/java/org/apache/lucene/search/join/GlobalOrdinalsWithScoreQuery.java@@ -240,6 +240,12 @@ final class GlobalOrdinalsWithScoreQuery extends Query implements Accountable {// the memory used by the cachereturn false;}++ @Override+ public int count(LeafReaderContext context) {+ // Signal we can't compute the exact count in sublinear time.+ return -1;+ }}static final class OrdinalMapScorer extends BaseGlobalOrdinalScorer {diff --git a/lucene/join/src/java/org/apache/lucene/search/join/ToChildBlockJoinQuery.java b/lucene/join/src/java/org/apache/lucene/search/join/ToChildBlockJoinQuery.javaindex 6fd0dc99e6d..deee1805dc1 100644--- a/lucene/join/src/java/org/apache/lucene/search/join/ToChildBlockJoinQuery.java+++ b/lucene/join/src/java/org/apache/lucene/search/join/ToChildBlockJoinQuery.java@@ -135,6 +135,12 @@ public class ToChildBlockJoinQuery extends Query {}return Explanation.noMatch("Not a match");}++ @Override+ public int count(LeafReaderContext context) {+ // Signal we can't compute the exact count in sublinear time.+ return -1;+ }}static class ToChildBlockJoinScorer extends Scorer {diff --git a/lucene/queries/src/java/org/apache/lucene/queries/spans/FilterSpans.java b/lucene/queries/src/java/org/apache/lucene/queries/spans/FilterSpans.javaindex c29fe79ca1f..1546831f94a 100644--- a/lucene/queries/src/java/org/apache/lucene/queries/spans/FilterSpans.java+++ b/lucene/queries/src/java/org/apache/lucene/queries/spans/FilterSpans.java@@ -19,6 +19,7 @@ package org.apache.lucene.queries.spans;import java.io.IOException;import java.util.Objects;import org.apache.lucene.search.TwoPhaseIterator;+import org.apache.lucene.util.FixedBitSet;/*** A {@link Spans} implementation wrapping another spans instance, allowing to filter spans matches@@ -123,9 +124,19 @@ public abstract class FilterSpans extends Spans {return in.cost();}+ @Override+ public void intoBitSet(int upTo, FixedBitSet bitSet, int offset) throws IOException {+ in.intoBitSet(upTo, bitSet, offset);+ }++ @Override+ public int docIDRunEnd() throws IOException {+ return in.docIDRunEnd();+ }+@Overridepublic String toString() {- return "Filter(" + in.toString() + ")";+ return "Filter(" + in + ")";}@Overridediff --git a/lucene/queries/src/test/org/apache/lucene/queries/spans/TestFilterSpans.java b/lucene/queries/src/test/org/apache/lucene/queries/spans/TestFilterSpans.javaindex 565885777a6..25a97534045 100644--- a/lucene/queries/src/test/org/apache/lucene/queries/spans/TestFilterSpans.java+++ b/lucene/queries/src/test/org/apache/lucene/queries/spans/TestFilterSpans.java@@ -16,19 +16,13 @@*/package org.apache.lucene.queries.spans;-import java.lang.reflect.Method;import org.apache.lucene.tests.util.LuceneTestCase;import org.junit.Test;public class TestFilterSpans extends LuceneTestCase {@Test- public void testOverrides() throws Exception {- // verify that all methods of Spans are overridden by FilterSpans,- for (Method m : FilterSpans.class.getMethods()) {- if (m.getDeclaringClass() == Spans.class) {- fail("method " + m.getName() + " not overridden!");- }- }+ public void testOverrides() {+ assertDelegatorOverridesAllRequiredMethods(FilterSpans.class);}}diff --git a/lucene/test-framework/src/java/org/apache/lucene/tests/index/AllDeletedFilterReader.java b/lucene/test-framework/src/java/org/apache/lucene/tests/index/AllDeletedFilterReader.javaindex 12c64290622..19326636676 100644--- a/lucene/test-framework/src/java/org/apache/lucene/tests/index/AllDeletedFilterReader.java+++ b/lucene/test-framework/src/java/org/apache/lucene/tests/index/AllDeletedFilterReader.java@@ -27,7 +27,6 @@ public class AllDeletedFilterReader extends FilterLeafReader {public AllDeletedFilterReader(LeafReader in) {super(in);liveDocs = new Bits.MatchNoBits(in.maxDoc());- assert maxDoc() == 0 || hasDeletions();}@Override@@ -40,6 +39,11 @@ public class AllDeletedFilterReader extends FilterLeafReader {return 0;}+ @Override+ public boolean hasDeletions() {+ return maxDoc() > 0;+ }+@Overridepublic CacheHelper getCoreCacheHelper() {return in.getCoreCacheHelper();diff --git a/lucene/test-framework/src/java/org/apache/lucene/tests/search/RandomApproximationQuery.java b/lucene/test-framework/src/java/org/apache/lucene/tests/search/RandomApproximationQuery.javaindex 92f247e3754..c03e8883e48 100644--- a/lucene/test-framework/src/java/org/apache/lucene/tests/search/RandomApproximationQuery.java+++ b/lucene/test-framework/src/java/org/apache/lucene/tests/search/RandomApproximationQuery.java@@ -100,6 +100,12 @@ public class RandomApproximationQuery extends Query {}return new DefaultScorerSupplier(scorer);}++ @Override+ public int count(LeafReaderContext context) throws IOException {+ // Signal we can't compute the exact count in sublinear time.+ return -1;+ }}private static class RandomApproximationScorer extends Scorer {diff --git a/lucene/test-framework/src/java/org/apache/lucene/tests/store/CorruptingIndexOutput.java b/lucene/test-framework/src/java/org/apache/lucene/tests/store/CorruptingIndexOutput.javaindex c3c3ebf4313..765c7489897 100644--- a/lucene/test-framework/src/java/org/apache/lucene/tests/store/CorruptingIndexOutput.java+++ b/lucene/test-framework/src/java/org/apache/lucene/tests/store/CorruptingIndexOutput.java@@ -36,11 +36,6 @@ public class CorruptingIndexOutput extends FilterIndexOutput {this.byteToCorrupt = byteToCorrupt;}- @Override- public String getName() {- return out.getName();- }-@Overridepublic void close() throws IOException {if (closed == false) {diff --git a/lucene/test-framework/src/java/org/apache/lucene/tests/util/LuceneTestCase.java b/lucene/test-framework/src/java/org/apache/lucene/tests/util/LuceneTestCase.javaindex 8d1a2b9aa20..af6fad301cf 100644--- a/lucene/test-framework/src/java/org/apache/lucene/tests/util/LuceneTestCase.java+++ b/lucene/test-framework/src/java/org/apache/lucene/tests/util/LuceneTestCase.java@@ -64,6 +64,7 @@ import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;import java.lang.reflect.Constructor;import java.lang.reflect.Method;+import java.lang.reflect.Modifier;import java.net.URISyntaxException;import java.nio.charset.StandardCharsets;import java.nio.file.Files;@@ -73,17 +74,20 @@ import java.nio.file.Paths;import java.text.Collator;import java.util.ArrayList;import java.util.Arrays;+import java.util.Collection;import java.util.Collections;import java.util.Comparator;import java.util.HashMap;import java.util.HashSet;import java.util.Iterator;import java.util.LinkedHashMap;+import java.util.LinkedHashSet;import java.util.List;import java.util.Locale;import java.util.Map;import java.util.Random;import java.util.Set;+import java.util.StringJoiner;import java.util.TimeZone;import java.util.TreeSet;import java.util.concurrent.Executor;@@ -3343,4 +3347,154 @@ public abstract class LuceneTestCase extends Assert {return intraMergeExecutor;}}++ /**+ * Verifies that the given delegator class overrides all methods that it is expected to delegate.+ *+ * @param delegator the decorating class whose overrides must be verified+ * @see #assertDelegatorOverridesAllRequiredMethods(Class, Set)+ */+ protected static void assertDelegatorOverridesAllRequiredMethods(Class<?> delegator) {+ assertDelegatorOverridesAllRequiredMethods(delegator, Set.of());+ }++ /**+ * Verifies that the given delegator class overrides all methods that it is expected to delegate.+ *+ * @param delegator the decorating class whose overrides must be verified+ * @param exclude the methods that should be ignored by the checks+ */+ protected static void assertDelegatorOverridesAllRequiredMethods(+ Class<?> delegator, Set<Method> exclude) {+ Set<String> missingOverrides = new LinkedHashSet<>();+ for (Method m : getMethods(delegator)) {+ if (!exclude.contains(m)+ && m.getDeclaringClass() != delegator+ && requiresDelegatorOverride(m)) {+ missingOverrides.add(methodSignature(m));+ }+ }+ if (!missingOverrides.isEmpty()) {+ fail(+ String.format(+ Locale.US,+ "The decorating class %s must override the following methods: %s",+ delegator.getSimpleName(),+ String.join(", ", missingOverrides)));+ }+ }++ /**+ * Returns the effective set of methods visible on the given class, filtering out overridden+ * declarations introduced by the reflection changes in Java 21+.+ *+ * @param clazz the class whose effective methods should be enumerated+ * @return a collection of methods where each signature appears at most once, resolved to the most+ * specific declaring class+ */+ private static Collection<Method> getMethods(Class<?> clazz) {+ Map<MethodKey, Method> methods = new HashMap<>();+ collectDeclaredMethods(clazz, methods);+ return methods.values();+ }++ /**+ * Recursively collects declared methods from the class hierarchy into the provided map, keeping+ * only the most specific declaration for each method signature.+ *+ * @param clazz the class being inspected+ * @param methods the map collecting signature-to-method entries+ */+ private static void collectDeclaredMethods(Class<?> clazz, Map<MethodKey, Method> methods) {++ for (Class<?> i : clazz.getInterfaces()) {+ collectDeclaredMethods(i, methods);+ }++ Class<?> superclass = clazz.getSuperclass();+ if (superclass != null && superclass != Object.class) {+ collectDeclaredMethods(superclass, methods);+ }++ for (Method m : clazz.getDeclaredMethods()) {+ MethodKey key = new MethodKey(m);+ Method previous = methods.get(key);+ if (previous != null && previous.getDeclaringClass() == clazz) {+ Class<?> previousReturnType = previous.getReturnType();+ Class<?> newReturnType = m.getReturnType();+ if (previousReturnType.isAssignableFrom(newReturnType)) {+ methods.put(key, m);+ }+ } else {+ methods.put(key, m);+ }+ }+ }++ /**+ * Signature key: method name + parameter types. We use a List for parameter types so that record+ * equality works with element-wise comparison.+ */+ private record MethodKey(String name, List<Class<?>> parameterTypes) {+ MethodKey(Method m) {+ this(m.getName(), List.of(m.getParameterTypes()));+ }+ }++ private static String methodSignature(Method m) {+ String name =+ String.format(+ Locale.US,+ "%s %s.%s",+ m.getReturnType().getSimpleName(),+ m.getDeclaringClass().getSimpleName(),+ m.getName());++ StringJoiner sj = new StringJoiner(", ", name + "(", ")");+ for (Class<?> parameterType : m.getParameterTypes()) {+ sj.add(parameterType.getSimpleName());+ }+ return sj.toString();+ }++ /**+ * Determines whether the given method must be overridden by a delegator class.+ *+ * <p>A method is considered required to override if it is:+ *+ * <ul>+ * <li>public,+ * <li>non-static,+ * <li>non-final,+ * <li>not declared on {@link Object},+ * <li>non abstract+ * </ul>+ *+ * @param m the method to inspect+ * @return {@code true} if the delegator must override this method+ */+ private static boolean requiresDelegatorOverride(Method m) {++ int modifier = m.getModifiers();++ // We want to ignore Objects methods as a base delegator cannot override them correctly anyway.+ return !isObjectMethod(m)+ && Modifier.isPublic(modifier)+ && !Modifier.isStatic(modifier)+ && !Modifier.isFinal(modifier)+ // We keep only the last method implementation, therefore, if it is abstract java will force+ // the implementation to define it.+ && !Modifier.isAbstract(modifier);+ }++ private static boolean isObjectMethod(Method m) {+ try {+ Object.class.getMethod(m.getName(), m.getParameterTypes());+ return true;+ } catch (+ @SuppressWarnings("unused")+ NoSuchMethodException ignored) {+ return false;+ }+ }}