Check if a class has a Method by name - Android java.lang.reflect

Android examples for java.lang.reflect:Method

Description

Check if a class has a Method by name

Demo Code

/**/*from   ww w . ja v  a  2s.  com*/
 * (c) Winterwell Associates Ltd, used under MIT License. This file is background IP.
 */
//package com.java2s;

import java.lang.reflect.Method;

public class Main {
    public static boolean hasMethod(Object obj, String method) {
        assert method != null;
        Method[] ms = obj.getClass().getMethods();
        for (Method m : ms) {
            if (m.getName().equals(method))
                return true;
        }
        return false;
    }

    /**
     * Lenient equals: null = ""
     * @param a
     * @param b
     * @return true if a=b
     */
    public static boolean equals(String a, String b) {
        if (a == null || a.length() == 0)
            return b == null || b.length() == 0;
        return a.equals(b);
    }
}

Related Tutorials