Java Method from Class hasMethod(String clazz, String methodName, Class[] methodTypes)

Here you can find the source of hasMethod(String clazz, String methodName, Class[] methodTypes)

Description

return true if given method exists

License

Open Source License

Parameter

Parameter Description
clazz - meta class
methodName - name of methods
methodTypes - types of arguments

Declaration

public static boolean hasMethod(String clazz, String methodName, Class[] methodTypes) 

Method Source Code

//package com.java2s;
/* ===========================================================================
 * $RCS$//from   ww w  .  j  av  a  2s  . c  om
 * Version: $Id: ReflectHelper.java,v 2.6 2006/08/18 01:59:13 shahzad Exp $
 * ===========================================================================
 *
 * TestPlayer - an automated test harness builder
 *
 * Copyright (c) 2005-2006 Shahzad Bhatti (bhatti@plexobject.com)
 *
 * This program is free software; you can redistribute it and/or modify 
 * it under the terms of the GNU General Public License as published by 
 * the Free Software Foundation; either version 2 of the License, or 
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License 
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * The author may be contacted at bhatti@plexobject.com 
 * See http://testplayer.dev.java.net/ for more details.
 *
 */

import java.lang.reflect.*;

public class Main {
    /**
     * return true if given method exists
     * @param clazz - meta class 
     * @param methodName - name of methods
     * @param methodTypes - types of arguments
     */
    public static boolean hasMethod(String clazz, String methodName, Class[] methodTypes) {
        try {
            return hasMethod(Class.forName(clazz), methodName, methodTypes);
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * return true if given method exists
     * @param clazz - meta class 
     * @param methodName - name of methods
     * @param methodTypes - types of arguments
     */
    public static boolean hasMethod(Class clazz, String methodName, Class[] methodTypes) {
        try {
            Method method = clazz.getMethod(methodName, methodTypes);
            return method != null;
        } catch (Exception e) {
            return false;
        }
    }
}

Related

  1. getAllMethodsWithAnnotation( String packageName, Class annotation)
  2. getDeclaredMethod(final Class clazz, final String methodName, final Class[] methodParameterTypes)
  3. getMethod(Class type, Method m)
  4. getMethod(final Class javaClass, final String methodName, final Class[] methodParameterTypes, final boolean shouldSetAccessible)
  5. getMethods(final Class clazz)
  6. invokePrivateMethod(String methodName, Class[] params, Object[] args, Class containingClass, Object invocationTarget)