vonage-java-sdk icon indicating copy to clipboard operation
vonage-java-sdk copied to clipboard

Add toString method for an easier debugging

Open peterjurkovic opened this issue 7 years ago • 8 comments

Would be nice to have toString() methods across the lib

peterjurkovic avatar Dec 21 '17 14:12 peterjurkovic

This is definitely something we need to get better at.

judy2k avatar Jan 11 '18 10:01 judy2k

I'm preferential to ReflectionToStringBuilder.toString. I know reflection can add a bit of overhead, but ideally toString is only being used for debugging output.

I also like json reflection using either Gson or Jackson. The downside with Jackson is you have to catch the exception:

@Override
public String toString() {
    try {
        return new ObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsString(this);
    } catch (JsonProcessingException jpe) {
        // do nothing we're falling back
    }
    return ReflectionToStringBuilder.reflectionToString(this);
}

Edit: I should add that ObjectMapper is thread safe and could be used statically to prevent multiple instantiation. Even if there was a risk of deadlocking, I'm going under the assumption toString would still be done for debugging purposes.

Something like (I'm really bad at naming things):

public class ToStringUtil {
    private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();

    public static String reflectionToJson(Object object) {
        try {
            return OBJECT_MAPPER.writerWithDefaultPrettyPrinter().writeValueAsString(object);
        } catch (JsonProcessingException jpe) {
            // do nothing we're falling back
        }
        return ReflectionToStringBuilder.reflectionToString(object);
    }
}
// Usage:
@Override
public String toString() {
    return ToStringUtil.reflectionToJson(this);
 }

cr0wst avatar Jan 24 '18 18:01 cr0wst

Have you considered adding Lombok to this project? This would also be a nice option not just for toString, but also as a reduction of boiler plate code for equals and hashCode and possibly more like constructors and so on. (Unless you do not want this, I'll provide a PR as part of the Hacktoberfest so that you can get an idea how this would look like.)

michel-zimmer avatar Sep 30 '20 20:09 michel-zimmer

Feel free to comment on my PR (linked above)

michel-zimmer avatar Oct 01 '20 11:10 michel-zimmer

Yes, I was actually looking into adding Lombok and then completely forgot about it. This looks good. Would you like to continue refactoring this for the rest of the codebase? If so, I could assign this issue to you for you to continue working on it.

yallen011 avatar Oct 05 '20 17:10 yallen011

Sure, I can do that. Is there a way to test this including the JSON de-/serialization? That way I could possibly simplify the code a lot. (Otherwise I don't feel confident enough, because I could break something.)

michel-zimmer avatar Oct 05 '20 17:10 michel-zimmer

We have JUnit tests you can use to make sure existing functionality still works when you change the code. Feel free to add any unit tests you feel you need to add if you don't think the existing test will suffice. Tests are located src/test folder. We use gradle so to run the test you can run ./gradlew build (using the wrapper on a mac) and it will build the code and run the tests or you can do ./gradlew test. If you are running on a windows machine, that command is gradlew.bat build and gradlew.bat test. Let me know if you have any more questions. I am here to help.

yallen011 avatar Oct 06 '20 03:10 yallen011

OK, could you maybe merge the PR? I'd like to build on top of that one, but the PR has been unlinked from my fork and I cannot add new commits.

michel-zimmer avatar Oct 06 '20 06:10 michel-zimmer

Given that the repo has evolved significantly, there are no plans to add Lombok or merge the PR at the moment, but adding toString(), equals(Object) and hashCode() are on the roadmap. The aim is to have every public data model class (i.e. requests and responses) implement these in a standardised manner.

SMadani avatar Nov 30 '23 17:11 SMadani

Done in #508.

SMadani avatar Jan 15 '24 11:01 SMadani