package it.unitn.molerat.repos.utils; import it.unitn.molerat.evidence.Changes; import it.unitn.molerat.repos.wrappers.RepoWrapper; import org.apache.commons.lang3.StringUtils; import java.util.Map; import java.util.Set; import java.util.TreeMap; import java.util.regex.Matcher; import java.util.regex.Pattern; public class CommitMetrics { private static final Pattern deletedPublicMethodPattern = Pattern.compile("public .*\\([^\\)]*\\)[^;]*$"); public static int getGlobalPublicMethodsRemoved(String leftRev, String rightRev, RepoWrapper wrapper) throws Exception { int removed = 0; String diff = wrapper.doDiff(leftRev, rightRev); Set changes = wrapper.inferChangesFromDiff(diff, leftRev, rightRev); for (Changes change : changes) { if (!change.getPath().endsWith(".java")) { continue; } Map deletions = change.getDeletions(); wrapper.filterCommentsAndBlanks(deletions); for (String del : deletions.values()) { del = StringUtils.trim(del); Matcher matcher = deletedPublicMethodPattern.matcher(del); if (matcher.matches()) { removed++; } matcher.reset(); } } return removed; } public static int getNumberOfPublicMethodsPerRevision(String rev, RepoWrapper wrapper) throws Exception { int pubAPICount = 0; Set files = wrapper.getRevisionFiles(rev, ".java"); for (String f : files) { String fc = wrapper.doCat(f, rev); pubAPICount += countPublicMethodDeclarationsInFile(fc); } return pubAPICount; } public static Map getFileContentsPerRevision(String rev, RepoWrapper wrapper) throws Exception { Map result = new TreeMap<>(); Set files = wrapper.getRevisionFiles(rev, ".java"); for (String file : files) { String contents = wrapper.doCat(file, rev); result.put(file, contents); } return result; } @Deprecated public static int countPublicMethodDeclarationsInFile(String fileContents) { int pubAPICount = 0; String[] lines = StringUtils.split(fileContents, System.getProperty("line.separator")); for (int i=0; i