Skip to content

Instantly share code, notes, and snippets.

@jyeary
Created April 25, 2016 13:08
Show Gist options
  • Select an option

  • Save jyeary/065160d2368a8ad1d9b8cb74fc72ef60 to your computer and use it in GitHub Desktop.

Select an option

Save jyeary/065160d2368a8ad1d9b8cb74fc72ef60 to your computer and use it in GitHub Desktop.
Determines the calling Class within a method, and returns the name of the Class.
public class Utils {
/**
* Default Constructor
*/
public Utils() {
}
/**
* Determines the caller class. This method should be invoked within a
* method body to determine which {@code Class} called it.
*
* @return The name of the {@code Class} which invoked the method.
*/
public static String getCallerClazz() {
StackTraceElement[] stElements = Thread.currentThread().getStackTrace();
StackTraceElement st = stElements[stElements.length - 1];
return st.getClassName();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment