MemberUtils.setAccessibleWorkaround(ctor); return ctor; } catch (final NoSuchMethodException e) { // NOPMD - Swallow if (MemberUtils.isMatchingConstructor(ctor, parameterTypes)) { MemberUtils.setAccessibleWorkaround(ctor); if (result == null || MemberUtils.compareConstructorFit(ctor, result, parameterTypes) < 0) {
try { final Method method = cls.getMethod(methodName, parameterTypes); MemberUtils.setAccessibleWorkaround(method); return method; } catch (final NoSuchMethodException e) { // NOPMD - Swallow the exception MemberUtils.isMatchingMethod(method, parameterTypes)) { if (accessibleMethod != null && (bestMatch == null || MemberUtils.compareMethodFit( accessibleMethod, bestMatch, MemberUtils.setAccessibleWorkaround(bestMatch);
/** * 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). * * @param left the "left" Constructor * @param right the "right" Constructor * @param actual the runtime parameter types to match against * {@code left}/{@code right} * @return int consistent with {@code compare} semantics * @since 3.5 */ static int compareConstructorFit(final Constructor<?> left, final Constructor<?> right, final Class<?>[] actual) { return compareParameterTypes(Executable.of(left), Executable.of(right), actual); }
/** * <p>Checks if the specified constructor is accessible.</p> * * <p>This simply ensures that the constructor is accessible.</p> * * @param <T> the constructor type * @param ctor the prototype constructor object, not {@code null} * @return the constructor, {@code null} if no matching accessible constructor found * @see java.lang.SecurityManager * @throws NullPointerException if {@code ctor} is {@code null} */ public static <T> Constructor<T> getAccessibleConstructor(final Constructor<T> ctor) { Validate.notNull(ctor, "constructor cannot be null"); return MemberUtils.isAccessible(ctor) && isAccessible(ctor.getDeclaringClass()) ? ctor : null; }
static boolean isMatchingConstructor(final Constructor<?> method, final Class<?>[] parameterTypes) { return isMatchingExecutable(Executable.of(method), parameterTypes); }
/** * 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). * * @param left the "left" Executable * @param right the "right" Executable * @param actual the runtime parameter types to match against * {@code left}/{@code right} * @return int consistent with {@code compare} semantics */ private static int compareParameterTypes(final Executable left, final Executable right, final Class<?>[] actual) { final float leftCost = getTotalTransformationCost(actual, left); final float rightCost = getTotalTransformationCost(actual, right); return leftCost < rightCost ? -1 : rightCost < leftCost ? 1 : 0; }
return getPrimitivePromotionCost(srcClass, destClass);
totalCost += getObjectTransformationCost(srcArgs[i], destArgs[i]); if (noVarArgsPassed) { totalCost += getObjectTransformationCost(destClass, Object.class) + varArgsCost; } else if (explicitArrayForVarags) { final Class<?> sourceClass = srcArgs[srcArgs.length-1].getComponentType(); totalCost += getObjectTransformationCost(sourceClass, destClass) + varArgsCost; } else { totalCost += getObjectTransformationCost(srcClass, destClass) + varArgsCost;
if (!MemberUtils.isAccessible(field)) { if (forceAccess) { field.setAccessible(true);
static boolean isMatchingMethod(final Method method, final Class<?>[] parameterTypes) { return isMatchingExecutable(Executable.of(method), parameterTypes); }
/** * 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). * * @param left the "left" Executable * @param right the "right" Executable * @param actual the runtime parameter types to match against * {@code left}/{@code right} * @return int consistent with {@code compare} semantics */ private static int compareParameterTypes(final Executable left, final Executable right, final Class<?>[] actual) { final float leftCost = getTotalTransformationCost(actual, left); final float rightCost = getTotalTransformationCost(actual, right); return leftCost < rightCost ? -1 : rightCost < leftCost ? 1 : 0; }
return getPrimitivePromotionCost(srcClass, destClass);
totalCost += getObjectTransformationCost(srcArgs[i], destArgs[i]); if (noVarArgsPassed) { totalCost += getObjectTransformationCost(destClass, Object.class) + varArgsCost; } else if (explicitArrayForVarags) { final Class<?> sourceClass = srcArgs[srcArgs.length-1].getComponentType(); totalCost += getObjectTransformationCost(sourceClass, destClass) + varArgsCost; } else { totalCost += getObjectTransformationCost(srcClass, destClass) + varArgsCost;
MemberUtils.setAccessibleWorkaround(ctor); return ctor; } catch (final NoSuchMethodException e) { // NOPMD - Swallow if (MemberUtils.isMatchingConstructor(ctor, parameterTypes)) { MemberUtils.setAccessibleWorkaround(ctor); if (result == null || MemberUtils.compareConstructorFit(ctor, result, parameterTypes) < 0) {
try { final Method method = cls.getMethod(methodName, parameterTypes); MemberUtils.setAccessibleWorkaround(method); return method; } catch (final NoSuchMethodException e) { // NOPMD - Swallow the exception MemberUtils.isMatchingMethod(method, parameterTypes)) { if (accessibleMethod != null && (bestMatch == null || MemberUtils.compareMethodFit( accessibleMethod, bestMatch, MemberUtils.setAccessibleWorkaround(bestMatch);
/** * <p>Returns an accessible method (that is, one that can be invoked via * reflection) that implements the specified Method. If no such method * can be found, return {@code null}.</p> * * @param method The method that we wish to call * @return The accessible method */ public static Method getAccessibleMethod(Method method) { if (!MemberUtils.isAccessible(method)) { return null; } // If the declaring class is public, we are done final Class<?> cls = method.getDeclaringClass(); if (Modifier.isPublic(cls.getModifiers())) { return method; } final String methodName = method.getName(); final Class<?>[] parameterTypes = method.getParameterTypes(); // Check the implemented interfaces and subinterfaces method = getAccessibleMethodFromInterfaceNest(cls, methodName, parameterTypes); // Check the superclass chain if (method == null) { method = getAccessibleMethodFromSuperclass(cls, methodName, parameterTypes); } return method; }
static boolean isMatchingMethod(final Method method, final Class<?>[] parameterTypes) { return isMatchingExecutable(Executable.of(method), parameterTypes); }
/** * 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). * * @param left the "left" Method * @param right the "right" Method * @param actual the runtime parameter types to match against * {@code left}/{@code right} * @return int consistent with {@code compare} semantics * @since 3.5 */ static int compareMethodFit(final Method left, final Method right, final Class<?>[] actual) { return compareParameterTypes(Executable.of(left), Executable.of(right), 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). * * @param left the "left" Executable * @param right the "right" Executable * @param actual the runtime parameter types to match against * {@code left}/{@code right} * @return int consistent with {@code compare} semantics */ private static int compareParameterTypes(final Executable left, final Executable right, final Class<?>[] actual) { final float leftCost = getTotalTransformationCost(actual, left); final float rightCost = getTotalTransformationCost(actual, right); return leftCost < rightCost ? -1 : rightCost < leftCost ? 1 : 0; }
return getPrimitivePromotionCost(srcClass, destClass);