伙计们,有没有办法将Annotation作为直接参数传递(而不是做所有的反射开销)?例如,在下面的代码中,我有一个包含int值的注释号,我想作为参数传递给addImpl方法,我该如何做到这一点(除了通过反射)?
代码片段:
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.PARAMETER, ElementType.FIELD})
public @interface Number {
int value();
}
public void add(int x2) {
addImpl(@Number(value = 10) lol, x2);
}
public void addImpl(Number a, int b) {
System.out.println(a.value() + b);
}
public static void main(String[] args) {
new TestClass().add(3);
}
https://stackoverflow.com/questions/3282239
复制相似问题