Mo3z
Mo3z

Reputation: 2128

Detecting whether a method/function exists in Java

Is there a method/function in Java that checks if another method/function is available just like function_exists(functionName) in PHP?

Here I am referring to a method/function of static class.

Upvotes: 14

Views: 31938

Answers (7)

Alecs
Alecs

Reputation: 2940

Here my solution using reflection...

    public static boolean methodExists(Class clazz, String methodName) {
    boolean result = false;
    for (Method method : clazz.getDeclaredMethods()) {
        if (method.getName().equals(methodName)) {
            result = true;
            break;
        }
    }
    return result;
}

Upvotes: 3

rit
rit

Reputation: 2308

You can use Reflections to lookup if the method exists:

public class Test {
    public static void main(String[] args) throws NoSuchMethodException {
        Class clazz = Test.class;

        for (Method method : clazz.getDeclaredMethods()) {
                if (method.getName().equals("fooBar")) {
                System.out.println("Method fooBar exists.");
            }
        }

        if (clazz.getDeclaredMethod("fooBar", null) != null) {
            System.out.println("Method fooBar exists.");
        }
   }

   private static void fooBar() {
   }
  }

But Reflection is not really fast so be careful when to use it (probably cache it).

Upvotes: 9

Cristiano Fontes
Cristiano Fontes

Reputation: 5088

You can do this like this

Obj.getClass().getDeclaredMethod(MethodName, parameterTypes)

Upvotes: 1

maerics
maerics

Reputation: 156642

Try using the Class.getMethod() method of the Class class =)

public class Foo {
  public static String foo(Integer x) {
    // ...
  }
  public static void main(String args[]) throws Exception {
    Method fooMethod = Foo.class.getMethod("foo", Integer.class);
    System.out.println(fooMethod);
  }
}

Upvotes: 5

lynks
lynks

Reputation: 5689

You can use the reflection API to achieve this.

YourStaticClass.getClass().getMethods();

Upvotes: 1

Joachim Sauer
Joachim Sauer

Reputation: 308249

You can find out if a method exists in Java using reflection.

Get the Class object of the class you're interested in and call getMethod() with the method name and parameter types on it.

If the method doesn't exist, it will throw a NoSuchMethodException.

Also, please note that "functions" are called methods in Java.

Last but not least: keep in mind that if you think you need this, then chances are that you've got a design problem at hand. Reflection (which is what the methods to inspect the actual Java classes is called) is a rather specialized feature of Java and should not generally be used in business code (although it's used quite heavily and to some nice effects in some common libraries).

Upvotes: 22

Jon Skeet
Jon Skeet

Reputation: 1503489

I suspect you're looking for Class.getDeclaredMethods and Class.getMethods which will give you the methods of a class. You can then test whether the one you're looking for exists or not, and what it's parameters are etc.

Upvotes: 11

Related Questions