Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Android, how do I find caller of a method efficiently

I am building a testing/analysis tool that requires instrumentation of Android framework classes. In that tool, the classes need to be instrumented to find out the caller of a method (i.e., method signature) that is currently being executed, and based on the caller some processing is done. My question is, which method I should use to determine the caller?

I know that one approach is to use java.lang.Throwable and the method getStackTrace. However, it is not efficient. Are there any other (public or hidden) API's (similar to sun.reflect.Reflection.getCallerClass of standard JDK) for Android platform that can be used?

like image 801
anand Avatar asked Sep 09 '11 21:09

anand


People also ask

How do you find out who called a method in Java?

Sometimes it is nice to access the caller of a method. Doing this is relatively simple, you can use the getStackTrace() method on the current thread.

What is caller method?

Method Calls A method is a routine that applies to a particular class of objects. Once an object is declared, you can refer to it by its identifier when calling methods. The following example calls the SetActive method on the Find dialog box: Find.SetActive ()

How do you call all methods in a class?

Write the callAll() method to actually call each method explicitly without reflection. This allows for flexibility in subclassing. It also allows you to deal with methods which have parameters. It also allows you to bypass methods which don't apply.


1 Answers

There's Thread.getStackTrace().

like image 65
Dan S Avatar answered Sep 22 '22 16:09

Dan S