'How to tell whether an invoked method has a method body or not at runtime in an Android app/Java application?

I hope to know if an invoked method has a Java method body at runtime in an Android app/Java application.

For example, in the following code, a method f is invoked

obj.f();

To know whether f has a method body, I use reflection to get its modifiers and then check if the method is a native method:

int modifiers = obj.getClass().getMethod("f", new Class[] {}).getModifiers();
if(Modifier.isNative(modifiers)) {
   // No method body
} else {
   // has method body
}

My question is: except native methods, are there any other possible cases in which an invoked method does not have a Java method body? Abstract or interface methods are not possible since at runtime, what have been invoked must be concrete methods that implement them.



Solution 1:[1]

I don't quite understand what good that information could do, so maybe I'm missing something.

But some of the cases that I can think of off the top of my head are

  1. Objects created by java.lang.reflect.Proxy
  2. Lambdas such as Callable c = System::gc.
  3. default methods on interfaces may or may not count, depending on your definition and use-case

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Joachim Sauer