Extract annotation declarations

This commit is contained in:
Tamas Vajk 2022-04-11 15:30:36 +02:00 коммит произвёл Chris Smowton
Родитель 3b9b9b339e
Коммит 9fa840106f
7 изменённых файлов: 117 добавлений и 8 удалений

Просмотреть файл

@ -470,6 +470,10 @@ open class KotlinFileExtractor(
if (c.isInterfaceLike) {
val interfaceId = id.cast<DbInterface>()
tw.writeInterfaces(interfaceId, cls, pkgId, interfaceId)
if (kind == ClassKind.ANNOTATION_CLASS) {
tw.writeIsAnnotType(interfaceId)
}
} else {
val classId = id.cast<DbClass>()
tw.writeClasses(classId, cls, pkgId, classId)
@ -495,10 +499,20 @@ open class KotlinFileExtractor(
c.typeParameters.mapIndexed { idx, param -> extractTypeParameter(param, idx, javaClass?.typeParameters?.getOrNull(idx)) }
if (extractDeclarations) {
c.declarations.forEach { extractDeclaration(it, extractPrivateMembers = extractPrivateMembers, extractFunctionBodies = extractFunctionBodies) }
if (extractStaticInitializer)
extractStaticInitializer(c, { id })
extractJvmStaticProxyMethods(c, id, extractPrivateMembers, extractFunctionBodies)
if (kind == ClassKind.ANNOTATION_CLASS) {
c.declarations
.filterIsInstance<IrProperty>()
.map {
val getter = it.getter!!
val label = extractFunction(getter, id, false, null, listOf())
tw.writeIsAnnotElem(label!!.cast<DbMethod>())
}
} else {
c.declarations.forEach { extractDeclaration(it, extractPrivateMembers = extractPrivateMembers, extractFunctionBodies = extractFunctionBodies) }
if (extractStaticInitializer)
extractStaticInitializer(c, { id })
extractJvmStaticProxyMethods(c, id, extractPrivateMembers, extractFunctionBodies)
}
}
if (c.isNonCompanionObject) {
// For `object MyObject { ... }`, the .class has an

Просмотреть файл

@ -0,0 +1,55 @@
def.kt:
# 0| [CompilationUnit] def
# 0| 1: [Class] DefKt
# 13| 1: [Method] fn
#-----| 4: (Parameters)
# 13| 0: [Parameter] a
# 13| 5: [BlockStmt] { ... }
# 14| 0: [ExprStmt] <Expr>;
# 14| 0: [MethodAccess] println(...)
# 14| -1: [TypeAccess] ConsoleKt
# 14| 0: [MethodAccess] abc(...)
# 14| -1: [VarAccess] a
# 1| 2: [Interface] SomeAnnotation
#-----| -3: (Annotations)
# 1| 1: [Method] abc
#-----| 1: (Annotations)
# 1| 2: [Method] y
# 3| 3: [Interface] ReplaceWith
# 3| 1: [Method] expression
# 5| 4: [Interface] Deprecated
# 6| 1: [Method] message
# 7| 2: [Method] replaceWith
# 9| 5: [Class] X
# 11| 1: [Constructor] X
# 9| 5: [BlockStmt] { ... }
# 9| 0: [SuperConstructorInvocationStmt] super(...)
# 11| 1: [BlockStmt] { ... }
use.java:
# 0| [CompilationUnit] use
# 2| 1: [Class] use
#-----| -1: (Base Types)
# 2| 0: [TypeAccess] SomeAnnotation
# 4| 2: [Method] abc
#-----| 1: (Annotations)
# 3| 1: [Annotation] Override
# 4| 3: [TypeAccess] int
# 4| 5: [BlockStmt] { ... }
# 4| 0: [ReturnStmt] return ...
# 4| 0: [IntegerLiteral] 1
# 6| 3: [Method] y
#-----| 1: (Annotations)
# 5| 1: [Annotation] Override
# 6| 3: [TypeAccess] String
# 6| 5: [BlockStmt] { ... }
# 6| 0: [ReturnStmt] return ...
# 6| 0: [StringLiteral] ""
# 9| 4: [Method] annotationType
#-----| 1: (Annotations)
# 8| 1: [Annotation] Override
# 9| 3: [TypeAccess] Class<? extends Annotation>
# 9| 0: [WildcardTypeAccess] ? ...
# 9| 0: [TypeAccess] Annotation
# 9| 5: [BlockStmt] { ... }
# 10| 0: [ReturnStmt] return ...
# 10| 0: [NullLiteral] null

Просмотреть файл

@ -0,0 +1 @@
semmle/code/java/PrintAst.ql

Просмотреть файл

@ -1,2 +1,15 @@
| def.kt:2:1:2:31 | SomeAnnotation | Interface |
| use.java:2:23:2:25 | use | Class |
annotation
| def.kt:1:1:1:87 | SomeAnnotation | def.kt:1:53:1:66 | abc |
| def.kt:1:1:1:87 | SomeAnnotation | def.kt:1:69:1:86 | y |
| def.kt:3:1:3:52 | ReplaceWith | def.kt:3:30:3:51 | expression |
| def.kt:5:1:7:51 | Deprecated | def.kt:6:5:6:23 | message |
| def.kt:5:1:7:51 | Deprecated | def.kt:7:5:7:50 | replaceWith |
| 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 |
| 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 |
#select
| def.kt:0:0:0:0 | DefKt | Class |
| def.kt:1:1:1:87 | SomeAnnotation | Interface |
| def.kt:3:1:3:52 | ReplaceWith | Interface |
| def.kt:5:1:7:51 | Deprecated | Interface |
| def.kt:9:1:11:7 | X | Class |
| use.java:2:14:2:16 | use | Class |

Просмотреть файл

@ -3,3 +3,7 @@ import java
from ClassOrInterface x
where x.fromSource()
select x, x.getPrimaryQlClasses()
query predicate annotation(AnnotationType at, AnnotationElement ae) {
at.getAnAnnotationElement() = ae
}

Просмотреть файл

@ -1,3 +1,16 @@
annotation class SomeAnnotation(@get:JvmName("abc") val x: Int = 5, val y: String = "")
annotation class SomeAnnotation
annotation class ReplaceWith(val expression: String)
annotation class Deprecated(
val message: String,
val replaceWith: ReplaceWith = ReplaceWith(""))
@Deprecated("This class is deprecated", ReplaceWith("Y"))
@SomeAnnotation(y = "a")
class X
fun fn(a: SomeAnnotation) {
println(a.x)
}

Просмотреть файл

@ -1,3 +1,12 @@
public abstract class use implements SomeAnnotation {}
public class use implements SomeAnnotation {
@Override
public int abc() { return 1; }
@Override
public String y() { return ""; }
@Override
public Class<? extends java.lang.annotation.Annotation> annotationType() {
return null;
}
}