Error setting a default null value for an annotati

2019-01-17 19:01发布

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

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface SomeInterface {
    Class<? extends Foo> bar() default null;// this doesn't compile
}

6条回答
放我归山
2楼-- · 2019-01-17 19:22

Try this

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface SomeInterface {
    Class bar() default void.class;
}

It does not require a new class and it is already a keyword in Java that means nothing.

查看更多
我想做一个坏孩纸
3楼-- · 2019-01-17 19:23

It seem there is one other way of doing it.

I don't like this either, but it might work.

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface SomeInterface {
    Class<? extends Foo>[] bar() default {};
}

So basically you create an empty Array instead. This seems to allow a default value.

查看更多
smile是对你的礼貌
4楼-- · 2019-01-17 19:23
Class<? extends Foo> bar() default null;// this doesn't compile

As mentioned, the Java language specification does not allow null values in the annotations defaults.

What I tend to do is to do is to define DEFAULT_VALUE constants at the top of the annotation definition. Something like:

public @interface MyAnnotation {
    public static final String DEFAULT_PREFIX = "__class-name-here__ default";
    ...
    public String prefix() default DEFAULT_PREFIX;
}

Then in my code I do something like:

if (myAnnotation.prefix().equals(MyAnnotation.DEFAULT_PREFIX)) { ... }

In your case with a class, I would just define a marker class. Unfortunately you can't have a constant for this so instead you need to do something like:

public @interface MyAnnotation {
    public static Class<? extends Foo> DEFAULT_BAR = DefaultBar.class;
    ...
    Class<? extends Foo> bar() default DEFAULT_BAR;
}

Your DefaultFoo class would just an empty implementation so that the code can do:

if (myAnnotation.bar() == MyAnnotation.DEFAULT_BAR) { ... }

Hope this helps.

查看更多
我想做一个坏孩纸
5楼-- · 2019-01-17 19:30

I don't know why, but the JLS is very clear:

 Discussion

 Note that null is not a legal element value for any element type. 

And the definition of a default element is:

     DefaultValue:
         default ElementValue

Unfortunately I keep finding that the new language features (Enums and now Annotations) have very unhelpful compiler error messages when you don't meet the language spec.

EDIT: A little googleing found the following in the JSR-308, where they argue for allowing nulls in this situation:

We note some possible objections to the proposal.

The proposal doesn't make anything possible that was not possible before.

The programmer-defined special value provides better documentation than null, which might mean “none”, “uninitialized”, null itself, etc.

The proposal is more error-prone. It's much easier to forget checking against null than to forget checking for an explicit value.

The proposal may make the standard idiom more verbose. Currently only the users of an annotation need to check for its special values. With the proposal, many tools that process annotations will have to check whether a field's value is null lest they throw a null pointer exception.

I think only the last two points are relevant to "why not do it in the first place." The last point certainly brings up a good point - an annotation processor never has to be concerned that they will get a null on an annotation value. I tend to see that as more the job of annotation processors and other such framework code to have to do that kind of check to make the developers code clearer rather than the other way around, but it would certainly make it hard to justify changing it.

查看更多
beautiful°
6楼-- · 2019-01-17 19:37

That error message is misleading, but, no, the null literal is not a constant expression, as defined in the Java Language Specification, here.

Moreover, the Java Language Specification says

It is a compile-time error if the type of the element is not commensurate (§9.7) with the default value specified.

And to explain what that means

It is a compile-time error if the element type is not commensurate with the element value. An element type T is commensurate with an element value V if and only if one of the following is true:

  • [...]
  • V is not null.

So here, your element type, Class<? extends Foo>, is not commensurate with the default value, because that value is null.

查看更多
太酷不给撩
7楼-- · 2019-01-17 19:43

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.

查看更多
登录 后发表回答