[英]Alternative for toString() in java
What I have is an unmarshalled java object from XML. 我所拥有的是来自XML的未编组的Java对象。 The parts of this unmarshalled object is sent to various class for processing.
该未编组对象的各部分被发送到各种类进行处理。 I need to log the contents of the unmarshalled object.
我需要记录未编组对象的内容。 I can easily override the
toString()
method but the problem is that in this case the classes are generated at build time using xjc and I cannot override the toString() in those classes as they will be removed when generated again. 我可以轻松地覆盖
toString()
方法,但是问题在于,在这种情况下,此类是使用xjc在生成时生成的,而我无法覆盖那些类中的toString(),因为它们在再次生成时将被删除。
I am looking for some other alternative to print the contents. 我正在寻找其他打印内容的替代方法。 Any Idea ?
任何想法 ?
您可以使用xjc插件生成所需的toString。
Don't use toString()
for marshalling. 不要使用
toString()
进行编组。 That's not what it is intended for. 那不是目的。 (Refer to the javadocs for
java.lang.Object
for a guide to what that method is intended for.) (参考的Javadoc
java.lang.Object
用于指导,以什么方法旨在用于)。
Even if you do decide to implement marshalling / unmarshalling by hand, you should not attempt to use toString()
for this purpose. 即使您决定手动实施编组/解组,也不应为此目的使用
toString()
。
您可以编写一个实用程序类,该实用程序类使用反射来检查对象并返回它的字符串表示形式(所需的任何格式),或者使它直接记录字符串表示形式。
编写一个帮助程序类,该类使用未编组的Java对象并使用反射为您提供字符串表示形式,如果您不想使用反射,因为您知道xsd和未编组的内容,则可以对其进行硬编码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.