@XmlElement可以放在哪里?

时间:2014-10-07 20:58:43

标签: jaxb annotations setter

我最近在setter上使用的项目中看到了这个JAXB注释。根据我自己的经验,我知道@XmlElement可以用于属性和getter。我不确定这个注释是否可以而且应该用于制定者,我用Google搜索并且找不到明确的答案。请指教。

2 个答案:

答案 0 :(得分:3)

来自" 8.9属性&字段" JAXB 2.2规范(参见:https://jcp.org/aboutJava/communityprocess/mrel/jsr222/index2.html

  

对于属性,给定的注释可以应用于read或   写属性但不是两者。

换句话说,注释可以放在get或set方法上。根据我的经验,大多数人将注释放在get方法上。

答案 1 :(得分:2)

The code明确表示它确实适用于制定者:

public abstract class AbstractInlineAnnotationReaderImpl<T,C,F,M>
    implements AnnotationReader<T,C,F,M> {
...

    public final <A extends Annotation> A getMethodAnnotation(Class<A> annotation, M getter, M setter, Locatable srcPos) {
        A a1 = getter==null?null:getMethodAnnotation(annotation,getter,srcPos);
        A a2 = setter==null?null:getMethodAnnotation(annotation,setter,srcPos);

        if(a1==null) {
            if(a2==null)
                return null;
            else
                return a2;
        } else {
            if(a2==null)
                return a1;
            else {
                // both are present
                getErrorHandler().error(new IllegalAnnotationException(
                    Messages.DUPLICATE_ANNOTATIONS.format(
                        annotation.getName(), fullName(getter),fullName(setter)),
                    a1, a2 ));
                // recover by ignoring one of them
                return a1;
            }
        }
    }
...
}

但是我也找不到这方面的规范性参考。

相关问题