mardi 15 décembre 2020

How to get the name of a private field of a child class from the super class using Reflection or libraries?

Let's say I have this code structure:

public abstract class A {
 // members
 ...
 
 // constructor
 ...
 
 // methods
 protected void enterValue(By locator, String value) {
  ...
  System.out.println("entered " + value + " into the " + locator...);
 }
}

public class B extends A {
 // members
 private final By SEARCH_FIELD = By.id("search");
 // ... other FIELD members
 
 // constructor
 ...

 // methods
 public void searchProduct(String product) {
  enterValue(SEARCH_FIELD, product);
  ...
 }
}

The enterValue(By, String) method should prints for example: "entered Talent into the SEARCH_FIELD".
Also, I can have other classes of same structure as class B that can call class A method so I don't know the child class name and which field it would be in advance.
Is this something I can achieve with Reflection in Java or with some libraries?
My goal is to log every action with meaningful names into my ExtentReports.





Aucun commentaire:

Enregistrer un commentaire