Is there any way to remove all sysout from a Project?

1
I'm setting up a WebService and have some sysout test print variables, and as there are in several different files, I wonder if there is any way to remove all sysout on> project in Eclipse Oxygen at once.

    
asked by anonymous 11.01.2018 / 17:18

2 answers

2

Philip the closest to optimizing this process is you use the find and replace function of your text editor.

Some editors have the function of fetching the text sysout or System.out.println into multiple files at the same time, and you can replace them with a blank text. Doing so removes all.

    
11.01.2018 / 17:23
2

Let's make a class for this:

import java.io.OutputStream;
import java.io.PrintStream;

public final class Silencio {

    public static final OutputStream OUTPUT_STREAM_SILENCIO = new OutputStream() {
        @Override
        public void write(int b) {
            // Não faz nada.
        }
    };

    public static final PrintStream PRINT_STREAM_SILENCIO =
            new PrintStream(OUTPUT_STREAM_SILENCIO);

    private Silencio() {}

    public static void silenciar(Runnable r) {
        PrintStream outOriginal = System.out;
        PrintStream errOriginal = System.err;
        try {
            System.setOut(PRINT_STREAM_SILENCIO);
            System.setErr(PRINT_STREAM_SILENCIO);
            r.run();
        } finally {
            System.setOut(outOriginal);
            System.setErr(errOriginal);
        }
    }
}

There are two objects there that are a OutputStream that discards all output and a PrintStream done with that OutputStream . The silenciar(Runnable) method is the rogue method that shuts down System.out and System.err to execute a Runnable and then restores the original.

Here's a test:

public class TesteSilencio {

    private static int x = 0;

    public static void main(String[] args) {
        System.out.println("Será que vai ter barulho?");
        Silencio.silenciar(() -> {
            System.out.println("Bla bla bla");
            System.err.println("Gwa gwa gwa");
            System.out.println("Bla bla bla");
            System.err.println("Gwa gwa gwa");
            x++;
        });
        System.out.println("Será que o System.out ainda funciona? Vamos tentar de novo!");
        Silencio.silenciar(TesteSilencio::metodoBarulhento);
        System.out.println("Silêncio?");
        System.out.println("Resultado: " + x);
    }

    public static void metodoBarulhento() {
        System.out.println("Bla bla bla");
        System.err.println("Gwa gwa gwa");
        System.out.println("Bla bla bla");
        System.err.println("Gwa gwa gwa");
        x++;
    }
}

Here's the output:

Será que vai ter barulho?
Será que o System.out ainda funciona? Vamos tentar de novo!
Silêncio?
Resultado: 2

The fact that there is a Resultado: 2 also shows that the silenced code was actually executed, but the output of System.out or System.err was not polluted by them.

    
11.01.2018 / 17:42