Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Annotations with optional attributes

I have an annotation like this:

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.METHOD, ElementType.TYPE})
public @interface MyAnnotation {
  String  name();

  Class<InstanceConverter> converter();

What I'm trying to do is make name required and converter optional. It appears that all attributes of an annotation are required by default. How do I make converter optional?

I've read through two articles on annotations and none seem to mention optional attributes.

Thanks.

like image 372
Thom Avatar asked Sep 17 '12 12:09

Thom


1 Answers

You should add a default clause at the right side of the field declaration statement in the annotation @interface definition:

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.METHOD, ElementType.TYPE})
public @interface MyAnnotation {

  String  name(); // mandatory

  Class<InstanceConverter> converter() default InstanceConverter.class; // optional
}
like image 134
Alexandre Dupriez Avatar answered Oct 19 '22 16:10

Alexandre Dupriez