当前位置: 动力学知识库 > 问答 > 编程问答 >

java - Eclipse compiler says attribute is undefined in case of array of enums field in the annotation

问题描述:

I'm using Spring Security 3.1.0.RC3 because the possibility to secure methods with enum privileges was introduced in 3.1.

Here is my annotation:

public @interface SecuredEnum

{

public PrivilegeEnum[] value();

}

And here is how I use it:

import static somepath1.PrivilegeEnum.SOME_PRIV1;

public interface MyService extends BaseService {

@SecuredEnum(SOME_PRIV1)

void insertOrUpdateMyObject(MyObject myObject);

}

javac compiles this code correctly every time, but eclipse (Indigo SR1) compiler gives an error after Project -> Clean:

The attribute value is undefined for the annotation type SecuredEnum

and suggests a quick fix:

Create attribute 'value()'

So here is my workaround: I apply this fix, and comment out the array field in the annotation:

public @interface SecuredEnum

{

// public PrivilegeEnum[] value();

public PrivilegeEnum value();

}

After that eclipse compiles all classes correctly (except for those where @SecuredEnum takes two or more privileges). Next step is to uncomment the old array field and delete new field created by the quick fix. As a result, everything is compiled perfectly.

So this is pretty annoying to do each time the project is cleaned. Another problem is that I can't reproduce this problem with a sample project, so maybe there is some crucial information that is missing from this question text.

Whos bug is it and how is it possible to fix it?

UPDATE:

neither using explicit array in the annotation argument (i.e. adding { } brackets), nor defining the argument name ('value') explicitly nor using fully qualified enum value instead of static import helps

网友答案:

Could be a problem with the retention policy, which you haven't defined in your custom annotation. So the retention policy defaults to class. It should be runtime, at least that's what the Spring Security guys are using for the @Secured annotation.

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD})    
public @interface SecuredEnum {
    public PrivilegeEnum[] value();
}
网友答案:

With Eclipse Juno (4.2) I just needed to change (insert blank, delete blank) and save the file with the false positive to get rid of the "undefined value" Error.

I have to do this once, every time I change Compiler Settings, but then it is gone until then...

分享给朋友:
您可能感兴趣的文章:
随机阅读: