private String shadeSourceWithExcludes()

in src/main/java/org/apache/maven/plugins/shade/relocation/SimpleRelocator.java [224:259]


    private String shadeSourceWithExcludes(
            String sourceContent, String patternFrom, String patternTo, Set<String> excludedPatterns) {
        // Usually shading makes package names a bit longer, so make buffer 10% bigger than original source
        StringBuilder shadedSourceContent = new StringBuilder(sourceContent.length() * 11 / 10);
        boolean isFirstSnippet = true;
        // Make sure that search pattern starts at word boundary and that we look for literal ".", not regex jokers
        String[] snippets = sourceContent.split("\\b" + patternFrom.replace(".", "[.]") + "\\b");
        for (int i = 0, snippetsLength = snippets.length; i < snippetsLength; i++) {
            String snippet = snippets[i];
            String previousSnippet = isFirstSnippet ? "" : snippets[i - 1];
            boolean doExclude = false;
            for (String excludedPattern : excludedPatterns) {
                if (snippet.startsWith(excludedPattern)) {
                    doExclude = true;
                    break;
                }
            }
            if (isFirstSnippet) {
                shadedSourceContent.append(snippet);
                isFirstSnippet = false;
            } else {
                String previousSnippetOneLine = previousSnippet.replaceAll("\\s+", " ");
                boolean afterDotSlashSpace = RX_ENDS_WITH_DOT_SLASH_SPACE
                        .matcher(previousSnippetOneLine)
                        .find();
                boolean afterJavaKeyWord = RX_ENDS_WITH_JAVA_KEYWORD
                        .matcher(previousSnippetOneLine)
                        .find();
                boolean shouldExclude = doExclude || afterDotSlashSpace && !afterJavaKeyWord;
                shadedSourceContent
                        .append(shouldExclude ? patternFrom : patternTo)
                        .append(snippet);
            }
        }
        return shadedSourceContent.toString();
    }