Skip to content

Commit eb3a843

Browse files
javasrc2cpg: Line Numbers for Annotations (#2302)
- Added line number info for annotation nodes
1 parent b225f40 commit eb3a843

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

joern-cli/frontends/javasrc2cpg/src/main/scala/io/joern/javasrc2cpg/passes/AstCreator.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -950,6 +950,7 @@ class AstCreator(filename: String, javaParserAst: CompilationUnit, global: Globa
950950
.code(annotationExpr.toString)
951951
.name(annotationExpr.getName.getIdentifier)
952952
.fullName(expressionReturnTypeFullName(annotationExpr).getOrElse(fallbackType))
953+
.lineNumber(line(annotationExpr))
953954
}
954955

955956
private def astForAnnotationExpr(annotationExpr: AnnotationExpr): Ast = {

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class AnnotationTests extends JavaSrcCode2CpgFixture {
2222
annotationNode.code shouldBe "@NormalAnnotation(value = \"classAnnotation\")"
2323
annotationNode.name shouldBe "NormalAnnotation"
2424
annotationNode.fullName shouldBe "some.NormalAnnotation"
25+
annotationNode.lineNumber shouldBe Some(5)
2526
}
2627

2728
"test annotation node parameter assignment child" in {
@@ -61,6 +62,7 @@ class AnnotationTests extends JavaSrcCode2CpgFixture {
6162
annotationNode.code shouldBe "@SingleAnnotation(\"classAnnotation\")"
6263
annotationNode.name shouldBe "SingleAnnotation"
6364
annotationNode.fullName shouldBe "some.SingleAnnotation"
65+
annotationNode.lineNumber shouldBe Some(5)
6466
}
6567

6668
"test annotation node parameter assignment child" in {
@@ -99,6 +101,7 @@ class AnnotationTests extends JavaSrcCode2CpgFixture {
99101
annotationNode.code shouldBe "@MarkerAnnotation()"
100102
annotationNode.name shouldBe "MarkerAnnotation"
101103
annotationNode.fullName shouldBe "some.MarkerAnnotation"
104+
annotationNode.lineNumber shouldBe Some(5)
102105
}
103106

104107
"test annotation node parameter assignment child" in {
@@ -119,11 +122,11 @@ class AnnotationTests extends JavaSrcCode2CpgFixture {
119122
|""".stripMargin)
120123

121124
"test annotation node properties" in {
122-
import scala.jdk.CollectionConverters._
123125
val annotationNode = cpg.method.nameExact(io.joern.x2cpg.Defines.ConstructorMethodName).annotation.head
124126
annotationNode.code shouldBe "@MarkerAnnotation()"
125127
annotationNode.name shouldBe "MarkerAnnotation"
126128
annotationNode.fullName shouldBe "some.MarkerAnnotation"
129+
annotationNode.lineNumber shouldBe Some(5)
127130
}
128131

129132
"test annotation node parameter assignment child" in {
@@ -146,6 +149,7 @@ class AnnotationTests extends JavaSrcCode2CpgFixture {
146149
annotationNode.code shouldBe "@MarkerAnnotation"
147150
annotationNode.name shouldBe "MarkerAnnotation"
148151
annotationNode.fullName shouldBe "some.MarkerAnnotation"
152+
annotationNode.lineNumber shouldBe Some(5)
149153
}
150154
}
151155

@@ -162,6 +166,7 @@ class AnnotationTests extends JavaSrcCode2CpgFixture {
162166
annotationNode.code shouldBe "@MarkerAnnotation"
163167
annotationNode.name shouldBe "MarkerAnnotation"
164168
annotationNode.fullName shouldBe "some.MarkerAnnotation"
169+
annotationNode.lineNumber shouldBe Some(4)
165170
}
166171
}
167172

@@ -181,6 +186,7 @@ class AnnotationTests extends JavaSrcCode2CpgFixture {
181186
annotationNode.code shouldBe "@NormalAnnotation(value = 2)"
182187
annotationNode.name shouldBe "NormalAnnotation"
183188
annotationNode.fullName shouldBe "some.NormalAnnotation"
189+
annotationNode.lineNumber shouldBe Some(5)
184190
}
185191

186192
"test annotation node parameter value" in {
@@ -207,6 +213,7 @@ class AnnotationTests extends JavaSrcCode2CpgFixture {
207213
annotationNode.code shouldBe "@NormalAnnotation(value = { \"aaa\", \"bbb\" })"
208214
annotationNode.name shouldBe "NormalAnnotation"
209215
annotationNode.fullName shouldBe "some.NormalAnnotation"
216+
annotationNode.lineNumber shouldBe Some(5)
210217
}
211218

212219
"test annotation node parameter assignment child" in {
@@ -255,6 +262,7 @@ class AnnotationTests extends JavaSrcCode2CpgFixture {
255262
annotationNode.code shouldBe "@NormalAnnotation(value = @OtherAnnotation)"
256263
annotationNode.name shouldBe "NormalAnnotation"
257264
annotationNode.fullName shouldBe "some.NormalAnnotation"
265+
annotationNode.lineNumber shouldBe Some(6)
258266
}
259267

260268
"test annotation node parameter value" in {
@@ -286,10 +294,12 @@ class AnnotationTests extends JavaSrcCode2CpgFixture {
286294
wildcard1.name shouldBe "Wildcard1"
287295
wildcard1.fullName shouldBe "b.Wildcard1"
288296
wildcard1.code shouldBe "@Wildcard1"
297+
wildcard1.lineNumber shouldBe Some(6)
289298

290299
wildcard2.name shouldBe "Wildcard2"
291300
wildcard2.fullName shouldBe "b.Wildcard2"
292301
wildcard2.code shouldBe "@Wildcard2"
302+
wildcard2.lineNumber shouldBe Some(7)
293303

294304
case result => fail(s"Expected 3 annotations for Foo but got $result")
295305
}

0 commit comments

Comments
 (0)