Compare two arrays of classes and return true if they are identical. - Java Reflection

Java examples for Reflection:Class

Description

Compare two arrays of classes and return true if they are identical.

Demo Code

/**/*from   ww  w .  ja  v  a  2  s .c om*/
 * Copyright (c) 2011 eXtensible Catalog Organization
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the MIT/X11 license. The text of the license can be
 * found at http://www.opensource.org/licenses/mit-license.php.
 */
import org.apache.log4j.Logger;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Collection;

public class Main{
    /**
     * Compare two arrays of classes and return true if they are identical.
     * @param methodParameterTypes
     * @param parameterTypes
     * @return
     */
    public static boolean parametersMatch(Class[] methodParameterTypes,
            Class[] parameterTypes) {

        boolean parameterTypesMatch = true;

        if (methodParameterTypes.length == 0
                && (parameterTypes == null || parameterTypes.length == 0)) {

            // Do nothing - parameter types match

        } else if (parameterTypes != null
                && parameterTypes.length == methodParameterTypes.length) {

            for (int i = 0; i < parameterTypes.length; i++) {

                if (!methodParameterTypes[i]
                        .isAssignableFrom(parameterTypes[i])) {

                    parameterTypesMatch = false;
                    break;

                }

            }

        } else {

            parameterTypesMatch = false;

        }

        return parameterTypesMatch;

    }
}

Related Tutorials