Skip to content

wavesoftware/java-stringify-object

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

52 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Stringify Object for Java

Build Status Quality Gate Coverage Status Maven Central

A utility to safely inspect any Java Object as String representation. It's best to be used with domain model (also with JPA entities) with intention to dump those entities as text to log files.

It runs in two modes: PROMISCUOUS (by default) and QUIET. In PROMISCUOUS mode every defined field is automatically inspected, unless the field is annotated with @DoNotInspect annotation. In QUIET mode only fields annotated with @Inspect will gets inspected.

This library has proper support for object graph cycles, and JPA (Hibernate) lazy loaded elements.

Usage

In Promiscuous mode

// In PROMISCUOUS mode define fields to exclude
class Person {
  private int id;
  @DisplayNull
  private Person parent;
  private List<Person> childs;
  private Account account;
  @Inspect(conditionally = IsInDevelopment.class)
  private String password;
  @DoNotInspect
  private String ignored;
}
  
// inspect an object  
List<Person> people = query.getResultList();  
ObjectStringifier stringifier = new ObjectStringifier(people);  
stringifier.setMode(Mode.PROMISCUOUS);
// stringifier.setBeanFactory(...);
assert "<Person id=15, parent=<Person id=16, parent=null, "  
 + "childs=[(↻)], account=⁂Lazy>, childs=[], "  
 + "account=⁂Lazy>".equals(stringifier.toString());  

In Quiet mode

// In QUIET mode define fields to inspect  
class Person {  
  @Inspect private int id;
  @Inspect @DisplayNull private Person parent;
  @Inspect private List<Person> childs;
  @Inspect private Account account;
  private String ignored;
}
  
// inspect an object  
List<Person> people = query.getResultList();  
ObjectStringifier stringifier = new ObjectStringifier(people);  
stringifier.setMode(Mode.QUIET);
assert "<Person id=15, parent=<Person id=16, parent=null, "  
 + "childs=[(↻)], account=⁂Lazy>, childs=[], "  
 + "account=⁂Lazy>".equals(stringifier.toString());  

Features

  • String representation of any Java class in two modes PROMISCUOUS and QUIET
  • Fine tuning of which fields to display
  • Support for cycles in object graph - (↻) is displayed instead
  • Support for Hibernate lazy loaded entities - ⁂Lazy is displayed instead

vs. Lombok @ToString

Stringify Object for Java is designed for slightly different use case then Lombok.

Lombok @ToString is designed to quickly inspect fields of simple objects by generating static simple implementation of this mechanism.

Stringify Object for Java is designed to inspect complex objects that can have cycles and can be managed by JPA provider like Hibernate (introducing Lazy Loading problems).

Pros of Lombok vs Stringify Object

  • Lombok is fast - it's statically generated code without using Reflection API.
  • Lombok is easy - it's zero configuration in most cases.

Cons of Lombok vs Stringify Object

  • Lombok can't detect cycles is object graph, which implies StackOverflowException being thrown in that case
  • Lombok can't detect a lazy loaded entities, which leads to force loading it from JPA by invoking SQL statements. It's typical n+1 problem, but with nasty consequences - your toString() method is invoking SQL without your knowledge!!

Dependencies

Contributing

Contributions are welcome!

To contribute, follow the standard git flow of:

  1. Fork it
  2. Create your feature branch (git checkout -b feature/my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin feature/my-new-feature)
  5. Create new Pull Request

Even if you can't contribute code, if you have an idea for an improvement please open an issue.

About

A utility to safely inspect any Java Object as string representation

Topics

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages