Java Method from Class existsMethod(final String clazz, final String name, final Class[] parameterTypes)

Here you can find the source of existsMethod(final String clazz, final String name, final Class[] parameterTypes)

Description

Analyze if a class or one of its super classes contains a given method.

License

Open Source License

Parameter

Parameter Description
clazz Class to analyze.
name Method name.
parameterTypes Parameter types.

Return

Does the class (or one of its super classes) have such a method?

Declaration

public static boolean existsMethod(final String clazz, final String name, final Class[] parameterTypes) 

Method Source Code

//package com.java2s;
/* This file is part of the project "Hilbert II" - http://www.qedeq.org
 *
 * Copyright 2000-2013,  Michael Meyling <mime@qedeq.org>.
 *
 * "Hilbert II" 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.
 *//*from  w  ww  .j ava2s .co  m*/

import java.lang.reflect.Method;

public class Main {
    /**
     * Analyze if a class or one of its super classes contains a given method.
     * <p>
     * Example: you can test with <code>YodaUtility.existsMethod("java.net.URLConnection",
     * "setConnectTimeout", new Class[] {Integer.TYPE}</code> with JDK 1.4.2 and if you run it
     * with a 1.5 JRE or higher then it will be successfully executed.
     *
     * @param   clazz           Class to analyze.
     * @param   name            Method name.
     * @param   parameterTypes  Parameter types.
     * @return  Does the class (or one of its super classes) have such a method?
     */
    public static boolean existsMethod(final String clazz, final String name, final Class[] parameterTypes) {
        Class c;
        try {
            c = Class.forName(clazz);
        } catch (ClassNotFoundException e) {
            return false;
        }
        return existsMethod(c, name, parameterTypes);
    }

    /**
     * Analyze if a class or one of its super classes contains a given method.
     * <p>
     * Example: you can test with <code>YodaUtility.existsMethod(URLConnection.class,
     * "setConnectTimeout", new Class[] {Integer.TYPE}</code> with JDK 1.4.2 and if you run it
     * with a 1.5 JRE or higher then it will be successfully executed.
     *
     * @param   clazz           Class to analyze.
     * @param   name            Method name.
     * @param   parameterTypes  Parameter types.
     * @return  Does the class (or one of its super classes) have such a method?
     */
    public static boolean existsMethod(final Class clazz, final String name, final Class[] parameterTypes) {
        Method method = null;
        Class cl = clazz;
        try {
            while (!Object.class.equals(cl)) {
                try {
                    method = cl.getDeclaredMethod(name, parameterTypes);
                    break;
                } catch (NoSuchMethodException ex) {
                    cl = cl.getSuperclass();
                }
            }
            if (method == null) {
                return false;
            }
            return true;
        } catch (SecurityException e) {
            throw new RuntimeException(e);
        }
    }
}

Related

  1. findMethod(Class javaClass, String methodName, Class[] methodParameterTypes)
  2. findMethod(Class c, String name, Class params[])
  3. findMethod(Method m, Class c)
  4. findMethods(Class c)