@ToString manual
Suppose you have a class with many-many fields and no
toString() method:
public class Person {
private String name;
private int age;
private String city;
public Person(String name, int age, String city){
this.name = name;
this.age = age;
this.city = city;
}
}
Generating
toString() to this class is easy: first annotate it with annotation
@ToString() then somehow load it with AnnoJ' custom classloader (
TransformingClassLoader).
The code:
@ToString
public class Person {
//the same as above
}
And one way to load the class:
List tl = Collections.singletonList(new ToStringCreator());
TransformingClassLoader loader = new TransformingClassLoader(tl);
loader.loadClass("Person");
Not that Annoj defines class
MainMethodBootstrap to force all clases of an application to be loaded by this class loader and also custom bootstrap classes can easily be written.
The classloader will define
toString() in
Person so every simple
println() invocation will produce meaningful output (and also every method that relies on
toString()).
But what if you don't want the value of age to be visible in the string representation of
Person? AnnoJ has a solution for you: annotate that field (
age) with
@Exclude. In this case
ToStringCreator will not show the age in the string representation.
If you want all methods of classes in a package be logged you have to define a class named
PackageInfo in that package and annotate it with
@ToString:
@ToString
public class PackageInfo{}