|
Lucene example source code file (VirtualMethod.java)
The Lucene VirtualMethod.java source codepackage 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.lang.reflect.Method; import java.util.Collections; import java.util.HashSet; import java.util.WeakHashMap; import java.util.Set; /** * A utility for keeping backwards compatibility on previously abstract methods * (or similar replacements). * <p>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 subclass. * The cost of reflection is minimized by the following usage of this class:</p> * <p>Define static final fields in the base class ({@code BaseClass}), * where the old and new method are declared:</p> * <pre> * static final VirtualMethod<BaseClass> newMethod = * new VirtualMethod<BaseClass>(BaseClass.class, "newName", parameters...); * static final VirtualMethod<BaseClass> oldMethod = * new VirtualMethod<BaseClass>(BaseClass.class, "oldName", parameters...); * </pre> * <p>This enforces the singleton status of these objects, as the maintenance of the cache would be too costly else. * If you try to create a second instance of for the same method/{@code baseClass} combination, an exception is thrown. * <p>To detect if e.g. the old method was overridden by a more far subclass on the inheritance path to the current * instance's class, use a <strong>non-static field: * <pre> * final boolean isDeprecatedMethodOverridden = * oldMethod.getImplementationDistance(this.getClass()) > newMethod.getImplementationDistance(this.getClass()); * * <em>// alternatively (more readable): * final boolean isDeprecatedMethodOverridden = * VirtualMethod.compareImplementationDistance(this.getClass(), oldMethod, newMethod) > 0 * </pre> * <p>{@link #getImplementationDistance} 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 scenarios can be handled * (think of 2.9 {@code TokenStream} deprecations).</p> * * @lucene.internal */ public final class VirtualMethod<C> { private static final Set<Method> singletonSet = Collections.synchronizedSet(new HashSet Other Lucene examples (source code examples)Here is a short list of links related to this Lucene VirtualMethod.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.