cdevents/sdk-java

Should use lombok for generating `toString` and `hashCode`

Opened this issue · 6 comments

xibz commented

Right now model classes do not override these methods making, equals, a little more complicated. Lombok can make this easy.

I am using a maven plugin jsonschema2pojo here to generate the model classes, not sure we can use Lombok with jsonschema2pojo

Today, here is the configuration to generate toStringand hashCode using this plugin - https://github.com/cdevents/sdk-java/blob/main/generator/pom.xml#L92-L93

xibz commented

If you couldn't do this, Id be very questionable about the library

joelittlejohn/jsonschema2pojo#524 but it looks like you can

If you couldn't do this, Id be very questionable about the library

That is not a nice thing to say. Please be respectful.

As @rjalander noted, generated code via schema2pojo does add equals and hashCode but toString is currently disabled. See

<configuration>
<outputDirectory>${sdk.project.dir}/src/main/java</outputDirectory>
<includeHashcodeAndEquals>true</includeHashcodeAndEquals>
<includeToString>false</includeToString>
<addCompileSourceRoot>false</addCompileSourceRoot>
<targetVersion>8</targetVersion>
<includeGeneratedAnnotation>true</includeGeneratedAnnotation>
</configuration>

@rjalander perhaps <includeToString> may be set to true?

Also, @xibz, even though I'm a fan of Lombok it doesn't make sense to add it to this project, nor any other source code generator driven by an annotation processor because this project uses its own source code generator.

I did not include toString because in these unit tests, I am comparing the expected and actual objects instead of strings for the generated events

toString is also useful for logging. Usually it's not a good idea to compare equality using literals, that is why Java has the equals method 😏

Yes the equals methods is implemented to compare the content of the objects in all the generated models, so it should produce the same result when comparing with objects too.

assertTrue(expectedEvent.getContext().equals(createdEvent.getContext()));
OR
assertEquals(expectedEvent.getContext(), createdEvent.getContext());

But I think we can still implement toString() method too, as it invokes only equals method in either case unless explicitly invoked.