Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create optional parameters for own annotations?

Following is the annotation code

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

I would like to make datatype an optional parameter, for example

@ColumnName(value="password")  

should be a valid code.

like image 985
Biju CD Avatar asked Aug 19 '10 09:08

Biju CD


People also ask

How do you make an optional parameter in Java?

Use Varargs to Have Optional Parameters in Java In Java, Varargs (variable-length arguments) allows the method to accept zero or multiple arguments. The use of this particular approach is not recommended as it creates maintenance problems.


2 Answers

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]";  } 
like image 181
Riduidel Avatar answered Sep 18 '22 12:09

Riduidel


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.

like image 37
Johannes Wachter Avatar answered Sep 18 '22 12:09

Johannes Wachter