0
How to know which method you are in?
- public class MethodName {
- public String methodOne() {
- return new Exception().getStackTrace()[0].toString();
- }
- public String methodTwo() {
- return Thread.currentThread().getStackTrace()[2].toString();
- }
- public static void main(String[] args) {
- MethodName name = new MethodName();
- long start = System.currentTimeMillis();
- for (int i = 0; i < 100000; i++) {
- name.methodOne();
- }
- System.out.printf("First method - new Exception() - in %d millis\n",
- System.currentTimeMillis() - start);
- start = System.currentTimeMillis();
- for (int i = 0; i < 100000; i++) {
- name.methodTwo();
- }
- System.out.printf("Second method - Thread.currentThread() - in %d millis\n",
- System.currentTimeMillis() - start);
- }
- }