Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Java Reflection, can you detect if a method is Native or not?

Using Java Reflection, you can detect all methods and their returns type. But is there a way to detect if a method is declared as native or not?

like image 205
Adel Boutros Avatar asked Nov 28 '22 17:11

Adel Boutros


2 Answers

Yes you can.The method getModifiers() returns an int which applied the right mask can tell you if the method is native or not

I would suggest doing it like this, for convenience:

   int modifiers = myMethod.getModifiers(); 
   boolean isNative = Modifier.isNative(modifiers);

The Modifier class is an utility specialized class meant to apply the appropriate masks in order to discover the modifiers of the method.

like image 135
Razvan Avatar answered Dec 06 '22 19:12

Razvan


Method has a getModifiers() method which returns the modifiers as an int, and one of the modifiers is Modifier.NATIVE, which is what your looking for. Modifier.isNative() can be used to decode the argument from getModifiers().

(Basically, if you have your method as a Method object named m, then Modifier.isNative(m.getModifiers()) should do it.)

like image 30
Dennis Meng Avatar answered Dec 06 '22 19:12

Dennis Meng