Skip to content

Commit

Permalink
[javasrc2cpg] Have enums and records extend java.lang.Enum and java.l…
Browse files Browse the repository at this point in the history
…ang.Record respectively (#5218)
  • Loading branch information
johannescoetzee authored Jan 10, 2025
1 parent 97806c7 commit d657796
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -694,6 +694,10 @@ private[declarations] trait AstForTypeDeclsCreator { this: AstCreator =>
Seq()
}
maybeJavaObjectType ++ inheritsFromTypeNames
} else if (typ.isEnumDeclaration) {
TypeConstants.Enum :: Nil
} else if (typ.isRecordDeclaration) {
TypeConstants.Record :: Nil
} else {
List.empty[String]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,8 @@ object TypeInfoCalculator {
val Object: String = "java.lang.Object"
val Class: String = "java.lang.Class"
val Iterator: String = "java.util.Iterator"
val Enum: String = "java.lang.Enum"
val Record: String = "java.lang.Record"
val Void: String = "void"
val Any: String = "ANY"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ class EnumTests extends JavaSrcCode2CpgFixture {
|}
|""".stripMargin)

"the enum type should extends java.lang.Enum" in {
cpg.typeDecl.name("FuzzyBool").inheritsFromTypeFullName.l shouldBe List("java.lang.Enum")
}

"it should parse a basic enum without values" in {
inside(cpg.typeDecl.name(".*FuzzyBool.*").l) { case List(typeDecl) =>
typeDecl.code shouldBe "public enum FuzzyBool"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ class RecordTests extends JavaSrcCode2CpgFixture {
|}
|""".stripMargin)

"extend java.lang.Record" in {
cpg.typeDecl("Foo").inheritsFromTypeFullName.l shouldBe List("java.lang.Record")
}

"have the correct representation for the compact constructor" in {
inside(cpg.method.nameExact("<init>").l) { case List(constructor) =>
constructor.fullName shouldBe "foo.Foo.<init>:void(java.lang.String)"
Expand Down

0 comments on commit d657796

Please sign in to comment.