Here are two ways of doing this.
Approach #1
This is going to be nothing but a hack and you will be stuck with dependencies on JDK flavors and versions as well.
Here are the high- level set of actions that you would need to do. I have created a detailed blog post that talks about how to do this in JDK7 and JDK8 [ I haven't tried this in JDK9, so your mileage may vary ]
- Create classes that implement each of the annotations (Annotations are internally represented as interfaces, so you can build classes that implement them)
- Use reflection to query the field that holds annotations and once you have it you instantiate the class that you created for the annotation, with values that you now need to represent (in your case, with values from the database) and then push it back into the field.
That should do it for you.
Approach #2
Here we basically piggy back on the customization support that Selenium's page factory model provides you. On a high-level here are the list of things that you need to do, to get this done. For a detailed overview, you can refer to my blog post here.
- A customized
org.openqa.selenium.support.pagefactory.ElementLocator
- A customized
org.openqa.selenium.support.pagefactory.ElementLocatorFactory
- A customized approach to deciphering the annotations and reading out the values from the annotations.
- A custom annotation that captures some meta data which can be used to read the locator from the DB for a given web element.