Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Annotation部分 确认有误 #4

Open
Jijun opened this issue Mar 26, 2018 · 0 comments
Open

Annotation部分 确认有误 #4

Jijun opened this issue Mar 26, 2018 · 0 comments

Comments

@Jijun
Copy link

Jijun commented Mar 26, 2018

@interface Hints {
Hint[] value();
}

@repeatable(Hints.class)
@interface Hint {
String value();
}
@hint("hint1")
@hint("hint2")
class Person {
}

Hint hint = Person.class.getAnnotation(Hint.class);
System.out.println(hint); // null

Hints hints1 = Person.class.getAnnotation(Hints.class);
System.out.println(hints1.value().length); // 2 这块有错误,使用java8运行空指针异常

Hint[] hints2 = Person.class.getAnnotationsByType(Hint.class);
System.out.println(hints2.length); // 2

需要将代码的annotation的加上@retention(RetentionPolicy.RUNTIME),否则默认是@retention(RetentionPolicy.CLASS),不过源代码是有这个策略的,但是文档没有所以为了避免误导开发者,尽量加上这块

原文也有一个issue
https://github.com/winterbe/java8-tutorial/issues/36

@Jijun Jijun changed the title Annotation 确认有误 Annotation部分 确认有误 Mar 26, 2018
@Jijun Jijun closed this as completed Mar 26, 2018
@Jijun Jijun reopened this Mar 26, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant