How to create optional parameters for own annotations?

JavaAnnotations

Java Problem Overview


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.

Java Solutions


Solution 1 - Java

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]"; 
}

Solution 2 - Java

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBiju CDView Question on Stackoverflow
Solution 1 - JavaRiduidelView Answer on Stackoverflow
Solution 2 - JavaJohannes WachterView Answer on Stackoverflow