Currently executing method in Java

Goal

Get the currently executing method in Java

Description

This recipe explains a simple yet effective way to get the currently executing method in Java. The original proposal comes from the following post in stackoverflow to which I say thanks 🙂

How to

The following does the trick:

String name = new Object(){}.getClass().getEnclosingMethod().getName();

Explanations

The enclodingMethod method works in the context of an anonymous inner class which is more or less what the previous code does. So, using it, it causes the generation of annonymous inner classes per method using this trick.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s