I have successfully obtained the values of List field from another class using reflection. However, I can't use it if the field is of type List.
Here's my actual code:
class PageModel {
@FindAll({ @FindBy(xpath = "//select") })
public static List<WebElement> webelements;
public static List<String> strings = Arrays.asList("One", "Two", "Three");
}
class Test {
public static void main(String[] args) {
Class model = Class.forName("PageModel");
// List<String> sample
obj = model.getField("strings").get(model);
List<Object> testStrings = (List<Object>) obj;
for (Object str : testStrings)
System.out.print(str);
// List<WebElement> sample
obj = model.getField("webelements").get(model);
List<Object> testElements = (List<Object>) obj;
// java.lang.reflect.Field cannot be cast to java.util.List
}
}
Even when I directly use List testElements = (List) obj; , the same error java.lang.reflect.Field cannot be cast to java.util.List is returned.
Thank you so much in advance.