I have method name in java, with that method name how can I get the class name

166 Views Asked by At

I want to retrieve the class name in the runtime, I have a method name declared inside the class, with the help of method name how can i retrieve the class name in java??

Exmaple:

public class DesktopTest extends BaseTest {
    @Test
    @Parameters("Browser")
    public void navigateToWebPage(String browser) {
        System.out.print("Testing Done");
    } 

I have method name "navigateToWebPage" which will be passed from maven commands, how to get the class name with this info

Help will be much appreciated

1

There are 1 best solutions below

2
On
String className = new Object(){}.getClass().getEnclosingClass().getName();

It's a method name, but what do you think?

or another

String className = CommonFuntion.getClassName();

2023.08.19

Sorry to Late Anyway, you can refer this

public static void main(){

TestClass testClass = new TestClass();

System.out.println("getName()      : " + testClass.getClass().getName());
System.out.println("getSimpleName()  : " + testClass.getClass().getSimpleName());
System.out.println("===========getMethods()===========");
for(Method method : testClass.getClass().getMethods()){
    System.out.println("method          : " + method.getName());
}
System.out.println("===========getFields()===========");
for(Field field : testClass.getClass().getFields()){
    System.out.println("field           : " + field.getName());
}

}