Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9fa8401

Browse files
tamasvajksmowton
authored andcommitted
Extract annotation declarations
1 parent 3b9b9b3 commit 9fa8401

7 files changed

Lines changed: 117 additions & 8 deletions

File tree

java/kotlin-extractor/src/main/kotlin/KotlinFileExtractor.kt

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,10 @@ open class KotlinFileExtractor(
470470
if (c.isInterfaceLike) {
471471
val interfaceId = id.cast<DbInterface>()
472472
tw.writeInterfaces(interfaceId, cls, pkgId, interfaceId)
473+
474+
if (kind == ClassKind.ANNOTATION_CLASS) {
475+
tw.writeIsAnnotType(interfaceId)
476+
}
473477
} else {
474478
val classId = id.cast<DbClass>()
475479
tw.writeClasses(classId, cls, pkgId, classId)
@@ -495,10 +499,20 @@ open class KotlinFileExtractor(
495499

496500
c.typeParameters.mapIndexed { idx, param -> extractTypeParameter(param, idx, javaClass?.typeParameters?.getOrNull(idx)) }
497501
if (extractDeclarations) {
498-
c.declarations.forEach { extractDeclaration(it, extractPrivateMembers = extractPrivateMembers, extractFunctionBodies = extractFunctionBodies) }
499-
if (extractStaticInitializer)
500-
extractStaticInitializer(c, { id })
501-
extractJvmStaticProxyMethods(c, id, extractPrivateMembers, extractFunctionBodies)
502+
if (kind == ClassKind.ANNOTATION_CLASS) {
503+
c.declarations
504+
.filterIsInstance<IrProperty>()
505+
.map {
506+
val getter = it.getter!!
507+
val label = extractFunction(getter, id, false, null, listOf())
508+
tw.writeIsAnnotElem(label!!.cast<DbMethod>())
509+
}
510+
} else {
511+
c.declarations.forEach { extractDeclaration(it, extractPrivateMembers = extractPrivateMembers, extractFunctionBodies = extractFunctionBodies) }
512+
if (extractStaticInitializer)
513+
extractStaticInitializer(c, { id })
514+
extractJvmStaticProxyMethods(c, id, extractPrivateMembers, extractFunctionBodies)
515+
}
502516
}
503517
if (c.isNonCompanionObject) {
504518
// For `object MyObject { ... }`, the .class has an
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
def.kt:
2+
# 0| [CompilationUnit] def
3+
# 0| 1: [Class] DefKt
4+
# 13| 1: [Method] fn
5+
#-----| 4: (Parameters)
6+
# 13| 0: [Parameter] a
7+
# 13| 5: [BlockStmt] { ... }
8+
# 14| 0: [ExprStmt] <Expr>;
9+
# 14| 0: [MethodAccess] println(...)
10+
# 14| -1: [TypeAccess] ConsoleKt
11+
# 14| 0: [MethodAccess] abc(...)
12+
# 14| -1: [VarAccess] a
13+
# 1| 2: [Interface] SomeAnnotation
14+
#-----| -3: (Annotations)
15+
# 1| 1: [Method] abc
16+
#-----| 1: (Annotations)
17+
# 1| 2: [Method] y
18+
# 3| 3: [Interface] ReplaceWith
19+
# 3| 1: [Method] expression
20+
# 5| 4: [Interface] Deprecated
21+
# 6| 1: [Method] message
22+
# 7| 2: [Method] replaceWith
23+
# 9| 5: [Class] X
24+
# 11| 1: [Constructor] X
25+
# 9| 5: [BlockStmt] { ... }
26+
# 9| 0: [SuperConstructorInvocationStmt] super(...)
27+
# 11| 1: [BlockStmt] { ... }
28+
use.java:
29+
# 0| [CompilationUnit] use
30+
# 2| 1: [Class] use
31+
#-----| -1: (Base Types)
32+
# 2| 0: [TypeAccess] SomeAnnotation
33+
# 4| 2: [Method] abc
34+
#-----| 1: (Annotations)
35+
# 3| 1: [Annotation] Override
36+
# 4| 3: [TypeAccess] int
37+
# 4| 5: [BlockStmt] { ... }
38+
# 4| 0: [ReturnStmt] return ...
39+
# 4| 0: [IntegerLiteral] 1
40+
# 6| 3: [Method] y
41+
#-----| 1: (Annotations)
42+
# 5| 1: [Annotation] Override
43+
# 6| 3: [TypeAccess] String
44+
# 6| 5: [BlockStmt] { ... }
45+
# 6| 0: [ReturnStmt] return ...
46+
# 6| 0: [StringLiteral] ""
47+
# 9| 4: [Method] annotationType
48+
#-----| 1: (Annotations)
49+
# 8| 1: [Annotation] Override
50+
# 9| 3: [TypeAccess] Class<? extends Annotation>
51+
# 9| 0: [WildcardTypeAccess] ? ...
52+
# 9| 0: [TypeAccess] Annotation
53+
# 9| 5: [BlockStmt] { ... }
54+
# 10| 0: [ReturnStmt] return ...
55+
# 10| 0: [NullLiteral] null
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
semmle/code/java/PrintAst.ql
Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,15 @@
1-
| def.kt:2:1:2:31 | SomeAnnotation | Interface |
2-
| use.java:2:23:2:25 | use | Class |
1+
annotation
2+
| def.kt:1:1:1:87 | SomeAnnotation | def.kt:1:53:1:66 | abc |
3+
| def.kt:1:1:1:87 | SomeAnnotation | def.kt:1:69:1:86 | y |
4+
| def.kt:3:1:3:52 | ReplaceWith | def.kt:3:30:3:51 | expression |
5+
| def.kt:5:1:7:51 | Deprecated | def.kt:6:5:6:23 | message |
6+
| def.kt:5:1:7:51 | Deprecated | def.kt:7:5:7:50 | replaceWith |
7+
| file:///modules/java.base/java/lang/annotation/Retention.class:0:0:0:0 | Retention | file:///modules/java.base/java/lang/annotation/Retention.class:0:0:0:0 | value |
8+
| file:///modules/java.base/java/lang/annotation/Target.class:0:0:0:0 | Target | file:///modules/java.base/java/lang/annotation/Target.class:0:0:0:0 | value |
9+
#select
10+
| def.kt:0:0:0:0 | DefKt | Class |
11+
| def.kt:1:1:1:87 | SomeAnnotation | Interface |
12+
| def.kt:3:1:3:52 | ReplaceWith | Interface |
13+
| def.kt:5:1:7:51 | Deprecated | Interface |
14+
| def.kt:9:1:11:7 | X | Class |
15+
| use.java:2:14:2:16 | use | Class |

java/ql/test/kotlin/library-tests/annotation_classes/classes.ql

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,7 @@ import java
33
from ClassOrInterface x
44
where x.fromSource()
55
select x, x.getPrimaryQlClasses()
6+
7+
query predicate annotation(AnnotationType at, AnnotationElement ae) {
8+
at.getAnAnnotationElement() = ae
9+
}
Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
annotation class SomeAnnotation(@get:JvmName("abc") val x: Int = 5, val y: String = "")
12

2-
annotation class SomeAnnotation
3+
annotation class ReplaceWith(val expression: String)
4+
5+
annotation class Deprecated(
6+
val message: String,
7+
val replaceWith: ReplaceWith = ReplaceWith(""))
8+
9+
@Deprecated("This class is deprecated", ReplaceWith("Y"))
10+
@SomeAnnotation(y = "a")
11+
class X
12+
13+
fun fn(a: SomeAnnotation) {
14+
println(a.x)
15+
}
316

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
11

2-
public abstract class use implements SomeAnnotation {}
2+
public class use implements SomeAnnotation {
3+
@Override
4+
public int abc() { return 1; }
5+
@Override
6+
public String y() { return ""; }
37

8+
@Override
9+
public Class<? extends java.lang.annotation.Annotation> annotationType() {
10+
return null;
11+
}
12+
}

0 commit comments

Comments
 (0)