Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -726,10 +726,20 @@ class AstCreator(filename: String, javaParserAst: CompilationUnit, global: Globa
private def astForFieldVariable(v: VariableDeclarator, fieldDeclaration: FieldDeclaration): Ast = {
// TODO: Should be able to find expected type here
val annotations = fieldDeclaration.getAnnotations

// variable can be declared with generic type, so we need to get rid of the <> part of it
// Ex - private Consumer<String, Integer> consumer;
// From Consumer<String, Integer> we need to get to Consumer so splitting it by '<'

val typeFullName =
typeInfoCalc
.fullName(v.getType)
.orElse(scopeStack.lookupVariableType(v.getTypeAsString, wildcardFallback = true))
.orElse(
scopeStack.lookupVariableType(
v.getTypeAsString.split("<").headOption.getOrElse(v.getTypeAsString),
wildcardFallback = true
)
)
.getOrElse(s"${Defines.UnresolvedNamespace}.${v.getTypeAsString}")
val name = v.getName.toString
val node = memberNode(v, name, s"$typeFullName $name", typeFullName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,27 @@ class NewMemberTests extends JavaSrcCode2CpgFixture {
cpg.member.name("x").astChildren.size shouldBe 0
}

"member with generic class" should {
val cpg = code("""
|import org.apache.kafka.clients.consumer.Consumer;
|public class CountryPopulationConsumer {
|
| private Consumer<String, Integer> consumer;
|
| void foo() {
| consumer.poll(1000);
| }
|}""".stripMargin)

"have a resolved typeFullName" in {
cpg.member.name("consumer").typeFullName.head shouldBe "org.apache.kafka.clients.consumer.Consumer"
}

"have a resolved package name in methodFullName" in {
cpg.call("poll").methodFullName.head.split(":").head shouldBe "org.apache.kafka.clients.consumer.Consumer.poll"
}
}

"enum entries with anonymous classes should not result in subtrees to the member node" in {
val cpg = code("""
|enum Foo {
Expand Down