Search code examples
javaautomationautomated-refactoring

Drop lines of code from a file


I'm working on a small project in which I need to remove some lines of commented code in java. There are two cases in which I should remove these lines: (i) when the line of code begins with //# and; (ii) when the line of code begins with //@.

However, the method I've developed for this case is not working on either type of removal:

private static void dropLines(String[] args, String f) throws IOException {
    List<String> lines = Files.readAllLines(Paths.get(f), Charset.forName("UTF-8"));
    PrintWriter pw = new PrintWriter(new FileWriter(f));
    boolean empty = false;

    for (String string : lines) {
        if (!string.trim().startsWith("//#")) {
            if (string.trim().equals("") && !empty) {
                pw.println(string);
                empty = true;
            } else if (!string.trim().equals("")) {
                pw.println(string);
                empty = false;
            }
        }
        else if (!string.trim().startsWith("//@")) {
            if (string.trim().equals("") && !empty) {
                pw.println(string);
                empty = true;
            } else if (!string.trim().equals("")) {
                pw.println(string);
                empty = false;
            }
        }
    }

    pw.flush();
    pw.close();
}

An example of an input file could be as follows:

    public class Main {

    //#ifdef calendar
//@ public static void googleCalendar() {
//@     System.out.println("Google Calendar");
//@ }
    //#endif

    //#ifdef category
    public static void addCategory() {
        System.out.println("Add Category");
    }
    //#endif
    public static void main(String[] args) {

        //#ifdef base
//@     System.out.println("Base");
        //#endif

        //#ifdef category
        addCategory();
        // #endif

        //#ifdef calendar
//@     googleCalendar();
        //#endif

    }
}

Important: although the input file has notations similar to preprocessing, this does not have any importance for this phase of removing the lines, because in theory, the preprocessing part is already done in previous methods with the use of Antenna. I need only create a method that deletes the rows after processing.


Solution

  • It looks like you'll want to change your surrounding if/else if statement to be a single if statement:

    private static void dropLines(String[] args, String f) throws IOException {
        List<String> lines = Files.readAllLines(Paths.get(f), Charset.forName("UTF-8"));
        PrintWriter pw = new PrintWriter(new FileWriter(f));
        boolean empty = false;
    
        for (String string : lines) {
            if (!string.trim().startsWith("//#") && !string.trim().startsWith("//@")) {
                if (string.trim().equals("") && !empty) {
                    pw.println(string);
                    empty = true;
                } else if (!string.trim().equals("")) {
                    pw.println(string);
                    empty = false;
                }
            }
        }
    
        pw.flush();
        pw.close();
    }
    

    With the way you have it right now, it's set up to always reprint the line, because if your line begins with "//@" then !string.trim().startsWith("//#") is true, and if it begins with "//#" then !string.trim().startsWith("//@") is true.