Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/394.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 通过reflections.org从父类访问带注释的方法_Java_Inheritance_Reflection_Annotations - Fatal编程技术网

Java 通过reflections.org从父类访问带注释的方法

Java 通过reflections.org从父类访问带注释的方法,java,inheritance,reflection,annotations,Java,Inheritance,Reflection,Annotations,我有这个密码 public static SelenideElement findElementByTitle(String elementName) throws IllegalAccessException { Set<Field> annotated = new Reflections(lastInitialized, new FieldAnnotationsScanner()).getFieldsAnnotatedWith(ElementTitle.class

我有这个密码

public static SelenideElement findElementByTitle(String elementName) throws IllegalAccessException {
        Set<Field> annotated = new Reflections(lastInitialized, new FieldAnnotationsScanner()).getFieldsAnnotatedWith(ElementTitle.class);

        for (Field field : annotated) {
            ElementTitle annotation = field.getAnnotation(ElementTitle.class);
            if (annotation.value().equals(elementName)) {
                field.setAccessible(true);
                SelenideElement el = (SelenideElement) field.get(page(lastInit));
                return el;
            }
        }
        throw new AutotestError("Element not found: " + elementName);
publicstaticselenideelement findElementByTitle(stringelementname)抛出IllegalAccessException{
Set annotated=new Reflections(lastInitialized,new fieldannotationsCanner()).getFieldsAnnotatedWith(ElementTitle.class);
用于(字段:带注释){
ElementTitle注释=field.getAnnotation(ElementTitle.class);
if(annotation.value().equals(elementName)){
字段。setAccessible(true);
SelenideElement el=(SelenideElement)field.get(page(lastInit));
返回el;
}
}
抛出新的AutotestError(“未找到元素:“+elementName”);
在我真正实现继承之前,它工作得很好,而且我的代码似乎无法从父类访问带注释的字段。如果我设置了包访问,我会因为歧义而出现异常(在不同的类中获得了几个相同的注释,例如“下一步”按钮)

下面是设置lastInitialized和lastInit字段的代码(PAGES\u包只是一个字符串,指向包含所有页面对象类的包)

publicstaticvoidinitpage(字符串pageName)引发异常{

设置
不能从父类访问带注释的字段是什么意思。
正是我所说的-我的方法只查看我上次初始化的类中的字段,而不查看它的父类中的字段,因为您将扫描限制为单个类
新反射(lastInitialized
最好只扫描使用普通反射的字段,此库主要用于扫描未加载的类,以防止加载不需要的类。这是否意味着我无法使用此库访问加载类的父类?我对反射有点陌生,我的一位同事建议我是库如果该类与包筛选器(代码中的
lastInitialized
)不匹配,则不匹配。您可以进行多次扫描,但这样做没有任何意义,速度会非常慢。在您的情况下,最好只使用普通反射来获取字段。
public static void initPage(String pageName) throws Exception {
        Set<Class<?>> annotated = new Reflections(PAGES_PACKAGE).getTypesAnnotatedWith(PageTitle.class);

        for (Class classToInit : annotated) {
            PageTitle annotation = (PageTitle) classToInit.getAnnotation(PageTitle.class);
            if (annotation.value().equals(pageName)) {
                classToInit.newInstance();
                lastInitialized = substringAfter(classToInit.toString(), "class ");
                lastInit = classToInit;
                return;
            }
        }

        throw new AutotestError("Can't find page to init: " + pageName);
    }