使用xmlbeans时使用反射创建实例
我有一个 xsd 文件,其中定义了 100 多种类型。我使用 xmlbeans 绑定生成了 java 代码,然后我可以使用它
MyType.Factory.newInstance();
来获取类实例。但由于类型太多,我决定使用反射来为每种类型创建实例。
我现在可以使用获取 MyType 的类(接口),
Class clz = Class.forName("com.foo.MyType");
但不知道如何获取 MyType 接口中定义的 Factory 类,然后获取新实例。
任何评论或提示表示赞赏。提前致谢。
I've got a xsd file with over 100 types defined in it. I generated java code with xmlbeans binding, and then I can use
MyType.Factory.newInstance();
to get the class instance. But since there are too many types I've decide to use reflection to get create instances for each type.
I can now get the class(interface) of MyType using
Class clz = Class.forName("com.foo.MyType");
But have no idea of how to get the Factory class defined in the MyType interface and then get the new instance.
Any comment or hint is appreciated. Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您必须使用二进制名称来引用内部类:com.foo.MyType$Factory
you have to use the binary name to refer to inner classes: com.foo.MyType$Factory
那么,您在接口
MyType
中声明了一个内部类Factory
吗?如果我有这个权利,并且如果Factory
是“MyType”声明的唯一成员类,并且如果我理解您要查找的内容,那么以下内容应该有效。编辑:测试了我的答案,它有效。我不知道 Pangea 提到的技术,但这也有效,而且比我的答案更好。代码看起来像这样。
So, you have an inner class
Factory
declared inside your interfaceMyType
? If I have that right, and ifFactory
is the only member class declared by `MyType', and if I understand what you're looking for, then the following should work.Edit: Tested my answer out, and it works. I wasn't aware of the technique Pangea mentioned, but that works, too, and it's better than my answer. The code would look like this.