3

I have this Checkstyle block to stop people from printing to console in my project:

<module name="Regexp">
    <property name="format" value="((System\.|(out|err)\.print)|printStackTrace)"/>
    <property name="ignoreComments" value="true"/>
    <property name="illegalPattern" value="true"/>
    <property name="message" value="No printing to console, use a logger."/>
</module>

Where the regex is ((System\.|(out|err)\.print)|printStackTrace)

However, someone snuck this in: out.println by importing import static java.lang.System.out;.

So I updated the regex to: ((System\.(out|err))|printStackTrace|(out|err)\.(print|write|append))

My question is: does this regex cover all ways to print to console in Java? Does anyone have a better regex that they use in Checkstyle?

wilmol
  • 1,429
  • 16
  • 22

1 Answers1

1

If you don't want people to use System.out.println, then the best thing you can do is to argue their job is in danger and menace them with other countermeasures. (Please, I'm joking, never do this) :)

They can just do something like

import static java.lang.System.out;

public class App 
{
    public interface wvs {
        void abc(Object o);
    }

    static final wvs bwz = out::println;  /* this is the only reference to 
                                           * println, but you can hide it 
                                           * in another part of the 
                                           * application */

    static void blah(wvs o, Object oo)
    {
        o.abc( oo );
    }

    public static void main( String[] args )
    {
        System.out.println( "Hello World!" ); /* this will be filtered */
        blah(bwz, "another message by courtesy of LC"); /* will this? */
    }
}

and get you crazy looking for calls to System.out.println(). You can even write a Logger class to print to the console bypassing the Logger configuration. :)

As a hint, Never try to prove the human intelligence by force, you'll lose. As a good manager, try to convince your people to act as expected. Convince them of the benefits of using Loggers, but never try them by brute force.

try to offer free beer to them if you see no console output from your application in one month... That works almost always.

Luis Colorado
  • 10,974
  • 1
  • 16
  • 31