Class VirtualMethod
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 subclass. The cost of reflection is minimized by the following usage of this class:
Define static final fields in the base class (BaseClass
),
where the old and new method are declared:
static final VirtualMethod<BaseClass> newMethod = new VirtualMethod<BaseClass>(BaseClass.class, "newName", parameters...); static final VirtualMethod<BaseClass> oldMethod = new VirtualMethod<BaseClass>(BaseClass.class, "oldName", parameters...);
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/baseClass
combination, an exception is thrown.
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 non-static field:
final boolean isDeprecatedMethodOverridden = oldMethod.getImplementationDistance(this.getClass()) > newMethod.getImplementationDistance(this.getClass()); // alternatively (more readable): final boolean isDeprecatedMethodOverridden = VirtualMethod.compareImplementationDistance(this.getClass(), oldMethod, newMethod) > 0
GetImplementationDistance(Type) 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
@lucene.internal
Inheritance
Assembly: Lucene.Net.TestFramework.dll
Syntax
public sealed class VirtualMethod : object
Constructors
Name | Description |
---|---|
VirtualMethod(Type, String, Type[]) | Creates a new instance for the given |
Methods
Name | Description |
---|---|
CompareImplementationDistance(Type, VirtualMethod, VirtualMethod) | |
GetImplementationDistance(Type) | Returns the distance from the |
IsOverriddenAsOf(Type) | Returns, if this method is overridden/implemented in the inheritance path between
You can use this method to detect if a method that should normally be final was overridden by the given instance's class. |