Skip to content

Commit

Permalink
[javasrc2cpg] fix ext method call full name with same name of a scope…
Browse files Browse the repository at this point in the history
…d var (#4545)
  • Loading branch information
xavierpinho authored May 8, 2024
1 parent 42eb173 commit 5acae86
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ class AstCreator(
scope.lookupType(annotation.getNameAsString)
case namedExpr: NodeWithName[_] =>
scope.lookupVariableOrType(namedExpr.getNameAsString)
case namedExpr: NodeWithSimpleName[_] =>
case namedExpr: NodeWithSimpleName[_] if !expr.isMethodCallExpr =>
scope.lookupVariableOrType(namedExpr.getNameAsString)
// JavaParser doesn't handle literals well for some reason
case _: BooleanLiteralExpr => Some("boolean")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,37 @@ class NewCallTests extends JavaSrcCode2CpgFixture {
.last shouldBe "Derived.getCurrentSession:org.hibernate.Session()"
}
}

"call to external method in a builder-like pattern" should {
val cpg = code("""
|package example;
|import org.Builder;
|import org.Client;
|
|class Main {
| static void main(String[] args) {
| Client foo = new Builder().foo().buildClient(); // 8
| new Builder().somethingElse().buildClient(); // 9
| }
|}
|""".stripMargin)

"have correct method full name for `buildClient` call on line 8" in {
cpg.call("buildClient").lineNumber(8).l match {
case List(buildClient) =>
buildClient.methodFullName shouldBe "<unresolvedNamespace>.buildClient:<unresolvedSignature>(0)"
case result => fail(s"Expected single buildClient call but got $result")
}
}

"have correct method full name for `buildClient` call on line 9" in {
cpg.call("buildClient").lineNumber(9).l match {
case List(buildClient) =>
buildClient.methodFullName shouldBe "<unresolvedNamespace>.buildClient:<unresolvedSignature>(0)"
case result => fail(s"Expected single buildClient call but got $result")
}
}
}
}

class CallTests extends JavaSrcCode2CpgFixture {
Expand Down

0 comments on commit 5acae86

Please sign in to comment.