Package org.apache.commons.lang3.reflect
Class MemberUtils
- java.lang.Object
-
- org.apache.commons.lang3.reflect.MemberUtils
-
final class MemberUtils extends java.lang.ObjectContains common code for working withMethods/Constructors, extracted and refactored fromMethodUtilswhen it was imported from Commons BeanUtils.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static classMemberUtils.ExecutableA 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 intACCESS_TESTprivate static java.lang.Class<?>[]WIDENING_PRIMITIVE_TYPESArray of primitive number types ordered by "promotability" from narrow to wide.
-
Constructor Summary
Constructors Constructor Description MemberUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description (package private) static intcompareConstructorFit(java.lang.reflect.Constructor<?> left, java.lang.reflect.Constructor<?> right, java.lang.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 intcompareMethodFit(java.lang.reflect.Method left, java.lang.reflect.Method right, java.lang.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 intcompareParameterTypes(MemberUtils.Executable left, MemberUtils.Executable right, java.lang.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).private static floatgetObjectTransformationCost(java.lang.Class<?> srcClass, java.lang.Class<?> destClass)Gets the number of steps needed to turn the source class into the destination class.private static floatgetPrimitivePromotionCost(java.lang.Class<?> srcClass, java.lang.Class<?> destClass)Gets the number of steps required to promote a primitive to another type.private static floatgetTotalTransformationCost(java.lang.Class<?>[] srcArgs, MemberUtils.Executable executable)Gets the sum of the object transformation cost for each class in the source argument list.(package private) static booleanisAccessible(java.lang.reflect.Member member)Tests whether aMemberis accessible.(package private) static booleanisMatchingConstructor(java.lang.reflect.Constructor<?> method, java.lang.Class<?>[] parameterTypes)private static booleanisMatchingExecutable(MemberUtils.Executable method, java.lang.Class<?>[] parameterTypes)(package private) static booleanisMatchingMethod(java.lang.reflect.Method method, java.lang.Class<?>[] parameterTypes)(package private) static booleanisPackage(int modifiers)Tests whether a given set of modifiers implies package access.(package private) static booleanisPublic(java.lang.reflect.Member member)Tests whether aMemberis public.(package private) static booleanisStatic(java.lang.reflect.Member member)Tests whether aMemberis static.(package private) static <T extends java.lang.reflect.AccessibleObject>
TsetAccessibleWorkaround(T obj)Default access superclass workaround.
-
-
-
Field Detail
-
ACCESS_TEST
private static final int ACCESS_TEST
- See Also:
- Constant Field Values
-
WIDENING_PRIMITIVE_TYPES
private static final java.lang.Class<?>[] WIDENING_PRIMITIVE_TYPES
Array of primitive number types ordered by "promotability" from narrow to wide.
-
-
Method Detail
-
compareConstructorFit
static int compareConstructorFit(java.lang.reflect.Constructor<?> left, java.lang.reflect.Constructor<?> right, java.lang.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
comparesemantics.
-
compareMethodFit
static int compareMethodFit(java.lang.reflect.Method left, java.lang.reflect.Method right, java.lang.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
comparesemantics.
-
compareParameterTypes
private static int compareParameterTypes(MemberUtils.Executable left, MemberUtils.Executable right, java.lang.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
comparesemantics.
-
getObjectTransformationCost
private static float getObjectTransformationCost(java.lang.Class<?> srcClass, java.lang.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(java.lang.Class<?> srcClass, java.lang.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(java.lang.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(java.lang.reflect.Member member)
Tests whether aMemberis accessible.- Parameters:
member- Member to test, may be null.- Returns:
trueifmis accessible.
-
isMatchingConstructor
static boolean isMatchingConstructor(java.lang.reflect.Constructor<?> method, java.lang.Class<?>[] parameterTypes)
-
isMatchingExecutable
private static boolean isMatchingExecutable(MemberUtils.Executable method, java.lang.Class<?>[] parameterTypes)
-
isMatchingMethod
static boolean isMatchingMethod(java.lang.reflect.Method method, java.lang.Class<?>[] parameterTypes)
-
isPackage
static boolean isPackage(int modifiers)
Tests whether a given set of modifiers implies package access.- Parameters:
modifiers- to test.- Returns:
trueunlesspackage/protected/privatemodifier detected
-
isPublic
static boolean isPublic(java.lang.reflect.Member member)
Tests whether aMemberis public.- Parameters:
member- Member to test, may be null.- Returns:
trueifmis public.
-
isStatic
static boolean isStatic(java.lang.reflect.Member member)
Tests whether aMemberis static.- Parameters:
member- Member to test, may be null.- Returns:
trueifmis static.
-
setAccessibleWorkaround
static <T extends java.lang.reflect.AccessibleObject> T setAccessibleWorkaround(T obj)
Default access superclass workaround.When a
publicclass has a default access superclass withpublicmembers, 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 ispublic. CallingsetAccessible(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.
-
-