Skip to content

Commit 1f27fb1

Browse files
#45 working on CBO scanner
1 parent 702d2c8 commit 1f27fb1

File tree

3 files changed

+39
-15
lines changed

3 files changed

+39
-15
lines changed

change-proneness-ranker/src/main/java/org/hjug/git/GitLogReader.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public File getGitDir(File basedir) {
4747
// and
4848
// https://github.com/centic9/jgit-cookbook/blob/master/src/main/java/org/dstadler/jgit/api/ReadFileFromCommit.java
4949
@Override
50-
public Map<String, ByteArrayOutputStream> listRepositoryContentsAtHEAD(Repository repository) throws IOException {
50+
public Map<String, ByteArrayOutputStream> (Repository repository) throws IOException {
5151
Ref head = repository.exactRef("HEAD");
5252
// a RevWalk allows us to walk over commits based on some filtering that is defined
5353
RevWalk walk = new RevWalk(repository);
@@ -56,7 +56,7 @@ public Map<String, ByteArrayOutputStream> listRepositoryContentsAtHEAD(Repositor
5656

5757
TreeWalk treeWalk = new TreeWalk(repository);
5858
treeWalk.addTree(tree);
59-
treeWalk.setRecursive(false);
59+
treeWalk.setRecursive(false);listRepositoryContentsAtHEAD
6060

6161
// TODO: extract rest of this method to test it
6262
Map<String, ByteArrayOutputStream> fileContentsCollection = new HashMap<>();

cost-benefit-calculator/src/main/java/org/hjug/cbc/CostBenefitCalculator.java

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@
1313
import org.hjug.git.GitLogReader;
1414
import org.hjug.git.RepositoryLogReader;
1515
import org.hjug.git.ScmLogInfo;
16-
import org.hjug.metrics.GodClass;
17-
import org.hjug.metrics.GodClassRanker;
18-
import org.hjug.metrics.PMDGodClassRuleRunner;
16+
import org.hjug.metrics.*;
1917

2018
@Slf4j
2119
public class CostBenefitCalculator {
@@ -31,7 +29,10 @@ public List<RankedDisharmony> calculateCostBenefitValues(String repositoryPath)
3129
log.error("Failure to access Git repository", e);
3230
}
3331

34-
List<GodClass> godClasses = getGodClasses(repositoryLogReader, repository);
32+
Map<String, ByteArrayOutputStream> filesToScan = getFilesToScan(repositoryLogReader, repository);
33+
34+
List<GodClass> godClasses = getGodClasses(filesToScan);
35+
List<CBOClass> cboClasses = getCBOClasses(filesToScan);
3536

3637
List<ScmLogInfo> scmLogInfos = getRankedChangeProneness(repositoryLogReader, repository, godClasses);
3738

@@ -67,17 +68,10 @@ List<ScmLogInfo> getRankedChangeProneness(
6768
return scmLogInfos;
6869
}
6970

70-
private List<GodClass> getGodClasses(RepositoryLogReader repositoryLogReader, Repository repository) {
71-
Map<String, ByteArrayOutputStream> filesToScan = new HashMap<>();
72-
log.info("Identifying God Classes from files in repository");
73-
try {
74-
filesToScan = repositoryLogReader.listRepositoryContentsAtHEAD(repository);
75-
} catch (IOException e) {
76-
log.error("Error reading Git repository contents", e);
77-
}
78-
71+
private List<GodClass> getGodClasses(Map<String, ByteArrayOutputStream> filesToScan) {
7972
PMDGodClassRuleRunner ruleRunner = new PMDGodClassRuleRunner();
8073

74+
log.info("Identifying God Classes from files in repository");
8175
List<GodClass> godClasses = new ArrayList<>();
8276
for (Map.Entry<String, ByteArrayOutputStream> entry : filesToScan.entrySet()) {
8377
String filePath = entry.getKey();
@@ -92,4 +86,32 @@ private List<GodClass> getGodClasses(RepositoryLogReader repositoryLogReader, Re
9286
godClassRanker.rankGodClasses(godClasses);
9387
return godClasses;
9488
}
89+
90+
private List<CBOClass> getCBOClasses(Map<String, ByteArrayOutputStream> filesToScan) {
91+
92+
CBORuleRunner ruleRunner = new CBORuleRunner();
93+
94+
log.info("Identifying highly coupled classes from files in repository");
95+
List<CBOClass> cboClasses = new ArrayList<>();
96+
for (Map.Entry<String, ByteArrayOutputStream> entry : filesToScan.entrySet()) {
97+
String filePath = entry.getKey();
98+
ByteArrayOutputStream value = entry.getValue();
99+
100+
ByteArrayInputStream inputStream = new ByteArrayInputStream(value.toByteArray());
101+
Optional<CBOClass> godClassOptional = ruleRunner.runCBOClassRule(filePath, inputStream);
102+
godClassOptional.ifPresent(cboClasses::add);
103+
}
104+
105+
return cboClasses;
106+
}
107+
108+
private Map<String, ByteArrayOutputStream> getFilesToScan(RepositoryLogReader repositoryLogReader, Repository repository) {
109+
Map<String, ByteArrayOutputStream> filesToScan = new HashMap<>();
110+
try {
111+
filesToScan = repositoryLogReader.listRepositoryContentsAtHEAD(repository);
112+
} catch (IOException e) {
113+
log.error("Error reading Git repository contents", e);
114+
}
115+
return filesToScan;
116+
}
95117
}

cost-benefit-calculator/src/main/java/org/hjug/cbc/RankedDisharmony.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ public class RankedDisharmony {
1616
private final Integer changePronenessRank;
1717
private final Integer priority;
1818

19+
private Integer couplingCount;
20+
1921
private final Integer wmc;
2022
private final Integer wmcRank;
2123
private final Integer atfd;

0 commit comments

Comments
 (0)