【IDEA】关于IDEA在DEBUG程序时私自调用ToString()方法的问题

发现问题的场景案例

  • 在运行如下的动态代理代码的时候,debug此程序,触发debug断点后,一行行往下执行发现打印的和正常情况不一致
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class Test {
    
    
    public static void main(String[] args) {
    
    
        SellTicketFactory proxy = (SellTicketFactory) ProxyFactory.getInstance(new TrainStation());
        proxy.sell();
    }
}

class ProxyFactory{
    
    

    public static Object getInstance(Object obj){
    
    
        InvocationHandler handler = new MyInvocationHandler(obj);
        return Proxy.newProxyInstance(obj.getClass().getClassLoader(), obj.getClass().getInterfaces(), handler);
    }
}

interface SellTicketFactory{
    
    
    void sell();
}

class TrainStation implements SellTicketFactory{
    
    
    @Override
    public void sell() {
    
    
        System.out.println("火车站卖票...");
    }
}

class MyInvocationHandler implements InvocationHandler{
    
    
    private final Object obj;

    public MyInvocationHandler(Object obj) {
    
    
        this.obj = obj;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    
    
        System.out.println("before.....");
        Object value = method.invoke(obj, args);
        System.out.println("after...");
        return value;
    }
}

在这里插入图片描述

正常运行情况

在这里插入图片描述
在这里插入图片描述

原理

  • IDEA在debug程序时,当debug到某个对象时,会调用对象的toString()方法,用来在debug界面显示对象信息。
  • IDEA调用toString()方法时,即使在toString()方法中设置了断点,该断点也不会被触发,也就是说,开发者多数情况下不会知道toString()方法被调用了
  • 多数情况下调用一下toString()方法没有什么问题,但是也有例外,比如重写了toString()方法的类,随意的调用toString()方法会导致未知的问题。

解决方案

  • IDEA在debug时调用toString()方法的情况是可以在配置中关掉的,相关的配置位置如下图,去掉勾选即可生效
    英文版:
    在这里插入图片描述
    中文版:
    在这里插入图片描述