Skip to content

Commit 08f67c1

Browse files
#152 Making SCM Log info lookup more robust
Handling NPEs if there is an exception in either nested class lookup or non-nested class lookup
1 parent fb72d4e commit 08f67c1

1 file changed

Lines changed: 30 additions & 29 deletions

File tree

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

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -160,31 +160,30 @@ public <T extends Disharmony> List<ScmLogInfo> getRankedChangeProneness(List<T>
160160
List<Optional<ScmLogInfo>> scmLogInfos = disharmonies.parallelStream()
161161
.map(disharmony -> {
162162
String className = disharmony.getClassName();
163-
String path;
163+
String path = null;
164164
ScmLogInfo scmLogInfo = null;
165-
if (className.contains("$")
166-
&& classToSourceFilePathMapping.containsKey(
167-
className.substring(0, className.indexOf("$")))) {
168-
path = classToSourceFilePathMapping.get(className.substring(0, className.indexOf("$")));
169-
log.debug("Found source file {} for nested class: {}", path, className);
170-
innerClassPaths.put(className, path);
171-
} else {
172-
path = disharmony.getFileName();
173-
try {
174-
log.debug("Reading scmLogInfo for {}", path);
175-
scmLogInfo = gitLogReader.fileLog(path);
176-
scmLogInfo.setClassName(className);
177-
log.debug("Successfully fetched scmLogInfo for {}", scmLogInfo.getPath());
178-
scmLogInfosByPath.put(path, scmLogInfo);
179-
} catch (GitAPIException | IOException e) {
180-
log.error("Error reading Git repository contents.", e);
181-
} catch (NullPointerException e) {
182-
// Should not be reached
183-
log.error(
184-
"Encountered nested class in a class containing a violation. Class: {}, Path: {}",
185-
className,
186-
path);
165+
try {
166+
if (className.contains("$")
167+
&& classToSourceFilePathMapping.containsKey(
168+
className.substring(0, className.indexOf("$")))) {
169+
path = classToSourceFilePathMapping.get(className.substring(0, className.indexOf("$")));
170+
log.debug("Found source file {} for nested class: {}", path, className);
171+
innerClassPaths.put(className, path);
172+
} else {
173+
path = disharmony.getFileName();
174+
try {
175+
log.debug("Reading scmLogInfo for {}", path);
176+
scmLogInfo = gitLogReader.fileLog(path);
177+
scmLogInfo.setClassName(className);
178+
log.debug("Successfully fetched scmLogInfo for {}", scmLogInfo.getPath());
179+
scmLogInfosByPath.put(path, scmLogInfo);
180+
} catch (GitAPIException | IOException e) {
181+
log.error("Error reading Git repository contents.", e);
182+
}
187183
}
184+
} catch (NullPointerException e) {
185+
// Should not be reached
186+
log.error("Error looking up class SCM info. Class: {}, Path: {}", className, path, e);
188187
}
189188

190189
Optional<ScmLogInfo> scmLogInfoOptional = Optional.ofNullable(scmLogInfo);
@@ -201,12 +200,10 @@ public <T extends Disharmony> List<ScmLogInfo> getRankedChangeProneness(List<T>
201200

202201
ScmLogInfo innerClassScmLogInfo = null;
203202
if (scmLogInfo == null) {
203+
String className = innerClassPathEntry.getKey();
204+
String path = classToSourceFilePathMapping.get(className.substring(0, className.indexOf("$")));
205+
log.debug("Reading scmLogInfo for inner class {}", canonicaliseURIStringForRepoLookup(path));
204206
try {
205-
String className = innerClassPathEntry.getKey();
206-
String path =
207-
classToSourceFilePathMapping.get(className.substring(0, className.indexOf("$")));
208-
log.debug(
209-
"Reading scmLogInfo for inner class {}", canonicaliseURIStringForRepoLookup(path));
210207
innerClassScmLogInfo = gitLogReader.fileLog(canonicaliseURIStringForRepoLookup(path));
211208
innerClassScmLogInfo.setClassName(className);
212209
log.debug(
@@ -215,7 +212,11 @@ public <T extends Disharmony> List<ScmLogInfo> getRankedChangeProneness(List<T>
215212
innerClassScmLogInfo.getPath());
216213
scmLogInfosByPath.put(path, innerClassScmLogInfo);
217214
} catch (GitAPIException | IOException e) {
218-
log.error("Error reading Git repository contents.", e);
215+
log.error(
216+
"Error reading Git repository contents for class {} with file path {}",
217+
className,
218+
path,
219+
e);
219220
}
220221
} else {
221222
innerClassScmLogInfo = new ScmLogInfo(

0 commit comments

Comments
 (0)