highsource / jaxb2-basics

Useful plugins and tools for JAXB2.
BSD 2-Clause "Simplified" License
109 stars 54 forks source link

Add a SimpleToString option #82

Closed mattrpav closed 9 months ago

mattrpav commented 7 years ago

The goal would be to have a deep (as possible), reflection-free, runtime-free option similar to the SimpleEquals and SimpleHashcode for toString method creation.

ja6a commented 7 years ago

+1

andrei-ivanov commented 6 years ago

I see there is a SimpleToStringPlugin, but it was disabled by the author at some point. @highsource Can it be enabled again please? Or is it somehow broken?

highsource commented 6 years ago

@andrei-ivanov There's nothing to enable, it was never implemented. The class is a copy-paste of ToStringPlugin.

The problem with simple toString plugin is that it is very cumbersome to implement it in a non-strategic way. Basically I'd have to generate all the "to string" code contained in the strategies into the schema-derived classes. And "to string" strategies are ca. 3-4x larger than, say, the hash code strategy.

At the same time I don't quite understand what is the problem with an additional runtime dependency. I mean, OK, it is better without the runtime dep, but for the price of ugly code in the toString method? I really don't know.

mattrpav commented 6 years ago

I was thinking the deep-as-possible is probably the wrong approach. A shallow inspection of only immediate member primitive and boxes primitives fields would cover a large number of use cases and allow for a depedency-free model jar. Scenarios that need more than first-level could then opt for the toString strategy and the runtime dependency.

Ie only “to string” member fields of these types:

String int / Integer short / Short long / Long double / double float / Float boolean / Boolean etc

highsource commented 6 years ago

@mattrpav This would be much easier but to be honest this is very limited. Would not even cover collections or JAXBElement<T>.

But I'll think about it.

mattrpav commented 6 years ago

@highsource gotcha. Perhaps, for collections a suitable solution for class:

public class MyFoo { private List<?> items... }

The toString on the collection could be:

items != null ? "items: [" + items.size() + "]" : "items: [null]"

I'm fine w/ no support for JAXBElement. I think the idea is to have an option for a bunch of easy use cases to dodge the runtime requirement, but understand that there are corner cases that cause it to jump to the full ToStringStrategy. I could see us using both. We leverege extending the ToStringStrategy to do password and other secret masking, I wouldn't expect that type of functionality in a SimpleString solution.

laurentschoelens commented 11 months ago

Follow up issue : https://github.com/highsource/jaxb-tools/issues/433

laurentschoelens commented 9 months ago

Closing in favor of follow up issue in jaxb-tools