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

Asked

Viewed 93 times

1

I’m riding a Webservice and there’s some sysout test to print the variables, and as there are in several different files, I would like to know if there is any way to remove all the sysout of the project in Eclipse Oxygen at once.

  • If it’s the shortcut to calling the method System.out.println I believe you deactivate in preferences.

2 answers

2


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

some editors have function to search the text sysout or System.out.println in several files at the same time, and you can replace them with a blank text. Thus doing the removal of all.

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 one OutputStream that discards the entire exit and a PrintStream done with that OutputStream. The method silenciar(Runnable) is the rogue method that shuts the mouth of System.out and the System.err to execute a Runnable and restores the original after.

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 way out:

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 evidences that the silenced code was actually executed, but without the output of the System.out or System.err be polluted by them.

  • From what I understand in this code, it’s not exactly what I wanted, it’s just silencing. I wanted to make the code cleaner by removing the system.out quickly

  • I thought you were allowed to write variables System.out and System.err. Or the setter also provides for other trickery?

  • 1

    @Jeffersonquesado I’m using the methods setOut and setErr to do this. Obviously if there is a SecurityManager that does not grant permission to do so, the result will be a SecurityException.

  • 1

    @Jeffersonquesado If the idea is to do System.out = PRINT_STREAM_SILENCIO, This does not compile because the out and the err are with the modifier final.

  • @Victorstafusa had not noticed this detail. I was going to come across a compilation error and would not know how to bypass it.

  • I still don’t quite understand the code, it just silences the print ?

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.