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.
The toString method is used to return a string representation of an object. If any object is printed, the toString() method is internally invoked by the java compiler.
A toString() is an in-built method in Java that returns the value given to it in string format. Hence, any object that this method is applied on, will then be returned as a string object.
You can open any method or type in your workspace using the "Open Type" wizard or the "Open Method" wizard. -Or- press Ctrl+Shift+T (for a type) or Ctrl+Shift+M (for a method). The "Open Type/Method" dialog will appear. If a type/method was previously selected in the editor or outline views, it will be displayed.
toString() method returns a string representation of the object. In general, the toString method returns a string that "textually represents" this object. The result should be a concise but informative representation that is easy for a person to read. It is recommended that all subclasses override this 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With