Show / Hide Table of Contents

    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-sealed 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 readonly fields in the base class (BaseClass), where the old and new method are declared:

    internal static readonly VirtualMethod newMethod =
        new VirtualMethod(typeof(BaseClass), "newName", parameters...);
    internal static readonly VirtualMethod oldMethod =
        new VirtualMethod(typeof(BaseClass), "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:

     bool isDeprecatedMethodOverridden =
         oldMethod.GetImplementationDistance(this.GetType()) > newMethod.GetImplementationDistance(this.GetType());
    
    // alternatively (more readable):
    bool isDeprecatedMethodOverridden =
        VirtualMethod.CompareImplementationDistance(this.GetType(), 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 TokenStream deprecations).

    This is a Lucene.NET INTERNAL API, use at your own risk
    Inheritance
    System.Object
    VirtualMethod
    Namespace: Lucene.Net.Util
    Assembly: Lucene.Net.dll
    Syntax
    public sealed class VirtualMethod : object

    Constructors

    | Improve this Doc View Source

    VirtualMethod(Type, String, Type[])

    Creates a new instance for the given baseClass and method declaration.

    Declaration
    public VirtualMethod(Type baseClass, string method, params Type[] parameters)
    Parameters
    Type Name Description
    Type baseClass
    System.String method
    Type[] parameters

    Methods

    | Improve this Doc View Source

    CompareImplementationDistance(Type, VirtualMethod, VirtualMethod)

    Utility method that compares the implementation/override distance of two methods.

    Declaration
    public static int CompareImplementationDistance(Type clazz, VirtualMethod m1, VirtualMethod m2)
    Parameters
    Type Name Description
    Type clazz
    VirtualMethod m1
    VirtualMethod m2
    Returns
    Type Description
    System.Int32
    • > 1, iff m1 is overridden/implemented in a subclass of the class overriding/declaring m2
    • < 1, iff m2 is overridden in a subclass of the class overriding/declaring m1
    • 0, iff both methods are overridden in the same class (or are not overridden at all)
    | Improve this Doc View Source

    GetImplementationDistance(Type)

    Returns the distance from the baseClass in which this method is overridden/implemented in the inheritance path between baseClass and the given subclass subclazz.

    Declaration
    public int GetImplementationDistance(Type subclazz)
    Parameters
    Type Name Description
    Type subclazz
    Returns
    Type Description
    System.Int32

    0 if and only if not overridden, else the distance to the base class.

    | Improve this Doc View Source

    IsOverriddenAsOf(Type)

    Returns, if this method is overridden/implemented in the inheritance path between baseClass and the given subclass subclazz.

    You can use this method to detect if a method that should normally be final was overridden by the given instance's class.

    Declaration
    public bool IsOverriddenAsOf(Type subclazz)
    Parameters
    Type Name Description
    Type subclazz
    Returns
    Type Description
    System.Boolean

    false if and only if not overridden.

    • Improve this Doc
    • View Source
    Back to top Copyright © 2020 Licensed to the Apache Software Foundation (ASF)