我有大量通过JAXB(maven-jaxb2-plugin
)生成的对象并使用jaxb2-annotate-plugin
注释它们。这些类可能会定义一个RelationType
,我想用相应的@RelationType
注释对它们进行注释。我使用XPath表达式在XSD中查找name属性并注释该类,并将其特定类型传递给注释。这方面的一个例子是:XML&JAXB:将属性传递到值
<jaxb:bindings node="//xsd:complexType[@name='SomeRelationType']">
<annox:annotate target="class">@com.example.RelationType(type = "SomeRelationType")</annox:annotate>
</jaxb:bindings>
它映射以下XSD片段:
<xsd:complexType name="SomeRelationType">
<xsd:complexContent>
<xsd:extension base="RelationType">
<xsd:sequence>
<xsd:element name="someValue" type="SomeValue"/>
<xsd:element name="otherValue" type="OtherValue"/>
</xsd:sequence>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
我找到了SomeRelationType
名的ComplexType并用@RelationType
注释,里面有注释类作为其类型参数的是SomeRelationType
。它会生成以下类别:
@RelationType(type = "SomeRelationType")
public class SomeRelationType extends RelationType implements Serializable {
private final static long serialVersionUID = 1L;
protected SomeValue someValue;
protected OtherValue otherValue;
}
如果它只是几个域对象,这可以正常工作。但是我有很大的数量,并且手动定义每个注释不仅繁琐而且在变化和扩展方面也不好。
泛型化它,我可以重写XPath表达式如下:
<jaxb:bindings node="//xsd:complexType[substring(@name, string-length(@name) - string-length('RelationType') + 1)]" multiple="true">
<annox:annotate target="class">@com.example.RelationType(type = "SomeRelationType")</annox:annotate>
</jaxb:bindings>
问题:我的注释的类型参数仍然定义为"SomeRelationType"
。如果我可以使用与XPath表达式中定义的相同的@name
,那将是非常好的。然后,名称以"RelationType"
结尾的所有课程也会自动获取@RelationType
注释,其中包含正确的type
参数。
它不工作,因为这样做,当然下面一样简单,但它表明想什么,我来实现:
<jaxb:bindings node="//xsd:complexType[substring(@name, string-length(@name) - string-length('RelationType') + 1)]" multiple="true">
<annox:annotate target="class">@com.example.RelationType(type = @name)</annox:annotate>
</jaxb:bindings>
这种事甚至有可能或XML/JAXB是这不可能?