In our project certain data classes (POJO used for API Request & Response)
are overridding the toString()
method to provide meaningful information - but certain data classes are not overridden toString()
.
When application print the logs
of data class in which toString()
is not overridden, they are not printing meaningful information they just call object class toString()
.
So we want to identify those data class and provide toString()
implementation
.
Is there any way to identify those kind of class in which toString()
method is not implemented
.
Looking in every data class and checking for toString()
method is tedious and time consuming task.
Is there any better way to do that using some tools like eclipse or something else?
One way is to use Reflections Library to obtain all classes within a given package, note that this does not work on classes like anonymous, private, partial, inaccessible, ..etc, so the best way is to do it manually as per @GhostCat answer
Now should you go this route, here's how it could be done, first obtain classes through Reflections Library, quoted from this answer by @Staale
Reflections reflections = new Reflections("my.project.prefix"); Set<Class<? extends Object>> allClasses = reflections.getSubTypesOf(Object.class);
Then iterate over the classes, and check if toString is declared in the class itself
for(Class clazz : allClasses)
{
if(!clazz.getMethod("toString").getDeclaringClass().getName().equals(clazz.getName()))
System.out.println("toString not overridden in class "+clazz.getName());
}