102

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.

Beryllium
  • 12,808
  • 10
  • 56
  • 86
Biju CD
  • 4,999
  • 11
  • 34
  • 55

3 Answers3

141

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]"; 
}
Steve Chambers
  • 37,270
  • 24
  • 156
  • 208
Riduidel
  • 22,052
  • 14
  • 85
  • 185
40

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.

Johannes Wachter
  • 2,715
  • 17
  • 17
0

For custom types you can do something like this

public @interface SomeAnnotation {

  Class<? extends SomeInterface> yourCustomType() default SomeNullInterface.class;
  
}

/**
 * Your custom type
 */
public interface SomeInterface {

}

/**
 * Your fake null value type
 */
public interface SomeNullInterface extends SomeInterface {

}

And somewhere in your code you can then check for null like this

if(!yourAnnotation.yourCustomType().isAssignableFrom(SomeNullInterface.class)){
  //your value is null
}
Marian Klühspies
  • 15,824
  • 16
  • 93
  • 136