如何为自己的注释创建可选参数?

下面是注释代码

public @interface ColumnName {
String value();
String datatype();
}

例如,我希望使 datatype成为一个可选参数

@ColumnName(value="password")

应该是一个有效的代码。

90455 次浏览

Seems like the first example in the official documentation says it all ...

/**
* Describes the Request-For-Enhancement(RFE) that led
* to the presence of the annotated API element.
*/
public @interface RequestForEnhancement {
int    id();
String synopsis();
String engineer() default "[unassigned]";
String date()     default "[unimplemented]";
}

To make it optional you can assign it a default value like that:

public @interface ColumnName {
String value();
String datatype() default "String";
}

Then it doesn't need to be specified when using the Annotation.