Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find all usages of toString() method

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.

like image 792
Andrew Logvinov Avatar asked Jun 05 '13 14:06

Andrew Logvinov


People also ask

What is the use of toString method?

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.

What type of method is toString ()?

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.

How can I see the usage method in eclipse?

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.

What is the purpose of the toString () method in object Why is it useful?

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.


1 Answers

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.

like image 167
Ravi K Thapliyal Avatar answered Oct 07 '22 19:10

Ravi K Thapliyal