I have a huge project with a class that is widely used everywhere inside this project. This class defines toString()
method which outputs a lot of information. I want to define another method, say, toShortString()
and replace all of the occurrences where original toString()
is called with this method call.
The problem is that there is a lot of code that looks like follows:
log.debug("Order issued: " + order);
log.debug("Loaded list of orders: " + orders);
where order
is instance of this object and orders
is a list of such objects.
Is there any way to find all such occurrences?
Any suggestions are welcome. IDE is IntelliJ Idea, if it matters.
Simply override the toString()
method body in your Order
class.
Technically it is not possible to find all calls, because even system libraries call toString()
in many places, like all kind of collections. Also you should pay attention to your templates (whatever GUI you are using.)
So, you want to log the short printout, and debug the full (the original). Both are calling toString()
. Then you could try to peek inside the calling stack trace to decide where is it called from. Use Thread.currentThread().getStackTrace()
to access the current stack trace.
Say, if any of the last 10 stacktrace elements is from you Log
class, it is called for logging, then you can print the short printout. Otherwise do the full printout.
Yes, it is good practice to move the different versions of toString()
into separate methods.