英文:
Java annotation to create constructor
问题
我正在使用PageFactory
页面对象模型来构建我的自动化测试框架。现在,对于每个页面类,我都必须创建一个构造函数。例如:
public class StudentProfile {
public StudentProfile(WebDriver driver) {
this.driver = driver;
PageFactory.initElements(driver, this);
}
}
但我想创建一个自定义注解,这样我就不必为每个页面类重复编写构造函数了。
@InitElements(driver)
public class StudentProfile {
// to do
}
@InitElements(driver)
public class SchoolHomePage {
// to do
}
我已经查看了以下教程,但不明白如何实现它:
英文:
I am using PageFactory
page object model for my Automation Framework. Now for every page class I have to create a constructor. For example:
public class StudentProfile {
public StudentProfile (WebDriver driver) {
this.driver = driver;
PageFactory.initElements(driver, this);
}
}
But I want to create a custom annotation. So that I don't have to repeat it again and again for each Page Class
@InitElements(driver)
public class StudentProfile {
// to do
}
@InitElements(driver)
public class SchoolHomePage {
// to do
}
I have gone through following tuts, but couldn't understand how am I going to implement it?
答案1
得分: 2
基本上你无法实现。注解 API 仅提供两种基本操作:
-
使注解在运行时可检查。如果你想编写一个工具,比如接受某个类的实例,并使用写在该类字段上的注解作为将该实例序列化为 XML 或 JSON 数据的指导,这将非常有用。但对于这种用例,它几乎没有任何作用;你不能在运行时添加构造函数并使其有用(因此构造函数在编译时不存在,而这显然是你想要它们存在的时间)。
-
让注解处理器插入编译过程中并对其进行查看。但是,使用 API,注解处理器默认情况下可以生成新文件;它不能修改现有文件。因此,它也无法添加那个构造函数。
第三种选择是:Project Lombok,它使用注解并可以在现有文件中动态生成代码。但这就像火箭科学 - 我们(我是核心贡献者之一)为各种编译器编写自定义代码,并进行一些几乎不受支持的把戏,以及大量的维护工作,以确保一切正常运行。
对于一个仅适用于你的项目而不适用于其他项目的自定义 lombok 扩展,这听起来是一个不错的任务,但是我们并不提供实质性支持。当然,你可以在 GitHub 上 fork lombok 并添加这个功能,但我们没有关于如何做到这一点的教程,也不建议这样做。
除了 lombok(这很棘手)之外,很遗憾,这是完全不可能的。
英文:
You basically cannot. The annotation API offers only two primitives:
-
Have the annotations be runtime-inspectable. This is great if you want to write a tool that, say, takes an instance of some class and uses annotations written on the fields of that class as guidance for serializing that instance into an XML or JSON blob. It does absolutely nothing for this use-case; you can't just add constructors to things at runtime and have that be useful (the constructors therefore do not exist at compile time which is obviously when you want them).
-
Have an annotation processor plug into the compilation process and it can see them. But, out of the box and by the API, annotation processors can generate new files; it cannot modify existing ones. Thus, it cannot add that constructor either.
There is a third option: Project Lombok which uses annotations and can generate code in existing files on-the-fly. But it's rocket science - we (I'm one of the core contributors) write custom code for various compilers and do some barely (to straight up un-)supported shenanigans along with a lot of maintenance effort to make sure everything keeps working.
This sounds like a fine task for a custom, just-for-your-project-and-no-others lombok addition which we don't meaningfully support. You can of course fork lombok off of github and add this if you must, but we have no tutorials for how to do that, and we don't recommend it.
Other than lombok (which is tricky), this is a straight up -impossible-, unfortunately.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论