views:

1138

answers:

2

I defined my own custom annotation

@Target(value={ElementType.METHOD, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface MyCustomAnnotation  {
    Class<?> myType();
}

how, if at all, can I make the attribute optional

+7  A: 

You can provide a default value for the attribute:

@Target(value={ElementType.METHOD, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface MyCustomAnnotation  {
    Class<?> myType() default Object.class;
}
Dan Dyer
found it before you, but I think it is fair to select your answer
flybywire
+1  A: 

Found it. It can't be optional, but a default can be declared like this:

@Target(value={ElementType.METHOD, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface MyCustomAnnotation  {
    Class<?> myType() default String.class;
}

If no default can make sense as "empty" value then that is a problem.

flybywire