我试图以下列方式声明自定义注释:
Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD})
public @interface InnerAnnotation {
}
Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD})
public @interface OuterAnnotation {
public String default "";
public InnerAnnotation innerAnnotation(); //here I wanted to do "public InnerAnnotation innerAnnotation() default {some default value}"
}
I wanted to use it in a way:
class first{
@OuterAnnotation(value = "new") //wanted to declare something like this without need to define innerAnnotation
public void func(){
}
}
我想为内部注释使用分配一些默认值(这样我就不必在使用它时提供任何强制值),但有些我不能这样做,因为编译器要求编译时对此有所了解。可以建议如何使用任何默认值的内部注释吗?
答案 0 :(得分:0)
您所用语法如下:
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD})
public @interface OuterAnnotation {
public String default "";
public InnerAnnotation innerAnnotation() default @InnerAnnotation(); //this does the trick;
}