Skip to content

Commit

Permalink
[c#] Fix FileNotFoundException on jar path for builtin_types (joernio…
Browse files Browse the repository at this point in the history
  • Loading branch information
karan-batavia authored Feb 28, 2024
1 parent 37687ab commit 5dc403c
Showing 1 changed file with 18 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,25 +62,33 @@ object CSharpProgramSummary {
val classLoader = getClass.getClassLoader
val builtinDirectory = "builtin_types"

val url = classLoader.getResource(builtinDirectory)
val path = Environment.operatingSystem match {
case Environment.OperatingSystemType.Windows => url.getPath.stripPrefix("/")
case _ => url.getPath
}

/*
Doing this because java actually cannot read directories from the classPath.
We're assuming there's no further nesting in the builtin_types directory structure.
*/
val resourcePaths =
File(path).listRecursively
.filter(_.name.endsWith(".json"))
.map(_.pathAsString)
Source
.fromResource(builtinDirectory)
.getLines()
.toList
.flatMap(u => {
val basePath = s"$builtinDirectory/$u"
Source
.fromResource(basePath)
.getLines()
.toList
.map(p => {
s"$basePath/$p"
})
})

if (resourcePaths.isEmpty) {
logger.warn("No JSON files found.")
InputStream.nullInputStream()
} else {
val mergedJsonObjects = ListBuffer[LinkedHashMap[String, ujson.Value]]()
for (resourcePath <- resourcePaths) {
val inputStream = File(resourcePath).newInputStream
val inputStream = classLoader.getResourceAsStream(resourcePath)
val jsonString = Source.fromInputStream(inputStream).mkString
val jsonObject = ujson.read(jsonString).obj
mergedJsonObjects.addOne(jsonObject)
Expand Down

0 comments on commit 5dc403c

Please sign in to comment.