Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How To Check If A Method Exists At Runtime In Java?

Tags:

How would one go about checking to see if a method exists for a class in Java? Would a try {...} catch {...} statement be good practice?

like image 975
jrtc27 Avatar asked Aug 14 '11 17:08

jrtc27


People also ask

How do you check if a method exists in a class Java?

Use the typeof operator to check if a method exists in a class, e.g. if (typeof myInstance. myMethod === 'function') {} . The typeof operator returns a string that indicates the type of the specific value and will return function if the method exists in the class.

Does function exist in Java?

There are no functions per se in Java. All you've got is methods. To imitate functions, Java generally uses static methods (as in java. lang.

How do I check if an object has a specific property in Java?

JavaScript provides you with three common ways to check if a property exists in an object: Use the hasOwnProperty() method. Use the in operator. Compare property with undefined .

What is Java reflection used for?

Reflection is a feature in the Java programming language. It allows an executing Java program to examine or "introspect" upon itself, and manipulate internal properties of the program. For example, it's possible for a Java class to obtain the names of all its members and display them.


2 Answers

I assume that you want to check the method doSomething(String, Object).

You might try this:

boolean methodExists = false; try {   obj.doSomething("", null);   methodExists = true; } catch (NoSuchMethodError e) {   // ignore } 

This will not work, since the method will be resolved at compile-time.

You really need to use reflection for it. And if you have access to the source code of the method you want to call, it's even better to create an interface with the method you want to call.

[Update] The additional information is: There is an interface that may exist in two versions, an old one (without the wanted method) and a new one (with the wanted method). Based on that, I suggest the following:

package so7058621;  import java.lang.reflect.Method;  public class NetherHelper {    private static final Method getAllowedNether;   static {     Method m = null;     try {       m = World.class.getMethod("getAllowedNether");     } catch (Exception e) {       // doesn't matter     }     getAllowedNether = m;   }    /* Call this method instead from your code. */   public static boolean getAllowedNether(World world) {     if (getAllowedNether != null) {       try {         return ((Boolean) getAllowedNether.invoke(world)).booleanValue();       } catch (Exception e) {         // doesn't matter       }     }     return false;   }    interface World {     //boolean getAllowedNether();   }    public static void main(String[] args) {     System.out.println(getAllowedNether(new World() {       public boolean getAllowedNether() {         return true;       }     }));   } } 

This code tests whether the method getAllowedNether exists in the interface, so it doesn't matter whether the actual objects have the method or not.

If the method getAllowedNether must be called very often and you run into performance problems because of that, I will have to think of a more advanced answer. This one should be fine for now.

like image 191
Roland Illig Avatar answered Oct 16 '22 02:10

Roland Illig


Reflection API throws NoSuchMethodException when using Class.getMethod(...) functions.

Otherwise Oracle has a nice tutorial about reflection http://download.oracle.com/javase/tutorial/reflect/index.html

like image 42
Jaka Avatar answered Oct 16 '22 00:10

Jaka