Find all usages of toString() method

Instead of replacing all the occurences of toString() which would be error prone (you would definitely miss some) and some really difficult to replace (for example, the System.out.println() on a List of Order objects would always invoke toString() only) I suggest you modify the toString() itself to call toShortString().

Move all the code inside toString() to another function called toLongString() and then use this function where you feel the need to have a detailed String representation of Order objects.


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.


Here's a way to find all explicit (will not find the 2 examples you showed) toString() calls in IDEA:

  1. Inside your class mark toString method as @Deprecated.
  2. Analyze -> Run inspection by name -> select Deprecated API usage.

It will list all usages of any deprecated APIs which of course includes the toString you just annotated. Don't forget to remove the annotation.