|
1 | | - |
2 | | -# Stringify Object for Java |
3 | | - |
4 | | -[](https://travis-ci.org/wavesoftware/java-stringify-object) [](https://sonar.wavesoftware.pl/dashboard/index/pl.wavesoftware.utils:stringify-object) [](https://coveralls.io/github/wavesoftware/java-stringify-object?branch=master) |
5 | | -[](https://bintray.com/bintray/jcenter/pl.wavesoftware.utils%3Astringify-object) |
6 | | - |
7 | | - |
8 | | -A utility to safely inspect any Java Object as String representation. It's best to be used with JPA entity model to be dumped |
9 | | -to log files. |
10 | | - |
11 | | -It utilize `@Inspect` annotation to indicate which fields |
12 | | - should be displayed. It has support for cycles, and Hibernate lazy loaded elements. |
13 | | - |
14 | | - ## Usage |
15 | | - |
16 | | -```java |
17 | | -// define fields to inspect |
| 1 | +# Stringify Object for Java |
| 2 | + |
| 3 | +[](https://travis-ci.org/wavesoftware/java-stringify-object) [](https://sonar.wavesoftware.pl/dashboard/index/pl.wavesoftware.utils:stringify-object) [](https://coveralls.io/github/wavesoftware/java-stringify-object?branch=master) [](https://bintray.com/bintray/jcenter/pl.wavesoftware.utils%3Astringify-object) |
| 4 | + |
| 5 | + |
| 6 | +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. |
| 7 | + |
| 8 | +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. |
| 9 | +In `QUIET` mode only fields annotated with `@Inspect` will gets inspected. |
| 10 | + |
| 11 | +This library has proper support for object graph cycles, and JPA (Hibernate) lazy loaded elements. |
| 12 | + |
| 13 | + ## Usage |
| 14 | + |
| 15 | + ### In Promiscuous mode |
| 16 | + |
| 17 | +```java |
| 18 | +// In PROMISCUOUS mode define fields to exclude |
18 | 19 | class Person { |
| 20 | + private int id; |
| 21 | + @DisplayNull |
| 22 | + private Person parent; |
| 23 | + private List<Person> childs; |
| 24 | + private Account account; |
| 25 | + @Inspect(conditionally = IsInDevelopment.class) |
| 26 | + private String password; |
| 27 | + @DoNotInspect |
| 28 | + private String ignored; |
| 29 | +} |
| 30 | + |
| 31 | +// inspect an object |
| 32 | +List<Person> people = query.getResultList(); |
| 33 | +ObjectStringifier stringifier = new ObjectStringifier(people); |
| 34 | +stringifier.setMode(Mode.PROMISCUOUS); |
| 35 | +// stringifier.setBeanFactory(...); |
| 36 | +assert "<Person id=15, parent=<Person id=16, parent=null, " |
| 37 | + + "childs=[(↻)], account=⁂Lazy>, childs=[], " |
| 38 | + + "account=⁂Lazy>".equals(stringifier.toString()); |
| 39 | +``` |
| 40 | + |
| 41 | + ### In Quiet mode |
| 42 | +```java |
| 43 | +// In QUIET mode define fields to inspect |
| 44 | +class Person { |
19 | 45 | @Inspect private int id; |
20 | | - @Inspect private Person parent; |
| 46 | + @Inspect @DisplayNull private Person parent; |
21 | 47 | @Inspect private List<Person> childs; |
22 | 48 | @Inspect private Account account; |
| 49 | + private String ignored; |
23 | 50 | } |
24 | | - |
25 | | -// inspect an object |
26 | | -List<Person> people = query.getResultList(); |
27 | | -ObjectStringifier stringifier = new ObjectStringifier(people); |
28 | | -assert "<Person id=15, parent=<Person id=16, parent=null, " |
29 | | - + "childs=[(↻)], account=⁂Lazy>, childs=[], " |
30 | | - + "account=⁂Lazy>".equals(stringifier.toString()); |
31 | | -``` |
32 | | - |
33 | | -## Features |
34 | | - |
35 | | - * String representation of any Java class |
36 | | - * Fine tuning of which fields to display |
37 | | - * Support for cycles in object graph - `(↻)` is displayed instead |
38 | | - * Support for Hibernate lazy loaded entities - `⁂Lazy` is displayed instead |
39 | | - |
40 | | -## vs. Lombok @ToString |
41 | | - |
42 | | -Stringify Object for Java is designed for **slightly different** use case then Lombok. |
43 | | - |
44 | | -Lombok `@ToString` is designed to quickly inspect fields of simple objects by generating static simple implementation of this mechanism. |
45 | | - |
46 | | -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). |
47 | | - |
48 | | -#### Pros of Lombok vs Stringify Object |
49 | | - |
50 | | - * Lombok is **fast** - it's statically generated code without using Reflection API. |
51 | | - * Lombok is **easy** - it's zero configuration in most cases. |
52 | | - |
53 | | -#### Cons of Lombok vs Stringify Object |
54 | | - |
55 | | - * Lombok can't **detect cycles** is object graph, which implies `StackOverflowException` being thrown in that case |
56 | | - * 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!! |
57 | | - |
58 | | -## Requirements |
59 | | - |
60 | | - * JDK >= 7 |
61 | | - * [EID Exceptions](https://github.com/wavesoftware/java-eid-exceptions) |
62 | | - |
63 | | -### Contributing |
64 | | - |
65 | | -Contributions are welcome! |
66 | | - |
67 | | -To contribute, follow the standard [git flow](http://danielkummer.github.io/git-flow-cheatsheet/) of: |
68 | | - |
69 | | -1. Fork it |
70 | | -1. Create your feature branch (`git checkout -b feature/my-new-feature`) |
71 | | -1. Commit your changes (`git commit -am 'Add some feature'`) |
72 | | -1. Push to the branch (`git push origin feature/my-new-feature`) |
73 | | -1. Create new Pull Request |
74 | | - |
| 51 | + |
| 52 | +// inspect an object |
| 53 | +List<Person> people = query.getResultList(); |
| 54 | +ObjectStringifier stringifier = new ObjectStringifier(people); |
| 55 | +stringifier.setMode(Mode.QUIET); |
| 56 | +assert "<Person id=15, parent=<Person id=16, parent=null, " |
| 57 | + + "childs=[(↻)], account=⁂Lazy>, childs=[], " |
| 58 | + + "account=⁂Lazy>".equals(stringifier.toString()); |
| 59 | +``` |
| 60 | + |
| 61 | +## Features |
| 62 | + |
| 63 | + * String representation of any Java class in two modes `PROMISCUOUS` and `QUIET` |
| 64 | + * Fine tuning of which fields to display |
| 65 | + * Support for cycles in object graph - `(↻)` is displayed instead |
| 66 | + * Support for Hibernate lazy loaded entities - `⁂Lazy` is displayed instead |
| 67 | + |
| 68 | +## vs. Lombok @ToString |
| 69 | + |
| 70 | +Stringify Object for Java is designed for **slightly different** use case then Lombok. |
| 71 | + |
| 72 | +Lombok `@ToString` is designed to quickly inspect fields of simple objects by generating static simple implementation of this mechanism. |
| 73 | + |
| 74 | +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). |
| 75 | + |
| 76 | +#### Pros of Lombok vs Stringify Object |
| 77 | + |
| 78 | + * Lombok is **fast** - it's statically generated code without using Reflection API. |
| 79 | + * Lombok is **easy** - it's zero configuration in most cases. |
| 80 | + |
| 81 | +#### Cons of Lombok vs Stringify Object |
| 82 | + |
| 83 | + * Lombok can't **detect cycles** is object graph, which implies `StackOverflowException` being thrown in that case |
| 84 | + * 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!! |
| 85 | + |
| 86 | +## Dependencies |
| 87 | + |
| 88 | + * Java >= 7 |
| 89 | + * [EID Exceptions](https://github.com/wavesoftware/java-eid-exceptions) library |
| 90 | + |
| 91 | +### Contributing |
| 92 | + |
| 93 | +Contributions are welcome! |
| 94 | + |
| 95 | +To contribute, follow the standard [git flow](http://danielkummer.github.io/git-flow-cheatsheet/) of: |
| 96 | + |
| 97 | +1. Fork it |
| 98 | +1. Create your feature branch (`git checkout -b feature/my-new-feature`) |
| 99 | +1. Commit your changes (`git commit -am 'Add some feature'`) |
| 100 | +1. Push to the branch (`git push origin feature/my-new-feature`) |
| 101 | +1. Create new Pull Request |
| 102 | + |
75 | 103 | Even if you can't contribute code, if you have an idea for an improvement please open an [issue](https://github.com/wavesoftware/java-stringify-object/issues). |
0 commit comments