morristech
8/8/2018 - 9:23 PM

HungarianNotation.java

//setup
// dir for script to work in
String DIR = "/your/project/dir";

// character that have to occur before target variable
List<Character> charactersBeforeVariable = Arrays.asList(
        ' ',
        '(',
        '.',
        '!',
        '-',
        ',',
        '[',
        '=',
        '*',
        '+',
        ';',
        '{',
        '|',
        '>',
        '<',
        '}',
        ')'
);

// variable prefixes(mVariable and sStatic)
List<String> prefixes = Arrays.asList("m", "s");

// main function
public void removeHungarianNotation() throws Exception {
    File targetDir = new File(DIR);
    if(!targetDir.exists())
        throw new IllegalArgumentException("Given directory " + DIR + " does not exist");
    // all files in dir
    Collection<File> files = getAllFilesInDir(targetDir);

    // files we will work on
    Collection<File> filteredFiles = files.stream()
            // filter out unwanted files
            .filter(file -> {
                String absolutePath = file.getAbsolutePath();
                return absolutePath.endsWith(".java") &&
                        !absolutePath.contains("build") &&
                        !absolutePath.contains(".git");
            })
            .collect(Collectors.toList());
    for (File file : filteredFiles) {
        BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
        StringBuilder newLinesOfAFile = new StringBuilder();

        String newLine;
        // loop through all lines in a given file
        while ((newLine = bufferedReader.readLine()) != null) {
            String lineBeforeChange = newLine;
            // check for all characters before variable
            for (Character characterBeforeVariable : charactersBeforeVariable) {
                // check all prefixes
                for (String prefix : prefixes) {
                    newLine = removeHungarianNotationFromLine(
                            newLine, 
                            characterBeforeVariable, 
                            prefix
                    );
                }
            }
            if (!lineBeforeChange.equals(newLine)) {
                // log changes to console
                System.out.println(lineBeforeChange + " has been replaced with " + newLine);
            }
            newLinesOfAFile.append(newLine);
            newLinesOfAFile.append("\n");
        }
        // replace old file content with new content
        FileWriter fw = new FileWriter(file, false);
        fw.append(newLinesOfAFile.toString());
        fw.close();
    }
}

public Collection<File> getAllFilesInDir(File dir) {
    Set<File> fileTree = new HashSet<>();
    if (dir == null || dir.listFiles() == null) {
        return fileTree;
    }
    for (File entry : dir.listFiles()) {
        if (entry.isFile()) {
            fileTree.add(entry);
        } else {
            fileTree.addAll(getAllFilesInDir(entry));
        }
    }
    return fileTree;
}

public String removeHungarianNotationFromLine(String oldLine, Character characterBeforeVariable, String prefix) {
    String newLine = oldLine;
    // loop through characters in line
    for(int index = 0; index < oldLine.length(); index += 2){
        // look for target in given line
        index = oldLine.indexOf(characterBeforeVariable + prefix, index);
        // mVariable have to exist and have to be at least 3 characters long(counting with characterBeforeVariable)
        if (index!=-1 && index + 3 < oldLine.length()) {
            // index 0 is characterBeforeVariable, index 1 is m or s
            char character = oldLine.charAt(index + 2); // potential capital letter in variable
            if (character >= 'A' && character <= 'Z') { // check if capital letter
                // replace ' mVariable' with ' variable'
                newLine = oldLine.replace(
                        characterBeforeVariable + prefix + character,
                        characterBeforeVariable + Character.toString(character).toLowerCase()
                );
                // remove recursively (imagine mCount = mCount + 15)
                return removeHungarianNotationFromLine(
                        newLine,
                        characterBeforeVariable,
                        prefix
                );
            }
        }
        // test condition before adding 2 to index
        if(index == -1)
            break;
    }
    return newLine;
}