Error setting a default null value for an annotation's field

前端 未结 7 2006
渐次进展
渐次进展 2020-11-30 08:20

Why am I getting an error \"Attribute value must be constant\". Isn\'t null constant???

@Target(ElementType.TYPE)
@Retention(RetentionPolicy         


        
7条回答
  •  南方客
    南方客 (楼主)
    2020-11-30 08:56

    It would seem this is illegal, although the JLS is very fuzzy on this.

    I wracked my memory to try and think of an existing annotation out there that had a Class attribute to an annotation, and remembered this one from the JAXB API:

    @Retention(RUNTIME) @Target({PACKAGE,FIELD,METHOD,TYPE,PARAMETER})        
    public @interface XmlJavaTypeAdapter {
        Class type() default DEFAULT.class;
    
        static final class DEFAULT {}    
    }
    

    You can see how they've had to define a dummy static class to hold the equivalent of a null.

    Unpleasant.

提交回复
热议问题