【发布时间】:2018-06-28 15:40:47
【问题描述】:
出于某种原因,我不明白为什么这段代码会打印true 和false,数组有什么特别之处在于它没有在此处包含该注释?
如果您改用getParameters,它会按预期工作。
import java.lang.annotation.*;
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE_USE, ElementType.PARAMETER})
@interface Lel {
}
class Test {
public static void a(@Lel String args) {}
public static void b(@Lel String[] args) {}
public static void main(String[] args) throws Exception {
System.out.println(Test.class.getDeclaredMethod("a", String.class)
.getAnnotatedParameterTypes()[0].isAnnotationPresent(Lel.class));
System.out.println(Test.class.getDeclaredMethod("b", String[].class)
.getAnnotatedParameterTypes()[0].isAnnotationPresent(Lel.class));
}
}
【问题讨论】:
标签: java reflection annotations