Skip to content

Commit 17781c4

Browse files
ventusfortismaltek
andauthored
Fix TypeDecl code to include abstract/final and generics #5726 (#5728)
* Fix TypeDecl code to include abstract/final and generics #5726 * Cycle through keywords --------- Co-authored-by: maltek <1694194+maltek@users.noreply.github.com> Co-authored-by: Malte Kraus <mkraus@qwiet.ai>
1 parent a492f02 commit 17781c4

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

joern-cli/frontends/javasrc2cpg/src/main/scala/io/joern/javasrc2cpg/astcreation/declarations/AstForTypeDeclsCreator.scala

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -790,7 +790,6 @@ private[declarations] trait AstForTypeDeclsCreator { this: AstCreator =>
790790

791791
private def codeForTypeDecl(typ: TypeDeclaration[?], isInterface: Boolean): String = {
792792
val codeBuilder = new mutable.StringBuilder()
793-
794793
typ.getModifiers.asScala
795794
.foreach { modifier =>
796795
codeBuilder.append(modifier.getKeyword.asString())
@@ -807,6 +806,15 @@ private[declarations] trait AstForTypeDeclsCreator { this: AstCreator =>
807806
codeBuilder.append(classPrefix)
808807
codeBuilder.append(typ.getNameAsString)
809808

809+
val typeParameters = typ match {
810+
case classOrInterface: ClassOrInterfaceDeclaration => classOrInterface.getTypeParameters.asScala
811+
case recordDeclaration: RecordDeclaration => recordDeclaration.getTypeParameters.asScala
812+
case _ => Nil
813+
}
814+
if (typeParameters.nonEmpty) {
815+
codeBuilder.append(typeParameters.mkString("<", ", ", ">"))
816+
}
817+
810818
codeBuilder.toString()
811819
}
812820

joern-cli/frontends/javasrc2cpg/src/test/scala/io/joern/javasrc2cpg/querying/TypeDeclTests.scala

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,18 @@ class NewTypeDeclTests extends JavaSrcCode2CpgFixture {
5959
}
6060
}
6161

62+
"type decl code" should {
63+
"include abstract modifier and type parameters" in {
64+
val cpg = code("""
65+
|class BaseReq {}
66+
|public abstract class GenericService<T extends BaseReq, R> {}
67+
|""".stripMargin)
68+
69+
cpg.typeDecl.nameExact("GenericService").code.l shouldBe
70+
List("public abstract class GenericService<T extends BaseReq, R>")
71+
}
72+
}
73+
6274
"typedecls extending unresolved types available in imports should have inheritsFrom set" in {
6375
val cpg = code("""package io.vrooom.vulnerableapp;
6476
|

0 commit comments

Comments
 (0)