Example usage for org.apache.commons.beanutils MethodUtils isAssignmentCompatible

List of usage examples for org.apache.commons.beanutils MethodUtils isAssignmentCompatible

Introduction

In this page you can find the example usage for org.apache.commons.beanutils MethodUtils isAssignmentCompatible.

Prototype

public static final boolean isAssignmentCompatible(Class parameterType, Class parameterization) 

Source Link

Document

Determine whether a type can be used as a parameter in a method invocation.

Usage

From source file:com.dianping.squirrel.client.util.ClassUtils.java

@SuppressWarnings("unchecked")
private static <T> Constructor<T> getMatchingDeclaredConstructor(Class<T> clazz, Class<?>[] parameterTypes) {
    try {//from  ww  w  .  jav a 2  s . c  o  m
        Constructor<T> ctor = clazz.getConstructor(parameterTypes);
        try {
            ctor.setAccessible(true);
        } catch (SecurityException se) {
            // do nothing
        }
        return ctor;

    } catch (NoSuchMethodException e) {
    }

    int paramSize = parameterTypes.length;
    Constructor<?>[] ctors = clazz.getDeclaredConstructors();
    for (int i = 0, size = ctors.length; i < size; i++) {
        Class<?>[] ctorParams = ctors[i].getParameterTypes();
        int ctorParamSize = ctorParams.length;
        if (ctorParamSize == paramSize) {
            boolean match = true;
            for (int n = 0; n < ctorParamSize; n++) {
                if (!MethodUtils.isAssignmentCompatible(ctorParams[n], parameterTypes[n])) {
                    match = false;
                    break;
                }
            }

            if (match) {
                Constructor<?> ctor = getDeclaredConstructor(ctors[i]);
                if (ctor != null) {
                    return (Constructor<T>) ctor;
                }
            }
        }
    }
    return null;
}

From source file:com.datastax.hectorjpa.spring.ConsistencyLevelAspect.java

/**
 * Get the closest match to our method. Will check inheritance as well as
 * overloading/* w w  w  .  j  av  a2s .  com*/
 * 
 * @param target
 * @param methodName
 * @param paramTypes
 * @return
 */
private Method getMatchingAccessibleMethod(Class<?> clazz, String methodName, Class<?>[] parameterTypes) {

    // search through all methods
    int paramSize = parameterTypes.length;
    Method bestMatch = null;
    Method[] methods = clazz.getMethods();
    float bestMatchCost = Float.MAX_VALUE;
    float myCost = Float.MAX_VALUE;
    for (int i = 0, size = methods.length; i < size; i++) {
        if (methods[i].getName().equals(methodName)) {

            // compare parameters
            Class<?>[] methodsParams = methods[i].getParameterTypes();
            int methodParamSize = methodsParams.length;
            if (methodParamSize == paramSize) {
                boolean match = true;
                for (int n = 0; n < methodParamSize; n++) {

                    if (!MethodUtils.isAssignmentCompatible(methodsParams[n], parameterTypes[n])) {

                        match = false;
                        break;
                    }
                }

                if (match) {
                    // get accessible version of method
                    Method method = MethodUtils.getAccessibleMethod(clazz, methods[i]);
                    if (method != null) {

                        myCost = getTotalTransformationCost(parameterTypes, method.getParameterTypes());
                        if (myCost < bestMatchCost) {
                            bestMatch = method;
                            bestMatchCost = myCost;
                        }
                    }

                }
            }
        }
    }

    return bestMatch;
}

From source file:com.datastax.hectorjpa.spring.ConsistencyLevelAspect.java

/**
 * Gets the number of steps required needed to turn the source class into
 * the destination class. This represents the number of steps in the object
 * hierarchy graph./*w w w  .  j a  v  a2s . c  om*/
 * 
 * @param srcClass
 *            The source class
 * @param destClass
 *            The destination class
 * @return The cost of transforming an object
 */
private float getObjectTransformationCost(Class<?> srcClass, Class<?> destClass) {
    float cost = 0.0f;
    while (srcClass != null && !destClass.equals(srcClass)) {
        if (destClass.isInterface() && MethodUtils.isAssignmentCompatible(destClass, srcClass)) {
            // slight penalty for interface match.
            // we still want an exact match to override an interface match,
            // but
            // an interface match should override anything where we have to
            // get a
            // superclass.
            cost += 0.25f;
            break;
        }
        cost++;
        srcClass = srcClass.getSuperclass();
    }

    /*
     * If the destination class is null, we've travelled all the way up to
     * an Object match. We'll penalize this by adding 1.5 to the cost.
     */
    if (srcClass == null) {
        cost += 1.5f;
    }

    return cost;
}