How to remove System.out.println's from codeba

2019-01-23 07:09发布

We have a huge (old legacy java) code-base, where many files (around 5k) have System.out.println's. We are planning to remove them for cleanup/performance reasons. How can we write a script that will replace them without introducing any issues in the code? The script cannot blindly delete them as following case can be an issue:

if ()
  some.code...
else
  System.out.println(...);
DB.close();

I'm thinking of replacing them with ';'. That will take care of above case. Do you see any other issues? Any other suggestions?

8条回答
Lonely孤独者°
2楼-- · 2019-01-23 08:01

Extending Oscar's concept you can do even better IMHO:

if(!DEBUG) {
    System.setOut(
        new PrintStream(new OutputStream() {
            public  void    close() {}
            public  void    flush() {}
            public  void    write(byte[] b) {}
            public  void    write(byte[] b, int off, int len) {}
            public  void    write(int b) {}

        } );
    }
}

In this case, if you are not in debug mode or any other the default system out is replaced internally with devNull implementation, else it works as expected. This way you do not have to find and replace anything in your code.

查看更多
男人必须洒脱
3楼-- · 2019-01-23 08:01

Personally I would use {} instead, but I think it works just the same.

查看更多
登录 后发表回答