Skip to content

Commit

Permalink
✨ Jars are now supported (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidBakerEffendi authored Feb 10, 2021
1 parent 181760f commit 37c55c4
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 20 deletions.
28 changes: 27 additions & 1 deletion plume/src/main/kotlin/io/github/plume/oss/Extractor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.Paths
import java.util.stream.Collectors
import java.util.zip.ZipFile
import kotlin.streams.asSequence
import kotlin.streams.toList
import io.shiftleft.codepropertygraph.generated.nodes.File as ODBFile
Expand Down Expand Up @@ -199,7 +200,32 @@ class Extractor(val driver: IDriver) {
.let { loadedFiles.addAll(it) }
}
} else if (f.isFile) {
loadedFiles.add(FileFactory(f))
if (f.name.endsWith(".jar")) {
unzipArchive(ZipFile(f)).forEach { loadedFiles.add(FileFactory(it)) }
} else {
loadedFiles.add(FileFactory(f))
}
}
}

private fun unzipArchive(zf: ZipFile) = sequence {
zf.use { zip ->
// Copy zipped files across
zip.entries().asSequence().filter { !it.isDirectory }.forEach { entry ->
val destFile = File(COMP_DIR + File.separator + entry.name)
val dirName = destFile.absolutePath.substringBeforeLast('/')
// Create directory path
File(dirName).mkdirs()
runCatching {
destFile.createNewFile()
}.onSuccess {
zip.getInputStream(entry)
.use { input -> destFile.outputStream().use { output -> input.copyTo(output) } }
}.onFailure {
logger.warn("Encountered an error while extracting entry ${entry.name} from archive ${zf.name}.")
}
yield(destFile)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,21 @@ object FileFactory {
}
}

/**
* Class wrapper for Java source files.
*/
class JavaFile(pathname: String) : PlumeFile(pathname)

/**
* Class wrapper for JVM class files.
*/
class JVMClassFile(pathname: String) : PlumeFile(pathname)

/**
* Class wrapper for unsupported files.
*/
class UnsupportedFile(pathname: String) : PlumeFile(pathname)

/**
* The file types supported by Plume's [Extractor].
*/
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import io.github.plume.oss.Extractor
import io.github.plume.oss.drivers.DriverFactory
import io.github.plume.oss.drivers.GraphDatabase
import io.github.plume.oss.drivers.OverflowDbDriver
import io.github.plume.oss.graphio.GraphMLWriter
import io.shiftleft.codepropertygraph.generated.nodes.Call
import io.shiftleft.codepropertygraph.generated.nodes.Local
import io.shiftleft.codepropertygraph.generated.nodes.Method
Expand All @@ -14,6 +15,7 @@ import org.junit.jupiter.api.BeforeAll
import org.junit.jupiter.api.Test
import overflowdb.Graph
import java.io.File
import java.io.FileWriter
import io.shiftleft.codepropertygraph.generated.nodes.File as ODBFile

class BasicExtractorTest {
Expand Down Expand Up @@ -99,7 +101,26 @@ class BasicExtractorTest {
assertNotNull(fileList.firstOrNull { it.name() == "/extractor_tests/dir_test/Dir1.class" })
assertNotNull(fileList.firstOrNull { it.name() == "/extractor_tests/dir_test/pack/Dir2.class" })
}
assertNotNull(ns.filterIsInstance<NamespaceBlock>().firstOrNull { it.name() == "extractor_tests.dir_test.pack" })
assertNotNull(
ns.filterIsInstance<NamespaceBlock>().firstOrNull { it.name() == "extractor_tests.dir_test.pack" })
}

@Test
fun validJarTest() {
extractor.load(validJarFile)
extractor.project()
g = driver.getWholeGraph()
GraphMLWriter.write(g, FileWriter("/tmp/plume/x.xml"))
g = driver.getProgramStructure()
val ns = g.nodes().asSequence().toList()
ns.filterIsInstance<ODBFile>().let { fileList ->
assertNotNull(fileList.firstOrNull { it.name() == "/intraprocedural/basic/Basic6.class" })
assertNotNull(fileList.firstOrNull { it.name() == "/intraprocedural/basic/basic6/Basic6.class" })
}
ns.filterIsInstance<NamespaceBlock>().let { nsList ->
assertNotNull(nsList.firstOrNull { it.name() == "intraprocedural.basic" })
assertNotNull(nsList.firstOrNull { it.name() == "intraprocedural.basic.basic6" })
}
}

@Test
Expand Down

0 comments on commit 37c55c4

Please sign in to comment.