/** * Applies the provided set of suggested fixes to the provided code and returns the new code. * The {@code filenameToCodeMap} must contain all the files that the provided fixes apply to. * The fixes can be provided in any order, but they may not have any overlapping modifications * for the same file. * This function will return new code only for the files that have been modified. */ public static Map<String, String> applySuggestedFixesToCode( Iterable<SuggestedFix> fixes, Map<String, String> filenameToCodeMap) { ImmutableSetMultimap.Builder<String, CodeReplacement> builder = ImmutableSetMultimap.builder(); for (SuggestedFix fix : fixes) { builder.putAll(fix.getReplacements()); } SetMultimap<String, CodeReplacement> replacementsMap = builder.build(); ImmutableMap.Builder<String, String> newCodeMap = ImmutableMap.builder(); for (Map.Entry<String, Set<CodeReplacement>> entry : Multimaps.asMap(replacementsMap).entrySet()) { String filename = entry.getKey(); if (!filenameToCodeMap.containsKey(filename)) { throw new IllegalArgumentException("filenameToCodeMap missing code for file: " + filename); } Set<CodeReplacement> replacements = entry.getValue(); String newCode = applyCodeReplacements(replacements, filenameToCodeMap.get(filename)); newCodeMap.put(filename, newCode); } return newCodeMap.build(); }