java 反射 getAnnotations() 返回 null

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16360632/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 22:44:37  来源:igfitidea点击:

Reflection getAnnotations() returns null

javareflection

提问by Menno

Searchable.java

Searchable.java

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Searchable { }

Obj.java

对象Java

public class Obj {
    @Searchable
    String myField;
}

void main(String[] args)

无效主(字符串[]参数)

Annotation[] annotations = Obj.class.getDeclaredField("myField").getAnnotations();

I would expect annotationsto be containing my @Searchable. Though it is null. According to documentation, this method:

我希望annotations包含我的@Searchable. 虽然是null. 根据文档,这种方法:

Returns all annotations present on this element. (Returns an array of length zero if this element has no annotations.) The caller of this method is free to modify the returned array; it will have no effect on the arrays returned to other callers.

返回此元素上存在的所有注释。(如果此元素没有注释,则返回长度为零的数组。)此方法的调用者可以自由修改返回的数组;它不会影响返回给其他调用者的数组。

Which is even more weird (to me), since it returns nullinstead of Annotation[0].

这更奇怪(对我来说),因为它返回null而不是Annotation[0].

What am I doing wrong here and more important, how will I be able to get my Annotation?

我在这里做错了什么,更重要的是,我将如何获得我的Annotation

采纳答案by Erik Pragt

I just tested this for you, and it just works:

我刚刚为你测试了这个,它确实有效:

public class StackOverflowTest {

    @Test
    public void testName() throws Exception {

        Annotation[] annotations = Obj.class.getDeclaredField("myField").getAnnotations();

        System.out.println(annotations[0]);
    }
}

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@interface Searchable {

}

class Obj {

    @Searchable
    String myField;
}

I ran it, and it produces the following output:

我运行它,它产生以下输出:

@nl.jworks.stackoverflow.Searchable()

Can you try running the above class in your IDE? I tried it with IntelliJ, openjdk-6.

您可以尝试在 IDE 中运行上述类吗?我用 IntelliJ、openjdk-6 试过了。

回答by Adam Arold

Your code is correct. The problem is somewhere else. I just copied and run your code and it works.

你的代码是正确的。问题出在其他地方。我只是复制并运行您的代码,它可以工作。

It is possible that you are importing the wrong Objclass in your code you may want to check that first.

Obj您可能在代码中导入了错误的类,您可能需要先检查一下。

回答by McCoy

In my case, the error was in my own annotation. I fixed a couple of things, and it finally ended up like this:

就我而言,错误出在我自己的注释中。我修复了几件事,最终结果是这样的:

import static java.lang.annotation.ElementType.ANNOTATION_TYPE;
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

import java.lang.annotation.Retention;
import java.lang.annotation.Target;
@Target( { METHOD, FIELD, ANNOTATION_TYPE })
@Retention(RUNTIME)
public @interface MyAnnotation{
}

It works now

现在可以用了