简体   繁体   English

Java将字段名称从不同的类转换为方法,其中扩展了方法的现有类

[英]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 如果我能够做到这一点,那将有助于在基于硒的测试项目中打印一些有意义的完整日志,而不是打印代码级xpath和元素ID

So here what i have in my current code. 所以这就是我当前代码中的内容。

Class A having all generic methods defined to use in other classes. 类A,具有定义为在其他类中使用的所有通用方法。

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. B类扩展了A类,并通过传递参数使用了A类中的方法。 In Class B these parameters are declared at top of the Class in the form of fields or Constants. 在类B中,这些参数以字段或常量的形式在类的顶部声明。

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']" 现在,当我运行测试时,如果方法无法打印为"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. 如果我能够检索名称(例如“ TRYAGAIN_BUTTON”),则可以代替它,以便我可以实现逻辑以在日志中打印这些值,并且在执行批处理案例以识别故障的根本原因时,这样做会更容易。

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. 但是现在我们已经将代码开发为ClassA方法,并在同一个ClassB中使用了不同字段值的多个位置,并且ClassA扩展到了C和D等多个类。

Thanks in advance for the help. 先谢谢您的帮助。

Use an enum instead of just a String to define your elements. 使用enum而不是仅String来定义元素。 The getValue() is a custom method in the enum , while name() is built-in. getValue()enum的自定义方法,而name()是内置的。

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. 抱歉,您的回复很晚,但是我通过使用带有最小重构的反射API以其他方式解决了该问题。 By writing a class with reflections that accepts B object. 通过编写带有反射的类,该类接受B对象。

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 在ref类中,我将所有从B类声明的字段以相反的顺序存储到HashMap中,即变量值作为键,变量名作为值

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. 因此,在我的B类中,每当我传递一个变量时,该变量作为值传递给ref类,并在其中查找哈希映射以匹配键。

This value i can use in A to print some meaning full logs. 我可以在A中使用此值来打印一些有意义的完整日志。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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