简体   繁体   中英

Java get field names into a method from different class, where method existing class is extended

As far i know this is not possible, but still posting here just to understand is there any solution already tried by stack members. If i could able to do this, that helps to print some meaning full logs in my selenium based testing project, instead of printing code level xpaths and element IDs

So here what i have in my current code.

Class A having all generic methods defined to use in other classes.

public class classA {

        public void someAction(String elementIdentification){
             //doing some acctions using elementIdentification
         }
}

Class B extended the Class A and used the methods from class A by passing parameters. In Class B these parameters are declared at top of the Class in the form of fields or Constants.

public class classB extends classA{

      //100 to 200 fields are declared like below
        private String RESEARCH_BUTTON = "BUT_23BB8DDB79FD1C6237315";
        private String TRYAGAIN_BUTTON = "//h2[@id='C2__C3__HEAD_5F3EC642AC086D0C23576']";

   public void classBMethods(){
         someAction(TRYAGAIN_BUTTON );
  }
}

Now when ever i run the test, if method fails its printing as "failed to identify //h2[@id='C2__C3__HEAD_5F3EC642AC086D0C23576']"

and this doesn't give any meaning full information on first glance. Instead of this if i could able to retrieve name ie "TRYAGAIN_BUTTON", so that i can implement logic to print these values in logs and that will be easier when executed batch cases for identifying the root cause of failure.

if it is one OR two place, i could have written different logic passing extra parameter as separate name. but now we already developed the code as ClassA method used at multiple places with different filed values from same ClassB, Also ClassA extended to multiple classes like C and D.

Thanks in advance for the help.

Use an enum instead of just a String to define your elements. The getValue() is a custom method in the enum , while name() is built-in.

Then you can write code like

public enum Element {
    String value;
    RESEARCH_BUTTON("BUT_23BB8DDB79FD1C6237315")
    // Add constructor and getValue() method
}

public void someAction(Element element){
    String val = element.getValue(); // returns the string
    String name = element.name(); // returns RESEARCH_BUTTON
}

Sorry for late reply, but i resolved the issue in other way by using the reflections api with min refactoring. By writing a class with reflections that accepts B object.

In ref class i stored all declared fields from Class B into HashMap in reverse order, ie Variable value as Key & Variable name as value

So in my B class when ever i pass a variable, that passes as value into ref class, and there it looks into hash map for matching key, If matching key is found then respective value is name of my variable from B class.

This value i can use in A to print some meaning full logs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM