Class BridgeMethodResolver


  • public final class BridgeMethodResolver
    extends java.lang.Object
    Code taken from the Spring framework. Helper for resolving synthetic bridge Methods to the Method being bridged.

    Given a synthetic bridge Method returns the Method being bridged. A bridge method may be created by the compiler when extending a parameterized type whose methods have parameterized arguments. During runtime invocation the bridge Method may be invoked and/or used via reflection. When attempting to locate annotations on Methods, it is wise to check for bridge Methods as appropriate and find the bridged Method.

    See The Java Language Specification for more details on the use of bridge methods.

    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      private static java.util.Map<java.lang.reflect.TypeVariable<?>,​java.lang.reflect.Type> createTypeVariableMap​(java.lang.Class<?> cls)
      Build a mapping of TypeVariable names to concrete Class for the specified Class.
      private static java.lang.Class<?> extractClassForTypeVariable​(java.lang.reflect.TypeVariable<?> typeVariable)
      Extracts the bound 'Class' for a give TypeVariable.
      private static void extractTypeVariablesFromGenericInterfaces​(java.lang.reflect.Type[] genericInterfaces, java.util.Map<java.lang.reflect.TypeVariable<?>,​java.lang.reflect.Type> typeVariableMap)  
      static java.lang.reflect.Method findBridgedMethod​(java.lang.reflect.Method bridgeMethod)
      Find the original method for the supplied bridge Method.
      private static java.lang.reflect.Method findGenericDeclaration​(java.lang.reflect.Method bridgeMethod)
      Search for the generic Method declaration whose erased signature matches that of the supplied bridge method.
      private static java.lang.reflect.Method findMethod​(java.lang.Class<?> clazz, java.lang.String name, java.lang.Class<?>[] paramTypes)
      Attempt to find a Method on the supplied class with the supplied name and parameter types.
      private static java.lang.reflect.Method[] getAllDeclaredMethods​(java.lang.Class<?> leafClass)
      Get all declared methods on the leaf class and all superclasses.
      private static java.lang.Class<?>[] getAllInterfacesForClass​(java.lang.Class<?> clazz)
      Return all interfaces that the given class implements as array, including ones implemented by superclasses.
      private static java.lang.reflect.Type getRawType​(java.lang.reflect.Type genericType, java.util.Map<java.lang.reflect.TypeVariable<?>,​java.lang.reflect.Type> typeVariableMap)
      Determine the raw type for the given generic parameter type.
      private static boolean isBridgedCandidateFor​(java.lang.reflect.Method candidateMethod, java.lang.reflect.Method bridgeMethod)
      Return true if the supplied 'candidateMethod' can be consider a validate candidate for the Method that is bridged by the supplied bridge Method.
      private static boolean isBridgeMethodFor​(java.lang.reflect.Method bridgeMethod, java.lang.reflect.Method candidateMethod, java.util.Map<java.lang.reflect.TypeVariable<?>,​java.lang.reflect.Type> typeVariableMap)
      Determine whether or not the bridge Method is the bridge for the supplied candidate Method.
      private static boolean isResolvedTypeMatch​(java.lang.reflect.Method genericMethod, java.lang.reflect.Method candidateMethod, java.util.Map<java.lang.reflect.TypeVariable<?>,​java.lang.reflect.Type> typeVariableMap)
      Return true if the Type signature of both the supplied generic Method and concrete Method are equal after resolving all TypeVariables using the supplied TypeVariable Map, otherwise returns false.
      private static void populateTypeMapFromParameterizedType​(java.lang.reflect.ParameterizedType type, java.util.Map<java.lang.reflect.TypeVariable<?>,​java.lang.reflect.Type> typeVariableMap)
      Read the TypeVariables from the supplied ParameterizedType and add mappings corresponding to the TypeVariable name -> concrete type to the supplied Map.
      private static java.lang.reflect.Method searchCandidates​(java.util.List<java.lang.reflect.Method> candidateMethods, java.lang.reflect.Method bridgeMethod)
      Search for the bridged method in the given candidates.
      private static java.lang.reflect.Method searchForMatch​(java.lang.Class<?> type, java.lang.reflect.Method bridgeMethod)
      If the supplied Class has a declared Method whose signature matches that of the supplied Method, then this matching Method is returned, otherwise null is returned.
      • Methods inherited from class java.lang.Object

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

      • BridgeMethodResolver

        private BridgeMethodResolver()
    • Method Detail

      • findBridgedMethod

        public static java.lang.reflect.Method findBridgedMethod​(java.lang.reflect.Method bridgeMethod)
        Find the original method for the supplied bridge Method.

        It is safe to call this method passing in a non-bridge Method instance. In such a case, the supplied Method instance is returned directly to the caller. Callers are not required to check for bridging before calling this method.

        Parameters:
        bridgeMethod - the bridge method
        Returns:
        the original method for the bridge
        Throws:
        java.lang.IllegalStateException - if no bridged Method can be found
      • searchCandidates

        private static java.lang.reflect.Method searchCandidates​(java.util.List<java.lang.reflect.Method> candidateMethods,
                                                                 java.lang.reflect.Method bridgeMethod)
        Search for the bridged method in the given candidates.
        Parameters:
        candidateMethods - the List of candidate Methods
        bridgeMethod - the bridge method
        Returns:
        the bridged method, or null if none found
      • isBridgedCandidateFor

        private static boolean isBridgedCandidateFor​(java.lang.reflect.Method candidateMethod,
                                                     java.lang.reflect.Method bridgeMethod)
        Return true if the supplied 'candidateMethod' can be consider a validate candidate for the Method that is bridged by the supplied bridge Method. This method performs inexpensive checks and can be used quickly filter for a set of possible matches.
      • isBridgeMethodFor

        private static boolean isBridgeMethodFor​(java.lang.reflect.Method bridgeMethod,
                                                 java.lang.reflect.Method candidateMethod,
                                                 java.util.Map<java.lang.reflect.TypeVariable<?>,​java.lang.reflect.Type> typeVariableMap)
        Determine whether or not the bridge Method is the bridge for the supplied candidate Method.
      • findGenericDeclaration

        private static java.lang.reflect.Method findGenericDeclaration​(java.lang.reflect.Method bridgeMethod)
        Search for the generic Method declaration whose erased signature matches that of the supplied bridge method.
        Throws:
        java.lang.IllegalStateException - if the generic declaration cannot be found
      • isResolvedTypeMatch

        private static boolean isResolvedTypeMatch​(java.lang.reflect.Method genericMethod,
                                                   java.lang.reflect.Method candidateMethod,
                                                   java.util.Map<java.lang.reflect.TypeVariable<?>,​java.lang.reflect.Type> typeVariableMap)
        Return true if the Type signature of both the supplied generic Method and concrete Method are equal after resolving all TypeVariables using the supplied TypeVariable Map, otherwise returns false.
      • getRawType

        private static java.lang.reflect.Type getRawType​(java.lang.reflect.Type genericType,
                                                         java.util.Map<java.lang.reflect.TypeVariable<?>,​java.lang.reflect.Type> typeVariableMap)
        Determine the raw type for the given generic parameter type.
      • searchForMatch

        private static java.lang.reflect.Method searchForMatch​(java.lang.Class<?> type,
                                                               java.lang.reflect.Method bridgeMethod)
        If the supplied Class has a declared Method whose signature matches that of the supplied Method, then this matching Method is returned, otherwise null is returned.
      • createTypeVariableMap

        private static java.util.Map<java.lang.reflect.TypeVariable<?>,​java.lang.reflect.Type> createTypeVariableMap​(java.lang.Class<?> cls)
        Build a mapping of TypeVariable names to concrete Class for the specified Class. Searches all super types, enclosing types and interfaces.
      • extractTypeVariablesFromGenericInterfaces

        private static void extractTypeVariablesFromGenericInterfaces​(java.lang.reflect.Type[] genericInterfaces,
                                                                      java.util.Map<java.lang.reflect.TypeVariable<?>,​java.lang.reflect.Type> typeVariableMap)
      • populateTypeMapFromParameterizedType

        private static void populateTypeMapFromParameterizedType​(java.lang.reflect.ParameterizedType type,
                                                                 java.util.Map<java.lang.reflect.TypeVariable<?>,​java.lang.reflect.Type> typeVariableMap)
        Read the TypeVariables from the supplied ParameterizedType and add mappings corresponding to the TypeVariable name -> concrete type to the supplied Map.

        Consider this case:

        
        
        
        • extractClassForTypeVariable

          private static java.lang.Class<?> extractClassForTypeVariable​(java.lang.reflect.TypeVariable<?> typeVariable)
          Extracts the bound 'Class' for a give TypeVariable.
        • getAllInterfacesForClass

          private static java.lang.Class<?>[] getAllInterfacesForClass​(java.lang.Class<?> clazz)
          Return all interfaces that the given class implements as array, including ones implemented by superclasses.

          If the class itself is an interface, it gets returned as sole interface.

          Parameters:
          clazz - the class to analyse for interfaces
          Returns:
          all interfaces that the given object implements as array
        • findMethod

          private static java.lang.reflect.Method findMethod​(java.lang.Class<?> clazz,
                                                             java.lang.String name,
                                                             java.lang.Class<?>[] paramTypes)
          Attempt to find a Method on the supplied class with the supplied name and parameter types. Searches all superclasses up to Object.

          Returns null if no Method can be found.

          Parameters:
          clazz - the class to introspect
          name - the name of the method
          paramTypes - the parameter types of the method
          Returns:
          the Method object, or null if none found
        • getAllDeclaredMethods

          private static java.lang.reflect.Method[] getAllDeclaredMethods​(java.lang.Class<?> leafClass)
          Get all declared methods on the leaf class and all superclasses. Leaf class methods are included first.