简体   繁体   English

查找 toString() 方法的所有用法

[英]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.该类定义了输出大量信息的toString()方法。 I want to define another method, say, toShortString() and replace all of the occurrences where original toString() is called with this method call.我想定义另一个方法,比如toShortString()并用这个方法调用替换所有调用原始toString()地方。

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.其中order是此对象的实例,而orders是此类对象的列表。

Is there any way to find all such occurrences?有没有办法找到所有这些事件?

Any suggestions are welcome.欢迎任何建议。 IDE is IntelliJ Idea, if it matters. IDE 是 IntelliJ Idea,如果它很重要的话。

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() .而不是替换所有出现的toString()容易出错(你肯定会错过一些)和一些很难替换的(例如, Order对象List上的System.out.println()总是会调用toString() only) 我建议你修改toString()本身来调用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.toString()所有代码移动到另一个名为toLongString()函数中,然后在您觉得需要对Order对象进行详细的String表示时使用该函数。

Simply override the toString() method body in your Order class.只需覆盖Order类中的toString()方法主体。

Technically it is not possible to find all calls, because even system libraries call toString() in many places, like all kind of collections.从技术上讲,不可能找到所有调用,因为即使是系统库也会在很多地方调用toString() ,就像所有类型的集合一样。 Also you should pay attention to your templates (whatever GUI you are using.)此外,您应该注意您的模板(无论您使用什么 GUI。)

So, you want to log the short printout, and debug the full (the original).因此,您想要记录简短的打印输出,并调试完整的(原始)。 Both are calling toString() .两者都在调用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.使用Thread.currentThread().getStackTrace()访问当前堆栈跟踪。

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.比如说,如果最后 10 个 stacktrace 元素中的任何一个来自您的Log类,则调用它进行日志记录,然后您可以打印简短的打印输出。 Otherwise do the full printout.否则,请进行完整的打印输出。

Yes, it is good practice to move the different versions of toString() into separate methods.是的,将不同版本的toString()到单独的方法中是一种很好的做法。

Here's a way to find all explicit (will not find the 2 examples you showed) toString() calls in IDEA:这是一种在 IDEA 中查找所有显式(不会找到您展示的 2 个示例) toString()调用的方法:

  1. Inside your class mark toString method as @Deprecated .在您的类@Deprecated toString方法标记为@Deprecated
  2. Analyze -> Run inspection by name -> select Deprecated API usage . Analyze -> Run inspection by name -> 选择Deprecated API usage

It will list all usages of any deprecated APIs which of course includes the toString you just annotated.它将列出任何已弃用的 API 的所有用法,其中当然包括您刚刚注释的toString Don't forget to remove the annotation.不要忘记删除注释。

In 2021 Intellij can find "implicit calls". 2021 年 Intellij 可以找到“隐式调用”。 It is an option of "Find usages settings" action (can be found by pressing double-shift).它是“查找用法设置”操作的一个选项(可以通过按双移找到)。

This will help you to find occurrences like:这将帮助您找到以下事件:

log.debug("Order issued: " + order);

but unfortunately won't find collection's transformation to string:但不幸的是找不到集合到字符串的转换:

log.debug("Loaded list of orders: " + orders);

查找用法设置

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何查找特定类型的toString()方法的隐式用法 - How to find implicit usages of toString() method for specific type 如何在IntelliJ Idea中找出方法的所有用法(直接用法,通过某些库的传递用法)? - How to find out all usages of a method (direct usage, transitive usage through some library) in IntelliJ Idea? Intellij从方法中按包查找用法 - Intellij find usages by package from method IntelliJ或Eclipse将方法的所有用法重构为不同类/接口上的方法 - IntelliJ or Eclipse refactoring all usages of a method to a method on a different class/interface Java IDE - 查找函数或类的所有INDIRECT用法/引用? - Java IDE - find all INDIRECT usages/references of a function or class? 如何在整个调用图中找到实例变量对象的所有用法? - How to find all the usages to instance variable object throughout call graph? 在IntelliJ中,是否可以找到具有特定类型的类型化方法的用法? - In IntelliJ, is it possible to find usages of a typed method with a specific type? 仅在 Intelij-Idea 中查找指定类的方法用法 - Find Method usages only for specified class in Intelij-Idea 如何在eclipse中找到方法实现的用法? - How do I find usages of method implementation in eclipse? 如何找到接口方法的一个实现的用法 - How to find usages of ONE implementation of an interface's method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM