Class MemberUtils

java.lang.Object
org.apache.commons.lang3.reflect.MemberUtils

final class MemberUtils extends Object
Contains common code for working with Methods/Constructors, extracted and refactored from MethodUtils when it was imported from Commons BeanUtils.
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    private static final class 
    A class providing a subset of the API of java.lang.reflect.Executable in Java 1.8, providing a common representation for function signatures for Constructors and Methods.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private static final int
     
    private static final Class<?>[]
    Array of primitive number types ordered by "promotability" from narrow to wide.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    (package private) static int
    compareConstructorFit(Constructor<?> left, Constructor<?> right, Class<?>[] actual)
    Compares the relative fitness of two Constructors in terms of how well they match a set of runtime parameter types, such that a list ordered by the results of the comparison would return the best match first (least).
    (package private) static int
    compareMethodFit(Method left, Method right, Class<?>[] actual)
    Compares the relative fitness of two Methods in terms of how well they match a set of runtime parameter types, such that a list ordered by the results of the comparison would return the best match first (least).
    private static int
    Compares the relative fitness of two Executables in terms of how well they match a set of runtime parameter types, such that a list ordered by the results of the comparison would return the best match first (least).
    private static float
    getObjectTransformationCost(Class<?> srcClass, Class<?> destClass)
    Gets the number of steps needed to turn the source class into the destination class.
    private static float
    getPrimitivePromotionCost(Class<?> srcClass, Class<?> destClass)
    Gets the number of steps required to promote a primitive to another type.
    private static float
    Gets the sum of the object transformation cost for each class in the source argument list.
    (package private) static boolean
    Tests whether a Member is accessible.
    (package private) static boolean
    isMatchingConstructor(Constructor<?> method, Class<?>[] parameterTypes)
     
    private static boolean
    isMatchingExecutable(MemberUtils.Executable method, Class<?>[] parameterTypes)
     
    (package private) static boolean
    isMatchingMethod(Method method, Class<?>[] parameterTypes)
     
    (package private) static boolean
    isPackage(int modifiers)
    Tests whether a given set of modifiers implies package access.
    (package private) static boolean
    isPublic(Member member)
    Tests whether a Member is public.
    (package private) static boolean
    isStatic(Member member)
    Tests whether a Member is static.
    (package private) static <T extends AccessibleObject>
    T
    Sets accessibility to work around reflection issues with public members of a superclass with default access.

    Methods inherited from class java.lang.Object

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

    • ACCESS_TEST

      private static final int ACCESS_TEST
      See Also:
    • WIDENING_PRIMITIVE_TYPES

      private static final Class<?>[] WIDENING_PRIMITIVE_TYPES
      Array of primitive number types ordered by "promotability" from narrow to wide.
  • Constructor Details

    • MemberUtils

      MemberUtils()
  • Method Details

    • compareConstructorFit

      static int compareConstructorFit(Constructor<?> left, Constructor<?> right, Class<?>[] actual)
      Compares the relative fitness of two Constructors in terms of how well they match a set of runtime parameter types, such that a list ordered by the results of the comparison would return the best match first (least).
      Parameters:
      left - The "left" Constructor.
      right - The "right" Constructor.
      actual - The runtime parameter types to match against. left/right.
      Returns:
      int consistent with compare semantics.
    • compareMethodFit

      static int compareMethodFit(Method left, Method right, Class<?>[] actual)
      Compares the relative fitness of two Methods in terms of how well they match a set of runtime parameter types, such that a list ordered by the results of the comparison would return the best match first (least).
      Parameters:
      left - The "left" Method.
      right - The "right" Method.
      actual - The runtime parameter types to match against. left/right.
      Returns:
      int consistent with compare semantics.
    • compareParameterTypes

      private static int compareParameterTypes(MemberUtils.Executable left, MemberUtils.Executable right, Class<?>[] actual)
      Compares the relative fitness of two Executables in terms of how well they match a set of runtime parameter types, such that a list ordered by the results of the comparison would return the best match first (least).
      Parameters:
      left - The "left" Executable.
      right - The "right" Executable.
      actual - The runtime parameter types to match against. left/right.
      Returns:
      int consistent with compare semantics.
    • getObjectTransformationCost

      private static float getObjectTransformationCost(Class<?> srcClass, Class<?> destClass)
      Gets the number of steps needed to turn the source class into the destination class. This represents the number of steps in the object hierarchy graph.
      Parameters:
      srcClass - The source class.
      destClass - The destination class.
      Returns:
      The cost of transforming an object.
    • getPrimitivePromotionCost

      private static float getPrimitivePromotionCost(Class<?> srcClass, Class<?> destClass)
      Gets the number of steps required to promote a primitive to another type.
      Parameters:
      srcClass - The (primitive) source class.
      destClass - The (primitive) destination class.
      Returns:
      The cost of promoting the primitive.
    • getTotalTransformationCost

      private static float getTotalTransformationCost(Class<?>[] srcArgs, MemberUtils.Executable executable)
      Gets the sum of the object transformation cost for each class in the source argument list.
      Parameters:
      srcArgs - The source arguments.
      executable - The executable to calculate transformation costs for.
      Returns:
      The total transformation cost.
    • isAccessible

      static boolean isAccessible(Member member)
      Tests whether a Member is accessible.
      Parameters:
      member - Member to test, may be null.
      Returns:
      true if m is accessible.
    • isMatchingConstructor

      static boolean isMatchingConstructor(Constructor<?> method, Class<?>[] parameterTypes)
    • isMatchingExecutable

      private static boolean isMatchingExecutable(MemberUtils.Executable method, Class<?>[] parameterTypes)
    • isMatchingMethod

      static boolean isMatchingMethod(Method method, Class<?>[] parameterTypes)
    • isPackage

      static boolean isPackage(int modifiers)
      Tests whether a given set of modifiers implies package access.
      Parameters:
      modifiers - to test.
      Returns:
      true unless package/protected/private modifier detected
    • isPublic

      static boolean isPublic(Member member)
      Tests whether a Member is public.
      Parameters:
      member - Member to test, may be null.
      Returns:
      true if m is public.
    • isStatic

      static boolean isStatic(Member member)
      Tests whether a Member is static.
      Parameters:
      member - Member to test, may be null.
      Returns:
      true if m is static.
    • setAccessibleWorkaround

      static <T extends AccessibleObject> T setAccessibleWorkaround(T obj)
      Sets accessibility to work around reflection issues with public members of a superclass with default access.

      When a public class has a default access superclass with public members, these members are accessible. Calling them from compiled code works fine. Unfortunately, on some JVMs, using reflection to invoke these members seems to (wrongly) prevent access even when the modifier is public. Calling setAccessible(true) solves the problem but will only work from sufficiently privileged code. Better workarounds would be gratefully accepted.

      Parameters:
      obj - The AccessibleObject to set as accessible, may be null.
      Returns:
      A boolean indicating whether the accessibility of the object was set to true.