Skip to content

Commit bc7ac5c

Browse files
authored
Allow extensions from io.shiftleft and io.joern packages (#1017)
* Allow extensions from `io.shiftleft` and `io.joern` * Clean fix to load from `io.shiftleft` and `io.joern`
1 parent 25d36aa commit bc7ac5c

2 files changed

Lines changed: 40 additions & 25 deletions

File tree

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package io.joern
2+
3+
class Empty {}

console/src/main/scala/io/shiftleft/console/Run.scala

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import io.shiftleft.passes.{CpgPass, DiffGraph}
55
import io.shiftleft.semanticcpg.language.HasStoreMethod
66
import io.shiftleft.semanticcpg.layers.{LayerCreator, LayerCreatorContext}
77
import org.reflections8.Reflections
8+
import org.reflections8.util.{ClasspathHelper, ConfigurationBuilder}
89

910
import scala.jdk.CollectionConverters._
1011

@@ -38,32 +39,43 @@ object Run {
3839
* @param exclude list of analyzers to exclude (by full class name)
3940
* */
4041
def codeForRunCommand(exclude: List[String] = List()): String = {
41-
val r = new Reflections("io.shiftleft")
42-
val layerCreatorTypeNames = r
43-
.getSubTypesOf(classOf[LayerCreator])
42+
val ioSlLayerCreators = creatorsFor("io.shiftleft", exclude)
43+
val ioJoernLayerCreators = creatorsFor("io.joern", exclude)
44+
codeForLayerCreators((ioSlLayerCreators ++ ioJoernLayerCreators).distinct)
45+
}
46+
47+
private def creatorsFor(namespace: String, exclude: List[String]) = {
48+
new Reflections(
49+
new ConfigurationBuilder().setUrls(
50+
ClasspathHelper.forPackage(namespace,
51+
ClasspathHelper.contextClassLoader(),
52+
ClasspathHelper.staticClassLoader()))
53+
).getSubTypesOf(classOf[LayerCreator])
4454
.asScala
4555
.filterNot(t => t.isAnonymousClass || t.isLocalClass || t.isMemberClass || t.isSynthetic)
4656
.filterNot(t => t.getName.startsWith("io.shiftleft.console.Run"))
4757
.toList
4858
.map(t => (t.getSimpleName.toLowerCase, t.getName))
4959
.filter(t => !exclude.contains(t._2))
60+
}
5061

62+
private def codeForLayerCreators(layerCreatorTypeNames: List[(String, String)]): String = {
5163
val optsMembersCode = layerCreatorTypeNames
5264
.map { case (varName, typeName) => s"val $varName = $typeName.defaultOpts" }
5365
.mkString("\n")
5466

5567
val optsCode =
5668
s"""
57-
|class OptsDynamic {
58-
| $optsMembersCode
59-
|}
60-
|
61-
|val opts = new OptsDynamic()
62-
|
63-
| import io.shiftleft.passes.DiffGraph
64-
| implicit def _diffGraph : DiffGraph.Builder = opts.commit.diffGraphBuilder
65-
| def diffGraph = _diffGraph
66-
|""".stripMargin
69+
|class OptsDynamic {
70+
| $optsMembersCode
71+
|}
72+
|
73+
|val opts = new OptsDynamic()
74+
|
75+
| import io.shiftleft.passes.DiffGraph
76+
| implicit def _diffGraph : DiffGraph.Builder = opts.commit.diffGraphBuilder
77+
| def diffGraph = _diffGraph
78+
|""".stripMargin
6779

6880
val membersCode = layerCreatorTypeNames
6981
.map { case (varName, typeName) => s"def $varName: Cpg = _runAnalyzer(new $typeName(opts.$varName))" }
@@ -85,18 +97,18 @@ object Run {
8597

8698
optsCode +
8799
s"""
88-
| class OverlaysDynamic {
89-
|
90-
| def apply(query : io.shiftleft.semanticcpg.language.HasStoreMethod) {
91-
| io.shiftleft.console.Run.runCustomQuery(console, query)
92-
| }
93-
|
94-
| $membersCode
95-
|
96-
| $toStringCode
97-
| }
98-
| val run = new OverlaysDynamic()
99-
|""".stripMargin
100+
| class OverlaysDynamic {
101+
|
102+
| def apply(query : io.shiftleft.semanticcpg.language.HasStoreMethod) {
103+
| io.shiftleft.console.Run.runCustomQuery(console, query)
104+
| }
105+
|
106+
| $membersCode
107+
|
108+
| $toStringCode
109+
| }
110+
| val run = new OverlaysDynamic()
111+
|""".stripMargin
100112
}
101113

102114
}

0 commit comments

Comments
 (0)