0

I want output com.iland.Action.LoginAction.doLogin()

The code should be effective for any class

for example

package com.iland.Action

Class LoginAction{

  void doLogin()
  {
      System.out.println("");//what contens should be printed in sop
  }
}

I would like to print dynamically fully qualified class name and the method name which is being executed.

1

3 Answers 3

4

Here is a code fragment that can help you.

StackTraceElement trace = new Throwable().getStackTrace()[0];
trace.getMethodName(); // returns method name
trace.getClassName(); // returns class name
Sign up to request clarification or add additional context in comments.

1 Comment

Can we get fully qualified method name as I need com.iland.Action.LoginAction.doLogin()
0

You can use this code inside your function body:

String Name=this.getClass().toString();
String fullName=Name.substring(Name.lastIndexOf(" ")+1);
System.out.println(fullName);// fully qualified class name.
System.out.println(fullName.substring(0,fullName.lastIndexOf(".")));// package name
System.out.println(fullName+new Throwable().getStackTrace()[0].getMethodName());// fully qualified method name

For more information on getStackTrace, refer this

5 Comments

How to get package name
@AmanArora this code i am using while catching exception. Could you tell me how to get line number of exception code
@xrcwrn Line number of exception code?.. Can you elaborate a bit?
I am using this code for logging try{.........} catch (SQLException e) { status = "failure"; System.out.println("Exception " + e); LogErrorBusiness.log(new Throwable().getStackTrace()[0].getClassName(), new Throwable().getStackTrace()[0].getMethodName(), e.toString()); along with generating fully qualified class name and method name i want to get line number where exception is being thrown
try this Thread.currentThread().getStackTrace()[2].getLineNumber();
0

It will show you class name with packege and method name

  System.out.println(new Throwable().getStackTrace()[0].getClassName());//  class name
        System.out.println(new Throwable().getStackTrace()[0].getMethodName());// method name
        StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace();
        System.out.println(stackTraceElements[1].getClassName());//class name
        System.out.println(stackTraceElements[1].getMethodName());//method name

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.