Index: src/java/org/apache/lucene/analysis/Analyzer.java =================================================================== --- src/java/org/apache/lucene/analysis/Analyzer.java (revision 895342) +++ src/java/org/apache/lucene/analysis/Analyzer.java (working copy) @@ -20,9 +20,9 @@ import java.io.Reader; import java.io.IOException; import java.io.Closeable; -import java.lang.reflect.Method; import org.apache.lucene.util.CloseableThreadLocal; +import org.apache.lucene.util.OverrideableMethod; import org.apache.lucene.store.AlreadyClosedException; import org.apache.lucene.document.Fieldable; @@ -84,22 +84,18 @@ } } - /** @deprecated */ + private static final OverrideableMethod tokenStreamMethod = + new OverrideableMethod(Analyzer.class, "tokenStream", String.class, Reader.class); + private static final OverrideableMethod reusableTokenStreamMethod = + new OverrideableMethod(Analyzer.class, "reusableTokenStream", String.class, Reader.class); + @Deprecated - protected boolean overridesTokenStreamMethod = false; + protected final boolean overridesTokenStreamMethod = + tokenStreamMethod.getOverrideDistance(this.getClass()) > reusableTokenStreamMethod.getOverrideDistance(this.getClass()); - /** @deprecated This is only present to preserve - * back-compat of classes that subclass a core analyzer - * and override tokenStream but not reusableTokenStream */ + /** @deprecated This is a no-op since Lucene 3.1. */ @Deprecated protected void setOverridesTokenStreamMethod(Class baseClass) { - try { - Method m = this.getClass().getMethod("tokenStream", String.class, Reader.class); - overridesTokenStreamMethod = m.getDeclaringClass() != baseClass; - } catch (NoSuchMethodException nsme) { - // cannot happen, as baseClass is subclass of Analyzer through generics - overridesTokenStreamMethod = false; - } } Index: src/java/org/apache/lucene/analysis/KeywordAnalyzer.java =================================================================== --- src/java/org/apache/lucene/analysis/KeywordAnalyzer.java (revision 895342) +++ src/java/org/apache/lucene/analysis/KeywordAnalyzer.java (working copy) @@ -26,7 +26,6 @@ */ public class KeywordAnalyzer extends Analyzer { public KeywordAnalyzer() { - setOverridesTokenStreamMethod(KeywordAnalyzer.class); } @Override public TokenStream tokenStream(String fieldName, Index: src/java/org/apache/lucene/analysis/PerFieldAnalyzerWrapper.java =================================================================== --- src/java/org/apache/lucene/analysis/PerFieldAnalyzerWrapper.java (revision 895342) +++ src/java/org/apache/lucene/analysis/PerFieldAnalyzerWrapper.java (working copy) @@ -72,7 +72,6 @@ if (fieldAnalyzers != null) { analyzerMap.putAll(fieldAnalyzers); } - setOverridesTokenStreamMethod(PerFieldAnalyzerWrapper.class); } Index: src/java/org/apache/lucene/analysis/standard/StandardAnalyzer.java =================================================================== --- src/java/org/apache/lucene/analysis/standard/StandardAnalyzer.java (revision 895342) +++ src/java/org/apache/lucene/analysis/standard/StandardAnalyzer.java (working copy) @@ -71,7 +71,6 @@ * @param stopWords stop words */ public StandardAnalyzer(Version matchVersion, Set stopWords) { stopSet = stopWords; - setOverridesTokenStreamMethod(StandardAnalyzer.class); replaceInvalidAcronym = matchVersion.onOrAfter(Version.LUCENE_24); this.matchVersion = matchVersion; } Index: src/java/org/apache/lucene/analysis/StopwordAnalyzerBase.java =================================================================== --- src/java/org/apache/lucene/analysis/StopwordAnalyzerBase.java (revision 895342) +++ src/java/org/apache/lucene/analysis/StopwordAnalyzerBase.java (working copy) @@ -58,11 +58,6 @@ * the analyzer's stopword set */ protected StopwordAnalyzerBase(final Version version, final Set stopwords) { - /* - * no need to call - * setOverridesTokenStreamMethod(StopwordAnalyzerBase.class); here, both - * tokenStream methods are final in this class. - */ matchVersion = version; // analyzers should use char array set for stopwords! this.stopwords = stopwords == null ? CharArraySet.EMPTY_SET : CharArraySet Index: src/java/org/apache/lucene/util/OverrideableMethod.java =================================================================== --- src/java/org/apache/lucene/util/OverrideableMethod.java (revision 0) +++ src/java/org/apache/lucene/util/OverrideableMethod.java (revision 0) @@ -0,0 +1,100 @@ +package org.apache.lucene.util; + +/** + * 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. + */ + +import java.util.IdentityHashMap; + +/** + * A utility for keeping backwards compatibility on previously abstract methods + * (or similar replacements). + *

Before the replacement method can be made abstract, the old method must kept deprecated. + * If somebody still overrides the deprecated method in a non-final class, + * you must keep track, of this and maybe delegate to the old method in the new one. + * The cost of reflection is minimized by the follwoing usage of this class:

+ *

Define static final fields in the base class, where the old and new method are declared:

+ *
+ *  static final OverrideableMethod<BaseClass> newMethod =
+ *   new OverrideableMethod<BaseClass>(BaseClass.class, "newName", parameters...);
+ *  static finalOverrideableMethod<BaseClass> oldMethod =
+ *   new OverrideableMethod<BaseClass>(BaseClass.class, "oldName", parameters...);
+ * 
+ *

To detect if e.g. the old method was overridden by a more far subclass, use a non-static + * field:

+ *
+ *  final boolean isDeprecatedMethodOverridden =
+ *   oldMethod.getOverrideDistance(this.getClass()) > newMethod.getOverrideDistance(this.getClass());
+ * 
+ *

{@link #getOverrideDistance} returns the distance of the subclass that overrides this method. + * The one with the larger distance should be used preferable. + * This way also more complicated method rename constellations can be handled + * (think of 2.9 {@code TokenStream} deprecations).

+ */ +public final class OverrideableMethod { + + private final Class baseClass; + private final String method; + private final Class[] parameters; + + private final IdentityHashMap, Integer> cache = + new IdentityHashMap, Integer>(); + + public OverrideableMethod(Class baseClass, String method, Class... parameters) { + this.baseClass = baseClass; + this.method = method; + this.parameters = parameters; + try { + baseClass.getDeclaredMethod(method, parameters); + } catch (NoSuchMethodException nsme) { + throw new IllegalArgumentException(baseClass.getName() + " has no such method: "+nsme.getMessage()); + } + } + + /** + * Returns, if the given class overrides the method this instance specifies. + * @return 0 iff not overridden, else the distance to the base class + */ + public synchronized int getOverrideDistance(final Class clazz) { + Integer distance = cache.get(clazz); + if (distance == null) { + cache.put(clazz, distance = Integer.valueOf(getDistance(clazz))); + } + return distance.intValue(); + } + + private int getDistance(final Class thisClass) { + if (!baseClass.isAssignableFrom(thisClass)) + throw new IllegalArgumentException(thisClass.getName() + " is not a subclass of " + baseClass.getName()); + boolean overridden = false; + int distance = 0; + for (Class clazz = thisClass; clazz != baseClass && clazz != null; clazz = clazz.getSuperclass()) { + // lookup method, if success mark as overridden + if (!overridden) { + try { + clazz.getDeclaredMethod(method, parameters); + overridden = true; + } catch (NoSuchMethodException nsme) { + } + } + + // increment distance if overridden + if (overridden) distance++; + } + return distance; + } + +} Property changes on: src\java\org\apache\lucene\util\OverrideableMethod.java ___________________________________________________________________ Added: svn:keywords + Date Author Id Revision HeadURL Added: svn:eol-style + native Index: src/test/org/apache/lucene/util/TestOverrideableMethod.java =================================================================== --- src/test/org/apache/lucene/util/TestOverrideableMethod.java (revision 0) +++ src/test/org/apache/lucene/util/TestOverrideableMethod.java (revision 0) @@ -0,0 +1,62 @@ +package org.apache.lucene.util; + +/** + * 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. + */ + +public class TestOverrideableMethod extends LuceneTestCase { + + public static final OverrideableMethod method = + new OverrideableMethod(TestOverrideableMethod.class, "method", String.class); + + public void method(String test) {} + + static class TestClass1 extends TestOverrideableMethod { + @Override + public void method(String test) {} + } + + static class TestClass2 extends TestClass1 { + } + + static class TestClass3 extends TestClass2 { + @Override + public void method(String test) {} + } + + static class TestClass4 extends TestOverrideableMethod { + } + + static class TestClass5 extends TestClass4 { + } + + public void test() { + assertEquals(0, method.getOverrideDistance(this.getClass())); + assertEquals(1, method.getOverrideDistance(TestClass1.class)); + assertEquals(1, method.getOverrideDistance(TestClass2.class)); + assertEquals(3, method.getOverrideDistance(TestClass3.class)); + assertEquals(0, method.getOverrideDistance(TestClass4.class)); + assertEquals(0, method.getOverrideDistance(TestClass5.class)); + try { + // cast to Class to remove generics: + @SuppressWarnings("unchecked") int dist = method.getOverrideDistance((Class) LuceneTestCase.class); + fail("LuceneTestCase is not a subclass and can never override method(String)"); + } catch (IllegalArgumentException arg) { + // pass + } + } + +} Property changes on: src\test\org\apache\lucene\util\TestOverrideableMethod.java ___________________________________________________________________ Added: svn:keywords + Date Author Id Revision HeadURL Added: svn:eol-style + native