Skip to content

Commit 5cc9800

Browse files
authored
Fixed FileNotFoundExecption during JavaParser work (#1303)
Added a solution
1 parent 32deee9 commit 5cc9800

File tree

2 files changed

+30
-20
lines changed

2 files changed

+30
-20
lines changed

utbot-summary/src/main/kotlin/org/utbot/summary/Summarization.kt

+30-19
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,10 @@ fun UtMethodTestSet.summarize(sourceFile: File?, searchDirectory: Path = Paths.g
7070
}
7171

7272
fun UtMethodTestSet.summarize(searchDirectory: Path): UtMethodTestSet =
73-
this.summarize(Instrumenter.adapter.computeSourceFileByClass(this.method.classId.jClass, searchDirectory), searchDirectory)
73+
this.summarize(
74+
Instrumenter.adapter.computeSourceFileByClass(this.method.classId.jClass, searchDirectory),
75+
searchDirectory
76+
)
7477

7578

7679
class Summarization(val sourceFile: File?, val invokeDescriptions: List<InvokeDescription>) {
@@ -326,32 +329,34 @@ class Summarization(val sourceFile: File?, val invokeDescriptions: List<InvokeDe
326329
)
327330
}
328331

329-
/*
330-
* asts of invokes also included
331-
* */
332+
/** ASTs of invokes are also included. */
332333
private fun sootToAST(
333334
testSet: UtMethodTestSet
334335
): MutableMap<SootMethod, JimpleToASTMap>? {
335336
val sootToAST = mutableMapOf<SootMethod, JimpleToASTMap>()
336337
val jimpleBody = testSet.jimpleBody
337338
if (jimpleBody == null) {
338-
logger.info { "No jimple body of method under test" }
339+
logger.debug { "No jimple body of method under test ${testSet.method.name}." }
339340
return null
340341
}
341-
val methodUnderTestAST = sourceFile?.let {
342-
SourceCodeParser(it, testSet).methodAST
343-
}
344342

345-
if (methodUnderTestAST == null) {
346-
logger.info { "Couldn't parse source file of method under test" }
347-
return null
348-
}
343+
if (sourceFile != null && sourceFile.exists()) {
344+
val methodUnderTestAST = SourceCodeParser(sourceFile, testSet).methodAST
345+
346+
if (methodUnderTestAST == null) {
347+
logger.debug { "Couldn't parse source file with path ${sourceFile.absolutePath} of method under test ${testSet.method.name}." }
348+
return null
349+
}
349350

350-
sootToAST[jimpleBody.method] = JimpleToASTMap(jimpleBody.units, methodUnderTestAST)
351-
invokeDescriptions.forEach {
352-
sootToAST[it.sootMethod] = JimpleToASTMap(it.sootMethod.jimpleBody().units, it.ast)
351+
sootToAST[jimpleBody.method] = JimpleToASTMap(jimpleBody.units, methodUnderTestAST)
352+
invokeDescriptions.forEach {
353+
sootToAST[it.sootMethod] = JimpleToASTMap(it.sootMethod.jimpleBody().units, it.ast)
354+
}
355+
return sootToAST
356+
} else {
357+
logger.debug { "Couldn't find source file of method under test ${testSet.method.name}." }
358+
return null
353359
}
354-
return sootToAST
355360
}
356361
}
357362

@@ -386,6 +391,7 @@ private fun makeDiverseExecutions(testSet: UtMethodTestSet) {
386391
private fun invokeDescriptions(testSet: UtMethodTestSet, searchDirectory: Path): List<InvokeDescription> {
387392
val sootInvokes =
388393
testSet.executions.filterIsInstance<UtSymbolicExecution>().flatMap { it.path.invokeJimpleMethods() }.toSet()
394+
389395
return sootInvokes
390396
//TODO(SAT-1170)
391397
.filterNot { "\$lambda" in it.declaringClass.name }
@@ -395,10 +401,15 @@ private fun invokeDescriptions(testSet: UtMethodTestSet, searchDirectory: Path):
395401
sootMethod.declaringClass.javaPackageName.replace(".", File.separator),
396402
searchDirectory
397403
)
398-
val ast = methodFile?.let {
399-
SourceCodeParser(sootMethod, it).methodAST
404+
405+
if (methodFile != null && methodFile.exists()) {
406+
val ast = methodFile.let {
407+
SourceCodeParser(sootMethod, it).methodAST
408+
}
409+
if (ast != null) InvokeDescription(sootMethod, ast) else null
410+
} else {
411+
null
400412
}
401-
if (ast != null) InvokeDescription(sootMethod, ast) else null
402413
}
403414
}
404415

utbot-summary/src/main/kotlin/org/utbot/summary/ast/SourceCodeParser.kt

-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ class SourceCodeParser {
3636
val methodName = sootMethod.name
3737
val className = sootMethod.declaredClassName
3838

39-
4039
val maxLineNumber =
4140
if (sootMethod.hasActiveBody())
4241
sootMethod.retrieveActiveBody()?.units?.maxOfOrNull { it.javaSourceStartLineNumber }

0 commit comments

Comments
 (0)