Class MethodMap


  • public class MethodMap
    extends java.lang.Object
    NOTE: This class was copied from plexus-utils, to allow this library to stand completely self-contained.
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  MethodMap.AmbiguousException
      simple distinguishable exception, used when we run across ambiguous overloading
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static int INCOMPARABLE  
      private static int LESS_SPECIFIC  
      (package private) java.util.Map<java.lang.String,​java.util.List<java.lang.reflect.Method>> methodByNameMap
      Keep track of all methods with the same name.
      private static int MORE_SPECIFIC  
    • Constructor Summary

      Constructors 
      Constructor Description
      MethodMap()  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void add​(java.lang.reflect.Method method)
      Add a method to a list of methods by name.
      java.lang.reflect.Method find​(java.lang.String methodName, java.lang.Object[] args)
      Find a method.
      java.util.List<java.lang.reflect.Method> get​(java.lang.String key)
      Return a list of methods with the same name.
      private static java.util.LinkedList<java.lang.reflect.Method> getApplicables​(java.util.List<java.lang.reflect.Method> methods, java.lang.Class<?>[] classes)
      Returns all methods that are applicable to actual argument types.
      private static java.lang.reflect.Method getMostSpecific​(java.util.List<java.lang.reflect.Method> methods, java.lang.Class<?>[] classes)  
      private static boolean isApplicable​(java.lang.reflect.Method method, java.lang.Class<?>[] classes)
      Returns true if the supplied method is applicable to actual argument types.
      private static boolean isMethodInvocationConvertible​(java.lang.Class<?> formal, java.lang.Class<?> actual)
      Determines whether a type represented by a class object is convertible to another type represented by a class object using a method invocation conversion, treating object types of primitive types as if they were primitive types (that is, a Boolean actual parameter type matches boolean primitive formal type).
      private static boolean isStrictMethodInvocationConvertible​(java.lang.Class<?> formal, java.lang.Class<?> actual)
      Determines whether a type represented by a class object is convertible to another type represented by a class object using a method invocation conversion, without matching object and primitive types.
      private static int moreSpecific​(java.lang.Class<?>[] c1, java.lang.Class<?>[] c2)
      Determines which method signature (represented by a class array) is more specific.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • methodByNameMap

        java.util.Map<java.lang.String,​java.util.List<java.lang.reflect.Method>> methodByNameMap
        Keep track of all methods with the same name.
    • Constructor Detail

      • MethodMap

        public MethodMap()
    • Method Detail

      • add

        public void add​(java.lang.reflect.Method method)
        Add a method to a list of methods by name. For a particular class we are keeping track of all the methods with the same name.
        Parameters:
        method - Method
      • get

        public java.util.List<java.lang.reflect.Method> get​(java.lang.String key)
        Return a list of methods with the same name.
        Parameters:
        key - the key
        Returns:
        list of methods
      • find

        public java.lang.reflect.Method find​(java.lang.String methodName,
                                             java.lang.Object[] args)
                                      throws MethodMap.AmbiguousException

        Find a method. Attempts to find the most specific applicable method using the algorithm described in the JLS section 15.12.2 (with the exception that it can't distinguish a primitive type argument from an object type argument, since in reflection primitive type arguments are represented by their object counterparts, so for an argument of type (say) java.lang.Integer, it will not be able to decide between a method that takes int and a method that takes java.lang.Integer as a parameter.

        This turns out to be a relatively rare case where this is needed - however, functionality like this is needed.

        Parameters:
        methodName - name of method
        args - the actual arguments with which the method is called
        Returns:
        the most specific applicable method, or null if no method is applicable.
        Throws:
        MethodMap.AmbiguousException - if there is more than one maximally specific applicable method
      • moreSpecific

        private static int moreSpecific​(java.lang.Class<?>[] c1,
                                        java.lang.Class<?>[] c2)
        Determines which method signature (represented by a class array) is more specific. This defines a partial ordering on the method signatures.
        Parameters:
        c1 - first signature to compare
        c2 - second signature to compare
        Returns:
        MORE_SPECIFIC if c1 is more specific than c2, LESS_SPECIFIC if c1 is less specific than c2, INCOMPARABLE if they are incomparable.
      • getApplicables

        private static java.util.LinkedList<java.lang.reflect.Method> getApplicables​(java.util.List<java.lang.reflect.Method> methods,
                                                                                     java.lang.Class<?>[] classes)
        Returns all methods that are applicable to actual argument types.
        Parameters:
        methods - list of all candidate methods
        classes - the actual types of the arguments
        Returns:
        a list that contains only applicable methods (number of formal and actual arguments matches, and argument types are assignable to formal types through a method invocation conversion).
      • isApplicable

        private static boolean isApplicable​(java.lang.reflect.Method method,
                                            java.lang.Class<?>[] classes)
        Returns true if the supplied method is applicable to actual argument types.
      • isMethodInvocationConvertible

        private static boolean isMethodInvocationConvertible​(java.lang.Class<?> formal,
                                                             java.lang.Class<?> actual)
        Determines whether a type represented by a class object is convertible to another type represented by a class object using a method invocation conversion, treating object types of primitive types as if they were primitive types (that is, a Boolean actual parameter type matches boolean primitive formal type). This behavior is because this method is used to determine applicable methods for an actual parameter list, and primitive types are represented by their object duals in reflective method calls.
        Parameters:
        formal - the formal parameter type to which the actual parameter type should be convertible
        actual - the actual parameter type.
        Returns:
        true if either formal type is assignable from actual type, or formal is a primitive type and actual is its corresponding object type or an object type of a primitive type that can be converted to the formal type.
      • isStrictMethodInvocationConvertible

        private static boolean isStrictMethodInvocationConvertible​(java.lang.Class<?> formal,
                                                                   java.lang.Class<?> actual)
        Determines whether a type represented by a class object is convertible to another type represented by a class object using a method invocation conversion, without matching object and primitive types. This method is used to determine the more specific type when comparing signatures of methods.
        Parameters:
        formal - the formal parameter type to which the actual parameter type should be convertible
        actual - the actual parameter type.
        Returns:
        true if either formal type is assignable from actual type, or formal and actual are both primitive types and actual can be subject to widening conversion to formal.