如何使 JAXB 生成的类参与访问者模式?
嘿伙计们,希望这里是一个轻松的好地方。
我正在使用 JAXB 从架构生成类,并且我希望能够使用访问者模式来处理它们。
为此,我认为我需要每个 JAXB 生成的类来实现我定义的接口,并向它们添加一个非常简单的方法,因此一个简单的示例是:
默认类:
public class MyClass {
private String name;
public void get/setName() {...}
}
所需的类:
public class MyClass implements MyVisitorNode {
private String name;
public void get/setName() {...}
public void accept(MyVisitorVisitor visitor) {
visitor.visit(this);
}
}
这可能吗?是的,有哪些选择? (更改架构、运行时字节码操作、以某种方式操作 JAXBContext...)
理想情况下,不依赖于特定于供应商的扩展。
谢谢!
Hey folks, hopefully a nice easy one here.
I'm generating classes with JAXB from a schema, and I'd like to be able to process them with a Visitor pattern.
To do that, I think I need every JAXB-generated class to implement the interface I've defined, and add a very simple method to them, so a simple example would be:
Default class:
public class MyClass {
private String name;
public void get/setName() {...}
}
Desired class:
public class MyClass implements MyVisitorNode {
private String name;
public void get/setName() {...}
public void accept(MyVisitorVisitor visitor) {
visitor.visit(this);
}
}
Is this possible, and if it is, what are the options? (Change the schema, runtime bytecode manipulation, manipulate the JAXBContext somehow...)
Ideally, without relying on vendor-specific extensions.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
JAXB 的 xjc 编译器有一个插件接口,允许开发人员创建修改生成代码的插件。我个人最喜欢的是 Fluent-api,但还有其他一些可以添加 toString、equals、hashCode 等。
我使用此技术创建了一个插件来实现访问者模式,并将其作为 google 代码项目提供。它可能不完全是您正在寻找的内容,但如果您需要修改代码以满足您的需求,那么它可能是开始检查代码和测试的好地方。
http://code.google.com/p/jaxb-visitor/
The xjc compiler for JAXB has a plugin interface that allows developers to create plugins that modify the generated code. My personal favorite is the fluent-api but there are others to add toString, equals, hashCode, etc.
I created a plugin using this technology to implement the visitor pattern and made it available as a google code project. It may not be exactly what you're looking for but it might be a good place to start to review the code and tests if you need to modify it to suit your needs.
http://code.google.com/p/jaxb-visitor/
JAX-B 生成的类是标准 Java 类,您可以按照您想要的任何方式对其进行自定义,例如扩展接口、添加其他方法等。类和属性上的注释是编组和反编组过程的驱动因素。
话虽如此,如果您自定义 JAXB 生成的类,则需要考虑一些事情。正如每个类顶部所述“重新编译源架构后对此文件的任何修改都将丢失”。简而言之,如果您自定义该类,则需要手动更改代码以支持任何架构更新。如果您重新生成了类,您的自定义代码将被删除,您将不得不重新开始。
The JAX-B generated classes are standard Java classes that you can customize in any way you desire, e.g., extend interface, add additional methods, etc..). The annotations on the class and attributes are the driving factor for the marshalling and unmarshalling process.
With that said, there are somethings you need to take into account if you customize the JAXB generated classes. As stated at the top of each class "Any modifications to this file will be lost upon recompilation of the source schema". In short, if you customize the class, you will need to manually make code changes to support any schema updates. If you do regenerated the classes, your custom code will be removed and you will have to start all over.