2

Working on a quick logging utility method. Normally I would use Aspects for this but working in the Android SDK where there isnt support for that.

How would I go about grabbing a method name in a generic way using reflection?

Also any performance hit in java with reflection ?

1 Answer 1

3

Something like this will probably work:

Thread.currentThread().getStackTrace()[0].getMethodName()
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, going to try this out. Question: if I was to call this from a generic utility method, technically the methodName I would need would be in the caller method, I should be able just grab the previous stack frame from .getStackTrace ?
Yep. Every method call is a stack frame.
what about performance? if i am logging at minimum two places in every method in every class how much overhead is this going to be?
No idea. Probably not enough to worry about, since I don't think it's actually reflection per se.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.