Skip to content

Instantly share code, notes, and snippets.

@JoeyChor
Last active January 18, 2016 07:40
Show Gist options
  • Save JoeyChor/ee7ee551682453b50340 to your computer and use it in GitHub Desktop.
Save JoeyChor/ee7ee551682453b50340 to your computer and use it in GitHub Desktop.
It's so beautiful
public static StackTraceElement getCallingClass() {
Class c = new Object(){}.getClass().getEnclosingClass();
String q = Arrays.stream(new Object(){}.getClass().getEnclosingClass().getName().split("\\."))
.limit(2)
.collect(Collectors.joining("."));
return Arrays.stream(Thread.currentThread().getStackTrace())
.filter((s) -> s.toString().startsWith(q) && !s.toString().startsWith(c.getName()))
.collect(Collectors.toList())
.get(0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment