2

Suppose I have a method like this

Calculator calc;
public void testMethod(){
-----
----
Calc.add(1,2);
-----
-------
}

Now I want print Calculator.add at the end of the function, i.e i want to print the classname.printname at the end of the function. The printing should be generic enough that I can use the same for other methods as well. How to achieve this goal.

Thanks in advance

3
  • I don't quite follow, do you want to print the method you're in or the method you've just called? And should Calc start with a lower case c? Commented Oct 11, 2012 at 8:51
  • @berry the method tht ive just called Commented Oct 11, 2012 at 8:52
  • 1
    Where do you want the printing to take place? Are you trying to write to a log file? If so, look into Log4J. It's a logging library for java. Commented Oct 11, 2012 at 9:08

4 Answers 4

2
private static final int CLIENT_CODE_STACK_INDEX;

static {
    // Finds out the index of "this code" in the returned stack trace - funny but it differs in JDK 1.5 and 1.6
    int i = 0;
    for (StackTraceElement ste : Thread.currentThread().getStackTrace()) {
        i++;
        if (ste.getClassName().equals(Resource.class.getName())) {
            break;
        }
    }
    CLIENT_CODE_STACK_INDEX = i;
}

public static String getCurrentMethodName() {
    return Thread.currentThread().getStackTrace()[CLIENT_CODE_STACK_INDEX].getMethodName();
}
public static String getCallerMethodName() {
    return Thread.currentThread().getStackTrace()[CLIENT_CODE_STACK_INDEX+1].getMethodName();
}
Sign up to request clarification or add additional context in comments.

2 Comments

im getting Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 6 at Reflections.getCallerMethodName(Reflections.java:32) at Reflections.main(Reflections.java:41)
probably because 'main' has no 'caller'
2

As the very simplest approach, remember you can always call this.getClass().getName():

class SimpleCalculator {

    public int add(int a, int b) {
        System.out.println(this.getClass().getName() +
                " - adding " + a + " and " + b);
        return a + b;
    }
}

Another common approach is to use a logging library like Log4J.

The class you'd be using is Logger

You configure Log4J to write to a certain file.

Each class declares a Logger object that prints messages to a file.

Each message begins with the name of the class that generated the message.

class SimpleCalculator {

    Logger calcLogger = Logger.getLogger(SimpleCalculator.class);

    public int add(int a, int b) {
        calcLogger.debug("add - adding " + a + " and " + b);
        return a + b;
    }
}

... or you could use a method like @urir suggests.

... or you could get crazy and use AOP.

1 Comment

I cannot edit the methods im calling. I should be able to achive this goal from the method where i called the method.
0

user Calculator.getClass().getName());for finding name of class and Calculator.getMethod();

or use System.out.println( "I was called by " + e.getStackTrace()[1].getClassName() + "." + e.getStackTrace()[1].getMethodName() + "()!" );

Comments

0

Check below lines by changing index value:

 System.out.println("Name of Caller method:" 
+ new Throwable().getStackTrace()[1].getMethodName());

    System.out.println("Name of Current method:" 
+ new Throwable().getStackTrace()[0].getMethodName());
            

Comments

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.