[英]JAXB: Annotations of field declared in superclass differ in subclasses
I will try to explain the issue with an example: 我将尝试用一个例子来解释这个问题:
Base: 基础:
public abstract class Base {
protected Foo foo;
}
Derived1: Derived1:
//@SomeXMLAnnotations
public class Derived1 extends Base {
//Here i would like to define annotations for foo
@XmlElements({
@XmlElement(name = "foo1", type = Foo1.class),
@XmlElement(name = "foo2", type = Foo2.class)
})
//@AnyAnnoations..
//protected Foo foo;
}
Derived2: Derived2的:
//@SomeXMLAnnotations
public class Derived2 extends Base {
//Here i would like to define annotations for foo too
//But they will differ from the ones defined in Derived1
@XmlElements({
@XmlElement(name = "foo3", type = Foo3.class),
@XmlElement(name = "foo4", type = Foo4.class)
})
//@AnyAnnoations..
//protected Foo foo;
}
The @XmlElements
annotation is just an example. @XmlElements
注释只是一个示例。 It should work with any other annotation too. 它也应该与任何其他注释一起使用。
I know I could shadow the superclass foo
field but I dont think it's a proper way to solve this issue. 我知道我可以遮盖超类foo
字段,但是我认为这不是解决此问题的正确方法。
So is it possible in java (with JAXB) to override/add annotation of/to a field that is declared in a superclass? 那么在Java(使用JAXB)中是否可以重写/向超类中声明的字段添加注释?
"Ugly" solution: “丑陋”的解决方案:
//@SomeXMLAnnotations
public class Derived1 extends Base {
//@AnyAnnoations..
public Foo getFoo() {
return this.foo
}
protected Foo setFoo(Foo foo) {
this.foo = foo;
}
}
Overriding/adding properties and annotating them seems to work. 覆盖/添加属性并对其进行注释似乎可行。 If getter or setter is never used somewhere it's pretty ugly though, because this has to happen in every subclass. 如果没有在任何地方使用getter或setter,那将是非常丑陋的,因为这必须在每个子类中发生。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.