Skip to content

Commit

Permalink
Replace typeNodePass regex with more efficient name extraction (#3921)
Browse files Browse the repository at this point in the history
* Replace typeNodePass regex with more efficient name extraction

* Add testcase
  • Loading branch information
johannescoetzee authored Dec 8, 2023
1 parent e2751e8 commit d0f8d4d
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ class LambdaTests extends JavaSrcCode2CpgFixture {
|}
|""".stripMargin)

"create the correct type node for the lambda" in {
cpg.typ.name(".*lambda.*").name.l shouldBe List("<lambda>0")
cpg.typ.name(".*lambda.*").fullName.l shouldBe List("Foo.<lambda>0:java.lang.String(java.lang.String)")
}

"create a method node for the lambda" in {
cpg.typeDecl.name("Foo").method.name(".*lambda.*").isLambda.l match {
case List(lambdaMethod) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,6 @@ class TypeNodePass protected (
}

object TypeNodePass {
// Lambda typeDecl type names fit the structure
// `a.b.c.d.ClassName.lambda$method$name:returnType(paramTypes)`
// so this regex works by greedily matching the package and class names
// at the start and cutting off the matched group before the signature.
private val lambdaTypeRegex = raw".*\.(.*):.*\(.*\)".r

def withTypesFromCpg(cpg: Cpg, keyPool: Option[KeyPool] = None): TypeNodePass = {
new TypeNodePass(Nil, cpg, keyPool, getTypesFromCpg = true)
}
Expand All @@ -80,9 +74,6 @@ object TypeNodePass {
}

def fullToShortName(typeName: String): String = {
typeName match {
case lambdaTypeRegex(methodName) => methodName
case _ => typeName.split('.').lastOption.getOrElse(typeName)
}
typeName.takeWhile(_ != ':').split('.').lastOption.getOrElse(typeName)
}
}

0 comments on commit d0f8d4d

Please sign in to comment.